Вы находитесь на странице: 1из 63

OOPS

INHERITANCE

INHERITANCE

Reusability

Reusability is the process of reusing something that already exists rather than trying to create the same all over again Thi can be This b achieved hi d by b creating ti new classes l and d reusing the properties of the existing ones. The mechanism of deriving a new class from an old one is i called ll d inheritance. i h it Def: Inheritance is the process of deriving a new class (derived class) from an old one (base class). The new ne class is called the derived deri ed class and the old is referred to as the base class. The derived class may inherit some or all of the traits / tt ib t of /attributes f the th base b class. l

Types of Inheritance

Single inheritance A derived class having only one single base class
A.

B.

Multiple Inheritance

Hierarchical Inheritance

Multilevel Inheritance

Hybrid Inheritance

Defining a derived class S t Syntax:


1) 2) 3) 4) 5) 6)

Class derived_class_name derived class name : visibility mode base_class base class { ---------------------------------------------------------};

The colon indicates that the derived_class_name is derived from the base_class_name. The visibility mode may either be private or public .

Example:
1. 2. 3. 4 4. 5. 6. 7. 8. 9. 10. 11. 12 12.

Class ABC : p private XYZ //private p derivation { ------------}; class ABC : public XYZ // Public derivation { ------------}; Class ABC : XYZ //private p derivation by y default { --------------};

When a base class is privately inherited by a derived class, public members of the base class become private members of the derived class, thus public members of the base class can only be accessed by the member function of the derived class. They are inaccessible to the object of the derived class

If a base class is publicly inherited, public members of the base class become public members of the derived class and therefore they are accessible to the objects of the derived class. In both cases the private members are not inherited and therefore, the private members of a base class will never become members of its derived class. class

Single Inheritance
A.

A class can be inherited either by public or private derivation. Example: Public Derivation

Program Example to illustrate Single inheritance on public Derivation

1. 2. 3. 4. 5. 6. 7 7. 8. 9. 10.

# include<iostream.h> class c ss B { int a; //private not inheritable public: //public inheritance int b; void get_ab(); get ab(); int get_a(void); void show_a(void); };

11. 12. 13 13. 14. 15. 16 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29 29.

class D : public B // public derivation { int c; public: void mult(void); void display(void); }; void B :: get_ab(void) { a = 5; b = 10; } int B :: get_a() { return a; } void id B :: show_a() h () { cout<<"a="<<a<<"\n"; }

30. 31. 32 32. 33. 34. 35. 36. 37. 38. 39 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50.

void D :: mult() { c = b * get_a(); get a(); } void D :: display() { cout << "a =" <<get_a() <<"\n"; cout << "b =" << b << "\n"; cout << "c = " << c << "\n"; } main() { D d; d.get_ab(); d.mult(); d.show_a(); d.show a(); d.display(); d.b = 20; d.mult(); d di l () d.display(); }

Program Explanation

The class D is a public derivation of the base class B D inherits all the public members of B. A public member of the base class B is also a public member of the derived class D. The private member of B cannot be inherited by D. The class D will have more members since it its s a derived class, and thus has inherited the public members of class B.

1. 2 2. 3. 4. 5. 6. 7. 8.

Void show_a() { cout <<a= <<a <<\n; } void mult () { c = b * get_a(); // c = b * a } Taking a closer look at the functions show_a() and mul(), You can notice that even though the data member A is private i t in i B and d can not t be b inherited; i h it d Objects Obj t of f D are able to access it through an inherited member function of B

Private Derivation:

Class B { int a; public: int b; void id get_ab(); t b() void get_a(); void show_a(); }; class D : private B { int c; p blic public: void mul(); void display(); } };

//private derivation

In private derivation, the public members of the base class become private members of the derived class. Therefore the objects of D can not th have di direct t access t to th the public bli member b f functions ti of fB B.

Class D after the private derivation Class D Private section: C;

B Get_ab(); Get_a(); Show_a();

Example: Void mul() { get_ab(); c=b * get_a(); } void display() { show_a; cout <<b=<<b<<\n<<c=<<c<<\n; << b= <<b<< \n<< c= <<c<< \n ; }

