programs of Hierarchical inheritance :
// hyrachicle inheritance #include<iostream> using namespace std; class A{ int a; public: void set_a(int a1){ a=a1; } int get_a(){ return a; } }; class B: public A{ int b; public: void set_b(int b1){ b=b1; } int addition(){ return b+get_a(); } }; class C: public A{ int c; public: void set_c(int c1){ c=c1; } int division(){ return get_a()/c; } }; int main(){ B obj; obj.set_a(10); obj.set_b(5); cout<<"Addition a+b= "<<obj.addition(); C obj1; obj1.set_a(20); obj1.set_c(2); cout<<"\nDivided a/c= "<<obj1.division(); };
Addition a+b= 15Divided a/c = 10
If you like code with vibhu and would like to contribute, you can also write an article using This link :
https://docs.google.com/forms/d/e/1FAIpQLScAmvlPvNUz35R-G0nc_zpRVP3o8xlhtFgC3aKPyLetX_RyXg/viewform?pli=1
OR mail your article to codewithvibhu@gmail.com. See your article appearing on the code with vibhu main page and help other students.
0 Comments