Derived Classes and Constructors

I am having trouble learning the correct syntax for when an object of a derived class tries to call the constructor of the base class. For example, say I have a header file that includes the declarations for a base class and derived class.

The constructor for the base class:
Base(const string& name, const int& age)

The constructor for the derived class:
Derived(const int& height, const int& weight)

In the Base class, we have private member variables TheName (initialized by the constructor through name) and TheAge (initialized by the constructor through age). In the Derived class, the private variables are TheHeight and TheWeight which are initialized through height and weight respectively.

In the implementation file for the base class, I know that the definition for the constructor will look like this:
1
2
3
4
Base:Base(const string& name, const int& age): TheName(name), TheAge(age)
{

}


How would the syntax for the constructor of the Derived class look like when declaring it in the header and defining it in the implementation file?

Also, in an application file, how would I make an object of the Derived class? All I do know is that making an object of the Base class would look like this:
Base("Sally", 10);
Your derived ctor needs to take four arguments in general: two to call Base ctor, and two to populate its own members:

1
2
3
4
5
6
Derived::Derived(const string& name, int age, int height, int weight)
 : Base(name, age),
   TheHeight(height),
   TheWeight(weight)
{
}
Topic archived. No new replies allowed.