Child class reading parent class methods
Understanding how a child class object can read the parent class methods/functions easily.
Learning Objectives
- Learning how child class can read parent class methods or functions once it is inherited with public accessibility.
Source Code
|
Run Output
Code Understanding
class FlyingThing { public: int max_height(){return 11300;}};
A class which is parent class of class Bird in this case. It has a method to return max_height a bird can fly.
class Bird:public FlyingThing { public: int max_wing_spread(){return 4;}};
A derived class which is derived from its parent class FlyingThing. It has a method which returns max-wing spread a bird can spread.
int main() {
Bird b;
Declaring of an object of child class only. Parent class object is not declared.
cout<<“Some birds can fly up to ” <<b.max_height()<<” meters.”<<endl;
Here we are using method max_height of parent class with object of child class. This is possible due to inheritance only.
cout<<“Some birds can have max wing span of “<<b.max_wing_spread()<<” meters.”<<endl;
Here we are using method max_wing_spread of child class only. This is possible to use here being it to be a public access.
return 0;
}
Notes
- public accessibility is important while inheriting the child class in this type of code. Other accessibility may pose certain limits.
Suggested Filename(s): smpl-inh2.cpp,simple-inheritance2.cpp,child-reading-parent-methods.cpp
sunmitra| Created: 15-Sep-2018 | Updated: 15-Sep-2018|