All variables in new object are set to zero, despite default arguments in constructor

99 views Asked by At

I am writing a student project. For some reason all variables of this object are set to 0 or false, despite the fact that the constructor have default arguments set. The class of this object inherit after another class. So, it looks like this:

class Weapon
public:
constructor()
[some functions]
private:
[some variables]
class WeaponShortBow : inherit publicly after Weapon
public:
constructor (default arguments)
[one other function]
private:
[nothing]

The constructor is being called inside the constructor of object of other class.

here is code:

Weapon.h

#ifndef DNDTESTY_WEAPON_H
#define DNDTESTY_WEAPON_H
#include "Effects/Effect.h"

class Weapon {
public:
    Weapon();

    void removeAmmo();

    int getRange() const;

    int and_so_on() const;

private:
    int Amunition;
    int Range;
    int TypeOfDice;
    int NumberOfDice;
    int HitModiff;
    bool IsFniesse;
    bool IsTwoHanded;
    bool IsMagical;
    bool IsLight;
    char Category;
    Effect *effect;
};

Weapon.cpp

#include "Weapon.h"

Weapon::Weapon(){
}

void Weapon::removeAmmo() {
    this->Amunition += -1;
}

int Weapon::getRange() const {
    return Range;
}

int Weapon::and_so_on() const {
    return and_so_on;
}

WeaponShortBow.h

#include "Effects/Effect.h"
#include "Effects/NoEffect.h"
#include "Weapon.h"

class WeaponShortBow : public Weapon{
public:
    WeaponShortBow(int Amunition = 12,
                   int Range = 24,
                   int TypeOfDice = 6,
                   int NumberOfDice = 1,
                   int HitModiff = 0,
                   bool IsFniesse = false,
                   bool IsTwoHanded = true,
                   bool IsMagical = false,
                   bool IsLight = false,
                   char Category = 'R');

    ~WeaponShortBow();

    Effect* ApplyShortBowEffect();
private:

};

WeaponShortBow.cpp

#include "WeaponShortBow.h"
#include "Effects/NoEffect.h"

WeaponShortBow::WeaponShortBow(
        int Amunition,
        int Range,
        int TypeOfDice,
        int NumberOfDice,
        int HitModiff,
        bool IsFniesse,
        bool IsTwoHanded,
        bool IsMagical,
        bool IsLight,
        char Category){
}

Effect* WeaponShortBow::ApplyShortBowEffect(){
    Effect* tmp = new NoEffect;
    return tmp;
}

the pointer to this object is stored here Actor.h

#include <string>
#include "DiceRoller.h"
#include "Weapons/Weapon.h"
#include "Weapons/WeaponShortBow.h"
#include "Weapons/Effects/Effect.h"

using namespace std;

class Actor {
public:

    Actor(string name, char token);
    virtual ~Actor();

    string name;
    char token;

    int some_functions();


private:

    int totalHP = 12;
    int lostHP = 0;

    int totalAC = 12;
    int lostAc = 0;

    int initiativeBonus = 0;
    int proficiency = 0;
    int speed = 6;

    int AbStr=14;
    int ModStr=(AbStr-10)/2;
    int AbDex=12;
    int ModDex=(AbDex-10)/2;
    int AbCon=12;
    int ModCon=(AbCon-10)/2;
    int AbInt=12;
    int ModInt=(AbInt-10)/2;
    int AbWis=12;
    int ModWis=(AbWis-10)/2;
    int AbCha=12;
    int ModCha=(AbCha-10)/2;

    Weapon* weapon;
    Effect* effect;

};

and here is Actor.cpp

#include "Actor.h"

using namespace std;

Actor::Actor(string name, char token) {
    this->name=name;
    this->token=token;
    this->weapon= new WeaponShortBow;
}
Actor::~Actor(){}


void Actor::reciveDmg(int dmg){
    this->lostHP=(this->lostHP)+dmg;
}

void Actor::some other functions(){}

I tried giving the values to the constructor in braces, but it does not seem to solve the problem. I also changed the type of pointer in actor.h from Weapon* to WeaponShortBow*, and instead of giving the default arguments in constructor, give them value in WeaponShortBow.h in private: like this private: int whatever = 2;

1

There are 1 answers

0
Ted Lyngmo On
  • WeaponShortBow takes a bunch of arguments, but does nothing with them. They are not automatically stored in the base class, Weapon.
  • Currently, there is no way for WeaponShortBow to actually store the arguments in the base class. The member variables are private and the base class, Weapon, has no constructor for accepting the arguments.

Consider this simplified version with only one of the arguments. Weapon here has a constructor taking the argument Ammunition. I've made it protected so that only derived classes can use it, but that's optional:

class Weapon {
public:
    Weapon() = default; // no need to provide an empty implementation this way

    int getAmmo() const;

protected: // if only derived classes should be able to use the constructor
    Weapon(int Ammunition);

private:
    int m_ammunition = 0; // used when default initialized
};

class WeaponShortBow : public Weapon{
public:
    WeaponShortBow(int Ammunition = 12);
};

The Weapon constructor taking the Ammunition parameter uses the member initializer list to initialize the member variable with the value given to the constructor:

Weapon::Weapon(int Ammunition) : m_ammunition(Ammunition) {}
int Weapon::getAmmo() const { return m_ammunition; }

Similarly, the WeaponShortBow constructor taking the Ammunition parameter uses that to initialize the base class:

WeaponShortBow::WeaponShortBow(int Ammunition) : Weapon(Ammunition) {}

So, the initialization goes:
WeaponShortBow constructor -> Weapon constructor -> Weapon member variable.

Demo