Public Section Mult() display These statements will not work since these functions are private in the class D, and can not the accessed directly by the objects of class D. d.get_ab(); d.get_a(); d show a(); d.show_a(); These functions can be used inside the mul() and display() functions.

Program Example to illustrate private derivation


1. 2. 3. 4. 5. 6. 7.

#include #i l d <iostream.h> i t h class b { int a; public: p int b; void get_ab();

18. 19. 20. 21. 22. 23. 24. 25. 26. 27 27. 28. 29. 30. 31. 32. 33. 34. 35.

8. 9. 10. 11. 12. 13 13. 14. 15. 16. 17.

int get_a(void); void show_a(void); _ ( ); }; class D : private b { int c; public: void mul(void); void display(void); };

void b :: get_ab(void) get ab(void) { cout <<"Enter the values for a and b:"; cin >> a >> b; } int b :: get_a() { return a; } void b :: show_a() show a() { cout <<"a = " << a << "\n"; } void D :: mul() { get_ab(); c= b * get_a(); }

36. 37 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48 48. 49. 50. 51.

void D :: display() { show_a(); cout <<"b "<<b<<"c"<<c; } int main() { D d; // d.getab(); wont work d.mul(); //d.show_a() wont work d.display(); //d b = 20 wont work //d.b d.mul(); d.display(); }

Inheriting private members


There are t Th two ways of f making ki th the private i t member b of f th the base class inheritable By modifying the visibility limit of the private member by making it public

This will make it accessible to be accessed by all the other functions of the program, program this taking away the advantage of data hiding.

By using the visibility mode protected. A member b d declared l d as iis accessible ibl b by th the member b functions within its class and any class immediately derived from it. It can not be accessed by the functions outside these two classes.

Class alpha { private: protected:


//visible to member functions of its own and derived class

public: }

When a protected member is inherited in public mode it becomes protected in the derived class, mode, class and therefore is accessible by the member functions of the derived class, class and its also ready for further inheritance A protected t t d member b inherited i h it d in i private i t mode d derivation become private in the derived class. Alth h its Although it available il bl to t member b functions f ti of f the th derived class it is not available for further i h it inheritance ( i (since private i t members b can not t be b inherited)

The keywords private, protected and public may appear in any order and any number of times in the declaration of a class.

Class alpha { protected: t t d public: private: public: }

Its also possible to inherit the base class in protected t t d mode d protected t t d derivation. d i ti I protected In t t d derivation both the public and protected members of th base the b class l b become protected, t t d members b of f the th derived class. The private members are not i h it d inherited.

Multilevel Inheritance

The class A serves as the base class for the derived class B, which in turn serves as a base class for the derived class C. C The class B is known as the intermediate base class since it provides the link for the inheritance between A and C. The chain in know as inheritance path. A derived class with multilevel inheritance is declared as follows:follows: Class A{.}; //base class Class B: p public A { {..}; }; //B derived from A Class C: public B {..}; // C derived from B

Example: Assume that the test result of a batch of students are stored in three different classes. Class student stores the roll-number, class test stores the marks obtained in two subjects j and class result contain the total marks obtained in the test. The class result can inherit the details of the marks obtained in the test and the roll-number of students through multilevel inheritance. 1 1. Class Student 2. { 3. protected: 4. i roll_number; int ll b 5. public: 6. void get_number(int); 7. void put_number(void); 8. };

9. 10. 11. 12. 13 13. 14. 15. 16. 17. 18. 19. 20. 21 21. 22. 23. 24. 25.

void student :: get_number( int a) { roll_number = a; } void student :: put_number put number () { cout <<Roll Number:<<roll_number <<\n; } class test : public student // First level derivation { protected: float sub1; float sub2; public: void get_marks(flaot, float); void id put_marks(void); t k ( id) };

26. 27. 28. 29. 30. 31. 32. 33 33. 34. 35. 36. 37. 38. 39. 40. 41 41.

void test :: get_marks(float x, float y) { sub1 = x; sub2 = y; } void test :: put_marks() { cout<<Marks cout<< Marks in SUB1 = <<sub1<<\ <<sub1<< <<sub1<< \n n; ; cout <<Marks in SUB2 = <<sub2 <<\ <<\n; } class result : public test //Second derivation { float total; ; public: void display(void); };

The class result, after inheritance from grand father through father will contain the following members. members
1) 2) 3) 4) 5) 6) 7) 8) 9) 10) 11) 12)

