I'm wondering about the syntax for inheritance and why I have to use public like this:
Code:class myClass: public myBase
I'm wondering about the syntax for inheritance and why I have to use public like this:
Code:class myClass: public myBase
You don't have to but if you inherit privately or protected you are not able to access the methods of myClass via a pointer to myBaseOriginally Posted by antex
try this
ant try what happens if you change public to private or protected.Code:class myBase { public: virtual void do_sth() = 0; void do_sth_else() {} }; class myClass: public myBase { //class myClass: private myBase { public: virtual void do_sth() { do_sth_else(); } }; int main() { myClass mc; mc.do_sth(); myBase * bp = new myClass; bp->do_sth(); }
Kurt
Expanding upon what ZuK said, basically the access control level determines the access levels for the derived class.
So if you want all the access levels to be the same as what is in the base class, you derive it publicly. Otherwise, if you want to make all the members of the base class private in the derived class, you derive it privately. And lastly, if you want the public/protected members of the base class to be protected in the derived class, you derive it protected(ly).
The cost of software maintenance increases with the square of the programmer's creativity.
Ok, I see, this made it much easier to understand. Thank you both!