5. When a derived class inherit from multiple base classes it is known as multiple Inheritance. A B C MULTIPLE INHERITANCE Base Class Derived Class
6.
7.
8.
9. A general form to defining a derived class is: Class derived class name: visibility mode base class name { members of derived class } ; Class ABC: public XYZ { members of ABC }; DEFINING DERIVED CLASS
10. Example of derived class definition is: Class Sub : public Super public derivation { ……… members of sub }; Class Sub: private Super private derivation { …… ... members of sub }; Class Sub: protected Super protected derivation { …… ... members of sub };
11. MULTIPLE INHERITANCE Example of derived class definition is: Class derived_class : vis_mode base1, vis_mode base 2 { ………… . members of derived class }; Class Sub : public SuperA, private SuperB { ……… members of sub };
12.
13. Private Visibility Mode- The public and protected members of the base class become private members of the derived class. The inherited members can only be accessed only through the member function of derived class. Protected Visibility Mode- The public and protected members of base class become protected members of the derived class.
14. Visibility of Inherited base class members in Derived Class. Visibility Mode Public members of base class becomes Protected members of base class becomes Private members of the base class is not accessible to the derived class. Public Public Protected Protected Protected Protected Private Private Private
15. Accessibility of Base class members No No Yes Private No Yes Yes Protected Yes Yes Yes Public Accessible from objects outside class Accessible from derived class Accessible from own class Access Specifier
16.
17. Class super { …… . }; Class Sub : public Super { ….. }; int main() { sub ob1; …… . }
18.
19. Class Base { int a; float b; public : Base(int I, float j) { a = i; b = j; } … }; Class Derived : public Base {…. Public : Derived ( int p, float q) : Base (p , q) { } }; Even if derived const does not need a parameter, yet it accepts parameter for base const.
20. Class Base { int a; float b; public : Base( int i, float j) { a = i; b = j; } … }; Class Derived : public Base { int x; float y; Public : Derived ( int i, int j , int p, float q) : Base (p , q) { x = i ; Y = j ; } }; Derived Const is accepting parameter for itself( i ,j) and ( p , q) for Base const
21.
22. Class gamma: public beta, public alpha { int k; public: gamma(int a,float b, int c): alpha(a) , beta(b) { k=c; } void show() { cout<<“1”<<x<<“2”<<y<<“3”<<k; }}; void main() { gamma g(14,15.2,17); g.show(); }
23.
24.
25.
26. Abstract class A class that serves only as a base class from which other classes can be derived, but no objects of this base type exist, is known as abstract class.
27.
28. Class base2 { Protected : int b; public : Base2(int y) { b = y; cout<<“Constructing Base2”; } ~Base2 ( ) { cout<< Destructing Base2 “; } }; Class derived : public Base2, public Base1 { int c; public : derived (int I, int j, int k): Base2(i), Base1(j) { c = k; cout <<“Constructing Derived”; }
29.
30. Virtual Base class Example Class Base { public : int a ; }; Class D1 : public Base D1 inherits Base { public : int b ; }; Class D2 : public Base D2 inherits Base { public : int c ; }; Class D3 : public D1, public D2 D3 inherits D1 and D2 { public : int total; }; Void main ( ) { D3 ob; ob.a = 25 this is ambiguous Ob.b = 50; Ob.c = 75; Ob.total = ob.a +ob.b + ob.c; Cout <<ob.a<<“”<< ob.b<<“”<< ob.c<<“”<< ob.total<<“”<<“”; }
35. class test: public student { protected: float sub1; float sub2; public: void getmarks() { cout<<"enter the marks of sub1,sub2"; cin>>sub1>>sub2; } void putmarks() { cout<<"marks in sub1="<<sub1; cout<<"marks in sub2="<<sub2; } };
36. class result : public test { float total; public: void display(void) { total=sub1+sub2; putnumber(); putmarks(); cout<<"Total"<<total; } }; void main() { result a; a.get_number(); a.getmarks(); a.display(); }