Private: Float toal; Protected: Int roll_number; Float sub1; Float sub2; Public: V id get_number(int); Void t b (i t) Void put_number(void); Void get_marks(flaot, float); Void put_marks(void); Void display(void);

The inherited functions put_number() and put_marks() can be be used in the definition of display() function. V id result Void lt :: di display(void) l ( id) { total = sub1 + sub2; put_number(); put_marks(); cout<<Total cout<< Total = << total <<\n; << \n ; } The Main function for this program will be as follows:i t main() int i () { result student1; student1.get_number(111); student1.getmarks(75.0, 59.5); student.display(); return 0; }

include <iostream.h> class student { protected: int roll_number; roll number; public: void get_number(int); void put_number(void); put number(void); }; void student :: get_number( int a) { roll_number = a; } void student :: put_number () { cout <<"Roll << Roll Number:"<<roll Number: <<roll_number number << <<"\n"; \n ; }

class test : public student // First level derivation { protected: t t d float sub1; float sub2; public: bli void get_marks(float, float); void put_marks(void); }; void test :: get_marks(float x, float y) { sub1 = x; sub2 = y; } void test :: put_marks() { cout<<"Marks in SUB1 = " <<sub1<<"\n"; ; cout <<"Marks in SUB2 = " <<sub2 <<"\n"; }

class result : public test //Second derivation { fl t total; float t t l public: void display(void); }; void result :: display(void) { total = sub1 + sub2; put_number(); put_marks(); cout<<"Total = " << total <<"\n"; } int main() { result student1; student1.get_number(111); student1.get_marks(75.0, 59.5); st dent1 displa () student1.display(); return 0; }

It is also possible to inherit the base class in protected mode protected derivation. In protected derivation both the public and protected members of the base class become protected members of the derived class. The private members are not inherited.

Multiple Inheritance

Syntax of a derived class Class D: visibility B-1, B 1 visibility B B-2. 2 { . };


Th visibility The i ibili may be b either i h public bli or private, i protected d

Example
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16 16. 17. 18.

Class m { protected: int m; public: void get_m(int); } }; void M::get_m(int x) { m = x; ; } class N { protected: int n; public void get_n(int); };

29. 30 30. 31. 32. 33. 34. 35. 36. 37.

void N::get_n(int y) { n = y; } class p : public m, public n { public: void display(void); } };

The derived class P will contain all the members of m and n in addition of its own members. members Class p { protected: i t m; int int n; public:
void get_m(int); void get_n(int); void display(void); };

The member function display can be defined as:


void p :: display(void) { cout <<m = << m << \n; cout <<n = << n << \n; cout <<m << m * n = << m * n << \n; \n ; };

class n { protected: int n; public: void get_n(int); }; void n::get_n(int y) { n = y; } class p : public m, public n { public: p y( ); void display(void); };

1. #include<iostream.h> 1 #i l d <i t h> 2. class m 3 { 3. 4. protected: 5. int m; ; 6. public: 7. void get_m(int); (i ) 8. }; 9 void m::get 9. m::get_m(int m(int x) 10.{ 11. m = x; ; 12.}

13.class 13 l n 14.{ 15 15. protected: 16. int n; 17. p public: 18. void get_n(int); 19 } 19.}; 20.void n::get_n(int y) 21 { 21.{ 22. n = y; 23.} 3}

