Keyword protected
In this program, length and breadth in the base class are protected data members. These data members are accessible from the derived class but, not accessible from outside it. This maintains the feature of data hiding in C++ programming. If you defined length and breadth as private members then, those two data are not accessible to derived class and if defined as public members, it can be accessible from both derived class and from
main( )
function.Accessbility | private | protected | public |
---|---|---|---|
Accessible from own class ? | yes | yes | yes |
Accessible from dervied class ? | no | yes | yes |
Accessible outside dervied class ? | no | no | yes |
Member Function Overriding in Inheritance
Suppose, base class and derived class have member functions with same name and arguments. If you create an object of derived class and write code to access that member function then, the member function in derived class is only invoked, i.e., the member function of derived class overrides the member function of base class.
No comments:
Post a Comment