Inheritance

5,822 views
5,666 views

Published on

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,822
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
411
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Inheritance

  1. 1. INHERITANCE
  2. 2. <ul><li>DEFINATION </li></ul><ul><li>Inheritance is a mechanism of creating new classes called derived class from existing ones i.e. base classes. </li></ul><ul><li>Inheritance is a most powerful feature of object oriented programming. </li></ul>
  3. 3. Types of Inheritance <ul><li>Single Inheritance </li></ul><ul><li>Multiple Inheritance </li></ul><ul><li>Multilevel Inheritance </li></ul><ul><li>Hierarchical Inheritance </li></ul><ul><li>Hybrid Inheritance </li></ul>
  4. 4. SINGLE INHERITANCE <ul><li>When a derived class inherits only from one base class ,it is Known as single Inheritance. </li></ul><ul><li>Base Class </li></ul><ul><li>Derived Class </li></ul>A B
  5. 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. 6. MULTILEVEL INHERITANCE <ul><li>When a derived class inherit from a class that itself inherits from other class , it is known as multilevel Inheritance. </li></ul><ul><li>Base Class </li></ul><ul><li>Derived Class of x </li></ul><ul><li>Base Class of z </li></ul><ul><li>Derived Class </li></ul>A B C
  7. 7. HYBRID INHERITANCE <ul><li>Hybrid Inheritance combines two or more forms of Inheritance. </li></ul>w x y z
  8. 8. HIERARCHICAL INHERITANCE <ul><li>When many derived classes inherit from a single base class , it is known as hierarchical Inheritance. </li></ul><ul><li>Base Class </li></ul><ul><li>Derived classes </li></ul>a b c d
  9. 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. 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. 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. 12. Visibility Modes <ul><li>DEFINATION:- </li></ul><ul><li>Visibility Mode specifies whether the features of the base class are privately derived or publicly derived or protected derived. </li></ul><ul><li>Public Visibility Mode - It means that the derived class can access the public and protected members of the base class. The public members of the base class become the public member of the derived class, and the protected member of the base class become the protected member of the derived class. </li></ul>
  13. 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. 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. 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. 16. Inheritance and Constructors and Destructors <ul><li>When an object of a derived class is created, the program first calls the constructor for base class. </li></ul><ul><li>When an object expires, the program first calls the derived destructor and then base destructor </li></ul>
  17. 17. Class super { …… . }; Class Sub : public Super { ….. }; int main() { sub ob1; …… . }
  18. 18. Base class Constructors <ul><li>Base class constructor require arguments to construct their objects. </li></ul><ul><li>It is the responsibility of derived class constructor to pass those arguments to base class. </li></ul><ul><li>Syntax : </li></ul><ul><li>Derived ::Derived (type1 x, type2 y ….) : base (x , y) </li></ul><ul><li>{ </li></ul><ul><li>… .. </li></ul><ul><li>} </li></ul>
  19. 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. 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. 21. Constructor in Derived Class <ul><li># include<iostream.h> </li></ul><ul><li>class alpha </li></ul><ul><li>{ </li></ul><ul><li>protected: </li></ul><ul><li>int x; </li></ul><ul><li>alpha(int i) </li></ul><ul><li>{ </li></ul><ul><li>x=i; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>class beta </li></ul><ul><li>{ </li></ul><ul><li>protected: </li></ul><ul><li>float y; </li></ul><ul><li>Beta(float j) </li></ul><ul><li>{ </li></ul><ul><li> y=j; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>
  22. 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. 23. Facts about inheritance <ul><li>Private members of a base class are not directly accessible in the derived class. </li></ul><ul><li>Example </li></ul><ul><li>int test; Global variable </li></ul><ul><li>Class base { int test; private members of base </li></ul><ul><li> public: </li></ul><ul><li>void getit() </li></ul><ul><li>{ cin>> test; </li></ul><ul><li>} </li></ul><ul><li> }; </li></ul><ul><li>Class derived : public base </li></ul><ul><li>{ public : </li></ul><ul><li>void check ( ) </li></ul><ul><li>{ test++; } not allowed </li></ul><ul><li>}; </li></ul>
  24. 24. <ul><li>When a class is derived publicly, you cannot selectively deny access to base members by declaring them in derived private section of the derived class. </li></ul><ul><li>Example </li></ul><ul><li>Class base { </li></ul><ul><li>Public : </li></ul><ul><li>int x,y,z; </li></ul><ul><li>}; </li></ul><ul><li>Class derived : public base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>int a; </li></ul><ul><li>Private : </li></ul><ul><li>Base :: x; // Invalid </li></ul><ul><li>} </li></ul>
  25. 25. <ul><li>When you derive a class privately, you can selectively allow access to some of the base members. To allow access to some of the inherited members, you can selectively declare some of the base class members in the public section of the privately derived class. </li></ul><ul><li>Example </li></ul><ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public : </li></ul><ul><li>int x,y,z; </li></ul><ul><li>}; </li></ul><ul><li>Class derived : private base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>int a; </li></ul><ul><li>Base :: x; alid </li></ul><ul><li>} </li></ul><ul><li>Or </li></ul><ul><li>Public : </li></ul><ul><li>Using Base :: x ; </li></ul>
  26. 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. 27. Constructors in Multiple inheritance <ul><li>As long as no base class constructor takes any arguments, the derived class need not have a constructor function. However, if a base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base constructor. </li></ul><ul><li>In multiple inheritance , the base classes are constructed in the order in which they appear in the declaration of the derived class. </li></ul><ul><li>Example </li></ul><ul><li>Class Base1 </li></ul><ul><li>{ protected : </li></ul><ul><li>int a; </li></ul><ul><li>public : </li></ul><ul><li>Base1(int x) </li></ul><ul><li>{ a = x; cout<<“Constructing Base 1”; } </li></ul><ul><li>~Base1( ) </li></ul><ul><li>{ cout<<“Destructing Base1”; } </li></ul><ul><li>}; </li></ul>
  28. 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. 29. ~ Derived ( ) { cout << Destructing Derived “; } Void show( ) { cout <<“1.”<<a<<“ 2.”<<b <<“ 3.”<<c; } }; Int main ( ) { Clrscr(); Derived ob(14,15,16); Ob.show(); } <ul><li>OUTPUT : </li></ul><ul><li>Constructing Base2 </li></ul><ul><li>Constructing Base1 </li></ul><ul><li>Constructing Derived </li></ul><ul><li>15 2. 14 3. 16 </li></ul><ul><li>Destructing Derived </li></ul><ul><li>Destructing Base1 </li></ul><ul><li>Destructing Base2 </li></ul>
  30. 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<<“ ”<<“ ”; }
  31. 31. <ul><li>To Use Scope resolution operator to a </li></ul><ul><li>Void main ( ) </li></ul><ul><li>{ </li></ul><ul><li>D3 ob; </li></ul><ul><li>ob.D1 :: a = 25 scope resolved </li></ul><ul><li>Ob.b = 50; </li></ul><ul><li>Ob.c = 75; </li></ul><ul><li>Ob.total = ob.D1 :: a +ob.b + ob.c; </li></ul><ul><li>Cout <<ob.D1 :: a<<“ ”<< ob.b<<“ ”<< ob.c<<“ ”<< ob.total<<“ ”<<“ ”; </li></ul><ul><li>} </li></ul>Remedy
  32. 32. <ul><li>To Use a keyword Virtual </li></ul><ul><li>Example </li></ul><ul><li>Class Base </li></ul><ul><li>{ public : </li></ul><ul><li>int a ; </li></ul><ul><li>}; </li></ul><ul><li>Class D1 : virtual public Base D1 inherits Base as Virtual </li></ul><ul><li>{ public : </li></ul><ul><li>int b ; </li></ul><ul><li>}; </li></ul><ul><li>Class D2 : virtual public Base </li></ul><ul><li>{ public : </li></ul><ul><li>int c ; </li></ul><ul><li>}; </li></ul><ul><li>Class D3 : public D1, public D2 this time only one copy of base </li></ul><ul><li>{ public : </li></ul><ul><li>int total; </li></ul><ul><li>}; </li></ul>
  33. 33. Void main ( ) { D3 ob; ob.a = 25 now unambiguous Ob.b = 50; Ob.c = 75; Ob.total = ob.a +ob.b + ob.c; Cout <<ob.a<<“ ”<< ob.b<<“ ”<< ob.c<<“ ”<< ob.total<<“ ”<<“ ”; }
  34. 34. Example of Multilevel Inheritance <ul><li> #include<iostream.h> </li></ul><ul><li> class student </li></ul><ul><li> { </li></ul><ul><li> protected: </li></ul><ul><li> int roll_number; </li></ul><ul><li> public: </li></ul><ul><li> void get_number() </li></ul><ul><li> { </li></ul><ul><li> cout<<&quot;Enter rollno&quot;; </li></ul><ul><li> cin>>roll_number; </li></ul><ul><li> } </li></ul><ul><li> void putnumber() </li></ul><ul><li> { </li></ul><ul><li> cout<<roll_number; </li></ul><ul><li> } </li></ul><ul><li> }; </li></ul>
  35. 35. class test: public student { protected: float sub1; float sub2; public: void getmarks() { cout<<&quot;enter the marks of sub1,sub2&quot;; cin>>sub1>>sub2; } void putmarks() { cout<<&quot;marks in sub1=&quot;<<sub1; cout<<&quot;marks in sub2=&quot;<<sub2; } };
  36. 36. class result : public test { float total; public: void display(void) { total=sub1+sub2; putnumber(); putmarks(); cout<<&quot;Total&quot;<<total; } }; void main() { result a; a.get_number(); a.getmarks(); a.display(); }
  37. 37. ANY QUERY THANK YOU

×