I've been through the internet and back, trying to looks for forum posts or tutorials on how to inherit a constructor from a parent class to a child class. I understand that when inheriting, constructors don't automatically inherit. How do I make it so that they do? I've been trying to experiment, but I'm not sure if the constructor is actually inheriting when I do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//IN PARENT.H
class Parent
{
protected:
int health, xp, atkPow;
public:
Base(int a, int b, int c);
};
//IN PARENT.CCP
Parent::Parent(int a, int b, int c)
{
health = a;
xp = b;
atkPow = c;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//IN CHILD.H
class Child : public Parent
{
public:
Child(int a, int b, int c);
};
//IN CHILD.CPP
//Is the following line the correct way to inherit a constructor?
Child::Child(int a, int b, int c) : Base(a, b, c)
{
//Are the contents of the parent constructor copied here?
}
If that is not the correct way to inherit a constructor, could you please teach me the right way to do so, or lead me to an informative tutorial on how to do so?