Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
C++ supports the concept of reusability, so that data redundancy can be reduced. The classes can be reused in many ways, and the most convenient one is inheritance. The mechanism of creating new classes, reusing the properties of the existing one is called inheritance (or derivation).
Various inheritances
1. Single inheritance Here the derived class inherits only one class, the base one. Base
Derived Here the derived class inherits the two member functions setD() and getD(). We can get different results by changing the visibility mode. a. If the mode is public then the public members of the base class are inherited in the public section of the derived class. b. If the mode is private then the public members of the base class are inherited in the private section of the derived class.
Making private members inheritable
So we can see that the private members of the base class cannot be inherited. But there may arise some situation where we need to inherit the private member also. C++ introduces a third access modifier known as protected.
class Base { int x; public: void setB(int a) {x=a;} int getB() {return x;} }; class Derived:public Base //publicly derived { int y; public: void setD(int a) {y=a;} int getD() {return y;} }; int main() { Derived obj; obj.setB(10); /*base member function is accesed*/ obj.setD(11); cout<<obj.getB(); //displays 10 cout<<obj.getD(); //displays 11 return 0; }
A protected member behaves same like an private member, except that it can be inherited. Now if we inherit a class the derivation will be as follows: Table: Visibility of members Base class visibility Private Protected Public Derived class visibility Public derivation Private derivation Protected derivation Not inherited Not inherited Not inherited Protected Private Protected Public Private Protected
2. Multilevel inheritance Here a class is derived from a class which is derived from another class.
Base
3. Multiple inheritance Here a class is derived from more than one class. Base1 Base2 class Base1 { Derived int x; public: void setx(int a) {x=a;} int getx() {return x;} }; class Base2 { int y; public: void sety(int a) {y=a;} int gety() {return y;} }; class Derived:public Base1, public Base2 { //multiple classes can be inherited //with comma separation int z; public: void set(int a) {z=a;} int get() {return z;} }; int main() { Derived obj; obj.setx(10; obj.sety(11); obj.set(12); cout<<obj.getx(); //displays 10 cout<<obj.gety(); //displays 11 cout<<obj.get(); //displays 12 return 0; }
Derived1 class Base { int x; Derived2 public: void setB(int a) {x=a;} int getB() {return x;} }; class Derived1:public Base { int y; public: void setD(int a) {y=a;} int getD() {return y;} }; class Derived2:public Derived1 { int z; public: void set(int a) {z=a;} int get() {return z;} }; int main() { Derived2 obj; obj.setB(10; obj.setD(11); obj.set(12); cout<<obj.getB(); //displays 10 cout<<obj.getD(); //displays 11 cout<<obj.get(); //displays 12 return 0; }
4. Hierarchical inheritance Here a single class serves as a base class for more than one derived classes. It usually makes a tree structure. class A int main() Base { { Derived public: B b; void fA() C c; {cout<<fA() is called\n;} b.fA(); Derived2 Derived1 }; b.fB(); class B: public A c.fA(); { c.fC(); public: return 0; void fB() } {cout<<fB() is called\n;} }; Output class C: public A { fA() is called public: fB() is called void fC() fA() is called {cout<<fC() is called\n;} fC() is called };
5. Hybrid inheritance As the name indicates, it is simply the mixture of all above kinds of inheritances. class A { public: void fA() {cout<<fA() is called\n;} }; class B: public A { public: void fB() {cout<<fB() is called\n;} }; class C { public: void fC() {cout<<fC() is called\n;} }; class D: public B, public C { public: void fD() {cout<<fD() is called\n;} }; Base2 Base2
Derived1 int main() { D obj; obj.fA(); obj.fB(); obj.fC(); obj.fD(); return 0; } Output fA() is called fB() is called fA() is called fC() is called
Derived2
Derived3
Ex.1
Ex.2 class A { public: void show(); }; class B: public A {}; class C: public A, public B {}; class D: public B, public C {}; void main() { D obj; Obj.show();
Derived1 Derived2
class A { public: void show(); }; class B { public: void show(); }; class C: public A, public B {}; void main() { C obj; Obj.show(); /*ambiguity, which version of show() is to be called*/ }
Derived3
Solution:
Using the scope resolution operator and passing the actual owner class to call the members.(see Ex.1)
Ex.2 Ex.1 class A { public: void show(); }; class B { public: void show(); }; class C: public A, public B {}; void main() { C obj; Obj.A::show(); //calls show() from class A Obj.B::show(); //calls show() from class A } class A { public: void show(); }; class B { public: void show(); }; class C: public A, public B { public: void show(); //override both of the //inherited functions }; void main() { C obj; Obj.show(); //calls show() from class C }
Ex.3 class A { .. };
Output
constructor of class a called constructor of class b called constructor of class c called constructor of class d called constructor of class e called constructor of class f called destructor of class f called destructor of class e called destructor of class d called destructor of class c called destructor of class b called destructor of class a called
bbb