Class inheritance
May 13, 2020 at 5:56am May 13, 2020 at 5:56am UTC
I want to override the Accuracy and the Damage. What am I doing wrong here? Is the only way to override the attributes is to use a constructor?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
class Pistol
{
public :
struct Attributes
{
public :
int Accuracy = 2;
int Damage = 2;
int Reload = 2;
int DrawSpeed = 2;
}Attributes;
};
class Baretta : public Pistol
{
public :
struct Attributes
{
public :
int Accuracy = 5;
int Damage = 2;
int Reload;
int DrawSpeed;
}Attributes;
};
May 13, 2020 at 7:10am May 13, 2020 at 7:10am UTC
1 2 3 4 5 6 7 8 9 10
class Pistol
{
public :
struct Attributes
{
// something
};
Attributes Attributes; // type and member have same name
};
Types Baretta::Attributes and Pistol::Attributes are two distinct types.
Baretta has two
member variables : Attributes and Pistol::Attributes
Utterly confusing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class Pistol
{
public :
int Accuracy = 2;
int Damage = 2;
int Reload = 2;
int DrawSpeed = 2;
Pistol() = default ;
Pistol(int a, int d) : Accuracy{a}, Damage{d} {}
};
class Baretta : public Pistol
{
public :
Baretta() : Pistol( 5, 42 ) {}
};
Topic archived. No new replies allowed.