A second question about inheritance

1
2
3
4
5
// the base class. No definition for ctor and dtor.
class base{
int x;
...
}


1
2
3
4
5
6
7
8
9
10
11
// the derived class
class derived: public base{
// the derived class has the ctor and dtor explicitly defined.
derived()
{
x = 100;
...
}
~derived();
...
}


My question is:

Is it legal to give the value of a variable of the base class in the derived class' ctor, like what I have shown in the above code snippet? Thank you!
In that example, no. That's because x is is the private section of the base class. Try making it protected and then it would be legal. For more information, search for access specifiers.

http://www.cplusplus.com/doc/tutorial/classes/
Last edited on
moorecm,

Thank you for your explanation!
Topic archived. No new replies allowed.