Inheritance It is the mechanism of deriving  new class from existing class.It provides the idea of reusability. A B Base ,...
Types of inheritance <ul><li>Single level </li></ul>B A Multilevel Multiple Hierarchical hybrid B A C A B C A B A C A D A ...
<ul><li>Public  members provides lowest security and it can be accessed in whole programme. </li></ul><ul><li>Private  mem...
syntax <ul><li>Class classname:  mode  oldclassname </li></ul><ul><li>Mode: </li></ul><ul><li>Public   : inherit all membe...
<ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Private: </li></ul><ul><li>Int x; </li></ul><ul><li>Protected: </...
Single level inheritance <ul><li>#include<iostream.h> </li></ul><ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>P...
<ul><li>#include<iostream.h> </li></ul><ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Int x; </li></ul><ul><li>P...
<ul><li>#include<iostream.h> </li></ul><ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>int x; </li></ul><ul><li>P...
Private inheritance <ul><li>#include<iostream.h> </li></ul><ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public...
<ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Int x; </li></ul><ul><li>Public: </li></ul><ul><li>Get_base(int a...
Function overriding
<ul><li>#include<iostream.h> </li></ul><ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Int x; </li></ul><ul><li>P...
<ul><li>#include<iostream.h> </li></ul><ul><li>Class area </li></ul><ul><li>{ </li></ul><ul><li>Protected: </li></ul><ul><...
<ul><li>#include<iostream.h> </li></ul><ul><li>Class area </li></ul><ul><li>{ </li></ul><ul><li>Protected: </li></ul><ul><...
Multilevel inheritance <ul><li>#include<iostream.h> </li></ul><ul><li>Class first </li></ul><ul><li>{ </li></ul><ul><li>Pu...
Multiple inheritance <ul><li>#include<iostream.h> </li></ul><ul><li>Class super </li></ul><ul><li>{ </li></ul><ul><li>Prot...
Hierarchical inheritance a b c d e f g h i j
<ul><li>#include<iostream.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>Class university </li></ul><ul><li>{ <...
Hybrid <ul><li>student </li></ul>Test sports result
<ul><li>class student { private : int rn; char na[20]; public: void getdata() { cout< < &quot;Enter Name And Roll No : &qu...
<ul><li>void main() { results r[5]; clrscr(); for(int i=0;i< 5;i++) { r[i].getdata(); r[i].gettest(); r[i].getscore(); } c...
2nd example of hybrid: <ul><li>PROBLEM:? </li></ul><ul><li>Animals->Mammals </li></ul><ul><li>Animals>Reptiles </li></ul><...
SOLUTION OF IT: VIRTUAL <ul><li>class a </li></ul><ul><li>{ </li></ul><ul><li>}; </li></ul><ul><li>class b:public a </li><...
<ul><li>Class a </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Void show( ) </li></ul><ul><li>{ </li></ul...
Constructor and destructor in inheritance <ul><li>Constructor:  first  base  class then  derived  class constructor is cal...
Constructor and destructor in inheritance <ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><...
Destructor in inheritance <ul><li>Called in reverse order </li></ul>
<ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Base() </li></ul><ul><li>{ </li></ul><u...
Explicitly calling constructor <ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Base() <...
Constructor  and destructor in  multilevel
<ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Base() </li></ul><ul><li>{ </li></ul><u...
Constructor  and destructor in  multiple inheritance Constructor is called in the sequence of inheritance Class  a { }; Cl...
<ul><li>Class base1 </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Base() </li></ul><ul><li>{ </li></ul><...
<ul><li>Class base1 </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Base() </li></ul><ul><li>{ </li></ul><...
<ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Int x; </li></ul><ul><li>Public: </li></ul><ul><li>base(int a) </...
Initialize the values of data members from constructor  <ul><li>Class demo </li></ul><ul><li>{ </li></ul><ul><li>int x,y; ...
Containership <ul><li>If we create the object of one class into another class and that object will be a member of the clas...
<ul><li>Class upper </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Void display() </li></ul><ul><li>{ </l...
<ul><li>Container class also call upper class constructor first like inheritance.It can use only public member of upper cl...
<ul><li>Class upper </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>upper() </li></ul><ul><li>{ </li></ul>...
Upcoming SlideShare
Loading in...5
×

inhertance c++

4,729

Published on

It is nice ppt. It will be helpful for u.

Published in: Education
1 Comment
8 Likes
Statistics
Notes
  • good for showing private and public within a program. Accesable for beginning programmers to look at and follow.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
4,729
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
537
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide

inhertance c++

  1. 1. Inheritance It is the mechanism of deriving new class from existing class.It provides the idea of reusability. A B Base , super,parent class derived , sub, child class
  2. 2. Types of inheritance <ul><li>Single level </li></ul>B A Multilevel Multiple Hierarchical hybrid B A C A B C A B A C A D A E F A G H I J B A C B A B C C
  3. 3. <ul><li>Public members provides lowest security and it can be accessed in whole programme. </li></ul><ul><li>Private members are visible only to class.It provides the highest security. </li></ul><ul><li>Protected members has middle security and it is visible to the class and classes derived from class. It is not accessible in main </li></ul>
  4. 4. syntax <ul><li>Class classname: mode oldclassname </li></ul><ul><li>Mode: </li></ul><ul><li>Public : inherit all member as in base class mode(public to public and private to private). </li></ul><ul><li>Private : all members of base class become private to derived class. </li></ul><ul><li>Protected : all members of base class become protected to derived class. </li></ul>
  5. 5. <ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Private: </li></ul><ul><li>Int x; </li></ul><ul><li>Protected: </li></ul><ul><li>Int y; </li></ul><ul><li>Public: </li></ul><ul><li>Int z; </li></ul><ul><li>}; </li></ul><ul><li>Class der1:public base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Void get() </li></ul><ul><li>{ </li></ul><ul><li>Int o; </li></ul><ul><li>o=x; //error not accessible </li></ul><ul><li>o=y; // ok </li></ul><ul><li>o=z; //ok </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class der2:private base { Public: Void get() { Int p; p=x; //error not accessible p=y; // ok p=z; //ok } }; Void main() { Int M; der1 d1; M=d1.x; //error M=d1.y; //error M=d1.z; //ok der2 d2; M=d2.x; //error M=d2.y; // error M=d2.z; //error }
  6. 6. Single level inheritance <ul><li>#include<iostream.h> </li></ul><ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>display() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“It is base class”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class derived:public base </li></ul><ul><li>{ </li></ul><ul><li>}; </li></ul><ul><li>Void main() </li></ul><ul><li>{ </li></ul><ul><li>Derived d; </li></ul><ul><li>d.display(); </li></ul><ul><li>} </li></ul>
  7. 7. <ul><li>#include<iostream.h> </li></ul><ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Int x; </li></ul><ul><li>Public: </li></ul><ul><li>Get_base(int a) </li></ul><ul><li>{ </li></ul><ul><li>X=a; </li></ul><ul><li>} </li></ul><ul><li>Display_base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“x=“<<x; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class derived:public base { Int y; Public: Get_derived(int a) { y=a; } Display_derived() { Cout<<“y=“<<y; } }; Void main() { derived d; d.Get_base(10); d.Dispaly_base(); d.Get_derived(20); d.Dispaly_derived(); }
  8. 8. <ul><li>#include<iostream.h> </li></ul><ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>int x; </li></ul><ul><li>Public: </li></ul><ul><li>Get_base(int a) </li></ul><ul><li>{ </li></ul><ul><li>X=a; </li></ul><ul><li>} </li></ul><ul><li>Display_base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“x=“<<x; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class derived:public base { Int y; Public: Get_derived(int a) { Get_base(40); y=a; } Display_derived() { Display_base(); Cout<<“y=“<<y; } }; Void main() { derived d; d.Get_derived(10); d.Dispaly_derived(); }
  9. 9. Private inheritance <ul><li>#include<iostream.h> </li></ul><ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Display_base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“It is base class”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class derived:private base { }; Void main() { derived d; d.Dispaly_base(); } o/p ------ error
  10. 10. <ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Int x; </li></ul><ul><li>Public: </li></ul><ul><li>Get_base(int a) </li></ul><ul><li>{ </li></ul><ul><li>X=a; </li></ul><ul><li>} </li></ul><ul><li>Display_base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“x=“<<x; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class derived:private base { Int y; Public: Get_derived(int a) { Get_base(20); y=a; } Display_derived() { Display_base(); Cout<<“y=“<<y; } }; Void main() { derived d; d.Get_derived(10); d.Dispaly_derived(); }
  11. 11. Function overriding
  12. 12. <ul><li>#include<iostream.h> </li></ul><ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Int x; </li></ul><ul><li>Public: </li></ul><ul><li>Getdata(int a) </li></ul><ul><li>{ </li></ul><ul><li>X=a; </li></ul><ul><li>} </li></ul><ul><li>Display () </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“x=“<<x; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class derived:public base { Int y; Public: Getdata(int a) { y=a; } Display() { Cout<<“y=“<<y; } }; Void main() { derived d; d.Getdata(); d.Dispaly(); } o/p--- y =
  13. 13. <ul><li>#include<iostream.h> </li></ul><ul><li>Class area </li></ul><ul><li>{ </li></ul><ul><li>Protected: </li></ul><ul><li>Int length,width; </li></ul><ul><li>Public: </li></ul><ul><li>Void getdata() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“enter length and width”.; </li></ul><ul><li>Cin>>length>>width; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class volume :public area </li></ul><ul><li>{ </li></ul><ul><li>Int height; </li></ul><ul><li>Public: </li></ul><ul><li>Void get() </li></ul><ul><li>{ </li></ul><ul><li>Getdata(); </li></ul><ul><li>Cout<<“enter height”; </li></ul><ul><li>Cin>>height; </li></ul><ul><li>} </li></ul><ul><li>Void cal_volume() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“volume=“<<length*height*width; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>void main() { Volume v; v.get(); v.cal_volume(); }
  14. 14. <ul><li>#include<iostream.h> </li></ul><ul><li>Class area </li></ul><ul><li>{ </li></ul><ul><li>Protected: </li></ul><ul><li>Int length,width; </li></ul><ul><li>Public: </li></ul><ul><li>Void getdata() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“enter len and width”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class volume :public area </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Int height; </li></ul><ul><li>Void get() </li></ul><ul><li>{ </li></ul><ul><li>Getdata(); </li></ul><ul><li>Cout<<“enter height”; </li></ul><ul><li>Cin>>height; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>void main() { Volume v; v.get(); Int vol=length*height*width; Cout<<“volume=“<<vol; } o/p === error (protected member can be accessed in that class and in derived class not in main)
  15. 15. Multilevel inheritance <ul><li>#include<iostream.h> </li></ul><ul><li>Class first </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Display1() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“first class”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class second:public first </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Display2() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“second class”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class third:public second { Public: Display3() { Cout<<“third class”; } }; Void main() { third t; t.Display1(); t.Display2(); t.Display3(); }
  16. 16. Multiple inheritance <ul><li>#include<iostream.h> </li></ul><ul><li>Class super </li></ul><ul><li>{ </li></ul><ul><li>Protected: </li></ul><ul><li>int base; </li></ul><ul><li>Void getbase() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“enter base”; </li></ul><ul><li>Cin>>base; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class sub:public super </li></ul><ul><li>{ </li></ul><ul><li>Protected: </li></ul><ul><li>Int power: </li></ul><ul><li>Void getpower() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“enter power”; </li></ul><ul><li>Cin>>power; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class result:public super,public:sub { Public: Void show() { getbase(); getpower(); Int t=1; for(int i=1;i<=power;i++) { t=t*base; } Cout<<“base=“<<base; Cout<<“power=“<<power; Cout<<“output result=“<<t; } }; Void main() { Result t; t.show(); }
  17. 17. Hierarchical inheritance a b c d e f g h i j
  18. 18. <ul><li>#include<iostream.h> </li></ul><ul><li>#include<string.h> </li></ul><ul><li>Class university </li></ul><ul><li>{ </li></ul><ul><li>Protected: </li></ul><ul><li>Char uname[20]; </li></ul><ul><li>Public: </li></ul><ul><li>void getuniversity() </li></ul><ul><li>{ </li></ul><ul><li>Strcpy(uname,”RTU”); </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class college1: public university </li></ul><ul><li>{ </li></ul><ul><li>Protected: </li></ul><ul><li>Char college_name[20] </li></ul><ul><li>Public: </li></ul><ul><li>college1() </li></ul><ul><li>{ </li></ul><ul><li>Strcpy(college_name,”I.E.T”); </li></ul><ul><li>} </li></ul><ul><li>Void display1() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“college name=<<college_name; </li></ul><ul><li>Cout<<“university name=<<uname; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class college2: public university { Protected: Char college_name[20]; Public: college2() { Strcpy(college_name,”A.I.E.T”); } Void display2() { Cout<<“college name=<<college_name; Cout<<“university name=<<uname; } }; Void main() { University u; u.getuniversity(); College1 c1; C1.display1(); College2 c2; C2.display2(); }
  19. 19. Hybrid <ul><li>student </li></ul>Test sports result
  20. 20. <ul><li>class student { private : int rn; char na[20]; public: void getdata() { cout< < &quot;Enter Name And Roll No : &quot;; cin>>na>>rn; } void putdata() { cout< < endl< < na< < &quot; &quot;< < rn< < &quot; &quot;; } }; class test : public student { protected: float m1,m2; public: void gettest() { cout< < endl< < &quot;Enter your marks In CP 1 And Cp 2 :&quot;; cin>>m1>>m2; } void puttest() { cout< < m1< < &quot; &quot;< < m2< < &quot; &quot;; } }; </li></ul>class sports { protected: float score; public: void getscore() { cout< < endl< < &quot;Enter your score :&quot;; cin>>score; } void putscore() { cout< < score< < &quot; &quot;; } }; class results : public test , public sports { private : float total; public : void putresult() { total = m1+m2+score; cout< < total; } };
  21. 21. <ul><li>void main() { results r[5]; clrscr(); for(int i=0;i< 5;i++) { r[i].getdata(); r[i].gettest(); r[i].getscore(); } cout< < endl< < &quot;Name Rollno CP 1 CP 2 Score Total“; for(i=0;i< 5;i++) { r[i].putdata(); r[i].puttest(); r[i].putscore(); r[i].putresult(); } getch(); } </li></ul>
  22. 22. 2nd example of hybrid: <ul><li>PROBLEM:? </li></ul><ul><li>Animals->Mammals </li></ul><ul><li>Animals>Reptiles </li></ul><ul><li>Mammals , Reptiles ->Snakes </li></ul><ul><li>SO DUPLICATION FROM B AND C TO D CLASS SO IT CONFUSES THE COMPILER AND IT DISPLAYS ERROR </li></ul>Animals Mammals Reptiles Snakes
  23. 23. SOLUTION OF IT: VIRTUAL <ul><li>class a </li></ul><ul><li>{ </li></ul><ul><li>}; </li></ul><ul><li>class b:public a </li></ul><ul><li>{ }; </li></ul><ul><li>class c:public a </li></ul><ul><li>{ }; </li></ul><ul><li>class d: public b, public c </li></ul><ul><li>{ }; </li></ul>VIRTUAL BASE CLASS class a { }; class b:public virtual a { }; class c: virtual public a { }; class d: public b, public c { }; B and C share the same subobject of A. Using the keyword virtual in this example ensures that an object of class D inherits only one subobject of class A //ERROR
  24. 24. <ul><li>Class a </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Void show( ) </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“It is base class”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class b:virtual public a </li></ul><ul><li>{ }; </li></ul><ul><li>Class c:virtual public a </li></ul><ul><li>{ }; </li></ul><ul><li>Class d:public b,public c </li></ul><ul><li>{ }; </li></ul>Void main() { D obj; Obj.show(); } o/p: It is base class
  25. 25. Constructor and destructor in inheritance <ul><li>Constructor: first base class then derived class constructor is called. </li></ul><ul><li>Destructor: Reverse(first derived class then base class destructor is called). </li></ul>
  26. 26. Constructor and destructor in inheritance <ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is base class<<endl”; </li></ul><ul><li>} </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>derived() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is derived class”; </li></ul><ul><li>} </li></ul>Void main() { Derived d; } O/P it is base class it is derived class
  27. 27. Destructor in inheritance <ul><li>Called in reverse order </li></ul>
  28. 28. <ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is base class constructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>~Base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is base class destructor<<endl”; </li></ul><ul><li>} </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>derived() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is derived class constructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>~derived() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is derived class destructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Void main() { Derived d; } o/p : it is base class constructor it is derived class constructor it is derived class destructor it is base class destructor
  29. 29. Explicitly calling constructor <ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is base class constructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>~Base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is base class destructor<<endl”; </li></ul><ul><li>} </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>derived() :base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is derived class constructor<<endl”; </li></ul><ul><li>} </li></ul>~derived() { Cout<<“it is derived class destructor<<endl”; } }; Void main() { Derived d; } o/p : it is base class constructor it is derived class constructor it is derived class destructor it is base class destructor
  30. 30. Constructor and destructor in multilevel
  31. 31. <ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is base class constructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>~Base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is base class destructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class derived1:public base </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>derived1() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is derived1 class constructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>~derived1() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is derived1 class destructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class derived2:public derived1 { Public: derived() { Cout<<“it is derived2 class constructor <<endl”; } ~derived() { Cout<<“it is derived2 class destructor <<endl”; } }; Void main() { Derived2 obj; } o/p: it is base class constructor it is derived1 class constructor it is derived2 class constructor it is derived2 class destructor it is derived1 class destructor it is base class destructor
  32. 32. Constructor and destructor in multiple inheritance Constructor is called in the sequence of inheritance Class a { }; Class b { }; Class c: public b,public a { }; Then first constructor of b then of a is called
  33. 33. <ul><li>Class base1 </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is base1 class constructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class base2 </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>base2() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is base2 </li></ul><ul><li>class constructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class derived:public base2,public base1 { Public: derived() { Cout<<“it is derived class constructor <<endl”; } }; Void main() { Derived d; } o/p: it is base2 class constructor it is base1 class constructor it is derived class constructor
  34. 34. <ul><li>Class base1 </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Base() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is base1 class constructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class base2 </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>base2() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is base2 </li></ul><ul><li>class constructor<<endl”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Class derived:public base2, public base1 { Public: derived():base1(), base2() { Cout<<“it is derived class constructor <<endl”; } }; Void main() { Derived d; } o/p: it is base2 class constructor it is base1 class constructor it is derived class constructor Same output even done explicit calling
  35. 35. <ul><li>Class base </li></ul><ul><li>{ </li></ul><ul><li>Int x; </li></ul><ul><li>Public: </li></ul><ul><li>base(int a) </li></ul><ul><li>{ </li></ul><ul><li>X=a; </li></ul><ul><li>Cout<<“x=“<<x<<endl; </li></ul><ul><li>}}; </li></ul><ul><li>Class derived:public base </li></ul><ul><li>{ </li></ul><ul><li>int y; </li></ul><ul><li>Public: </li></ul><ul><li>derived(int l,int m): base(l) </li></ul><ul><li>{ </li></ul><ul><li>y=m; </li></ul><ul><li>Cout<<“y=“<<y; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Void main( ) { Derived d(20,30); } o/p X=20 Y=30
  36. 36. Initialize the values of data members from constructor <ul><li>Class demo </li></ul><ul><li>{ </li></ul><ul><li>int x,y; </li></ul><ul><li>Public: </li></ul><ul><li>demo(int l,int m): x(l),y(m) </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“x=“<<x<<endl<<“y=“<<y; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Void main() { Demo d; }
  37. 37. Containership <ul><li>If we create the object of one class into another class and that object will be a member of the class.then it is called containership. </li></ul><ul><li>This relation is calles has_a relation. </li></ul><ul><li>While the inheritance is called kind_of and is_a </li></ul><ul><li>Relation. </li></ul>
  38. 38. <ul><li>Class upper </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>Void display() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“hello”<<endl; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class lower </li></ul><ul><li>{ </li></ul><ul><li>Upper obj; </li></ul><ul><li>Public: </li></ul><ul><li>Lower() </li></ul><ul><li>{ </li></ul><ul><li>Obj.display(); </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Void main() { Lower l; } o/p hello
  39. 39. <ul><li>Container class also call upper class constructor first like inheritance.It can use only public member of upper class not the private and protected members </li></ul>
  40. 40. <ul><li>Class upper </li></ul><ul><li>{ </li></ul><ul><li>Public: </li></ul><ul><li>upper() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is upper class constructor </li></ul><ul><li><<endl”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>Class lower </li></ul><ul><li>{ </li></ul><ul><li>Upper obj; </li></ul><ul><li>Public: </li></ul><ul><li>lower() </li></ul><ul><li>{ </li></ul><ul><li>Cout<<“it is lower class constructor </li></ul><ul><li><<endl”; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Void main() { lower l; } o/p it is upper class constructor it is lower class constructor
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×