14.class p : public p m, public p n 15.{ 16. public: 17. void display(void); p y( ); 18.}; 19.void p :: display(void) 20.{ 21. cout <<"m =" << m << "\n"; 22. cout <<"n = " << n << "\n"; 23. cout <<"m << m * n = " << m * n << "\n"; \n ; 24.}; 25.main() 26 { 26.{ 27. p p; 28. p.get_m(10); 29 29. p get n(20); p.get_n(20); 30. p.display(); 31.}

Ambiguity Resolution in Inheritance


There are cases in which a function having g the same name may y appear pp in the two base classes.

Example: Class m { public: void display(void); { cout t <<class l m; } }; class n { public: void display(void) { cout <<class n; } };

In such a case which display is used by the derived class?. Thi can b This be solved l db by d defining fi i a named d iinstance t within ithi th the derived class, using the class resolution operator with the function. function Class p: public m, public n { public void id display(void) di l ( id) { m :: di display(); l () } };

Ambiguity may also arise in the single inheritance applications, example: Class A { public: void display() { cout <<A\n; } }; class B : public A { public: p y() void display() { cout <<B\n; } };

The function in the derived class overrides the inherited function therefore a simple call to display() by B type object function, will invoke function defined in B only. The function defined in A can be invoked by y using g the scope p resolution operator. p

Example:
Main() { B b; b.display(); b A::display(); b.A::display(); b.B::display(); }

Hierarchical Inheritance

A case in which certain features of one level are shared by many others below that level.

Hybrid Inheritance

AP Process in i which hi h we apply l two or more types of f inheritance to design a program.

Multipath Inheritance and Virtual base class


There are situations in which all the three kinds of inheritance are involved, multilevel, multiple and hierarchical inheritance.

The Child class is derived from the base classes parent1 and parent2 (multiple inheritance), inheritance) which themselves have a common base class grandparent (hierarchical inheritance) The child inherits the properties of the grandparent g p (hierarchical inheritance) ( ) via two separate paths as shown by the figure above. The classes p parent1 and p parent2 are referred to as direct base classes while the grandparent is referred to as indirect base class

Inheritance by the child might cause some problems All the public and protected problems. members of grandparent are inherited into the child twice, twice via parent 1 and parent 2. 2 Meaning the child might have two duplicate sets of the members inherited from grandparent. This can be avoided by making the common base class a virtual base class.

Syntax:

Class A 2 { 2. 3. :::::::::::::::::::: 4. }; 5 class B1 : Virtual public A 5. //parent 1 6. { 7. ::::::::::::::::::::::: 8. }; 9. class B2 : public virtual A //parent 2 10. { 11. ::::::::::::::::::::::: 12. }; 13. class c: public B1, public B2 //child 14. { 15. ::::::::::::::::::::::::: // only l one copy of fa 16. ::::::::::::::::::::::::: // will be inherited 17. };
1.

When a class is made virtual, c++ takes care to see to it that only one copy of that class is inherited inherited, regardless of how many inheritance paths exist.

Program to demonstrate the Virtual Base Class: 1. #include<iostream.h> 2. class student 3 { 3. 4. protected: 5. int roll_number; 6. public: 7. void get_number(int a) 8. { 9. roll_number = a; 10. } 11 11. void put_number(void) put number(void) 12. { 13. cout <<"Roll No:"<<roll_number<<"\n" ; 14. } 15.};

17.class test : virtual public student 18 { 18.{ 19. protected: 20. float part1, part2; 21. public: 22. void get_marks(float x, float y) 23 23. { 24. part1 = x; part2 = y; 25. } 26. void put_marks(void) 27. { 28 28. cout << "Marks Marks obtained: "<<"\n"; << \n ; 29. cout <<"Part1 = " << part1 <<"\n"; 30. cout<<"Part2 = "<< p part2 << "\n"; ; 31. } 32.};

33. 34 34. 35. 36. 37. 38. 39. 40. 41 41. 42. 43. 44. 45. 46.

class sports : public virtual student { protected: float score; public: void get_score(float s) { score = s; } void put_score(void) { cout <<"sports wt: " <<score <<"\n\n"; } };

47. 48 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60 60.

class result : public test, public sports { float total; public: void display(void); }; void id result lt :: di display(void) l ( id) { total = p part1 + p part2 + score; ; put_number(); put_marks(); put_score(); () cout <<"Total Score: " <<total << "\n"; }

61. 62. 63. 64. 65. 66. 67. 68.

main() { result student1; student1.get_number(678); student1.get_marks(30.5 , 25.5); student1.get_score(7.0); student1.display(); }

END

Вам также может понравиться