1 / 55

INHERITANCE

INHERITANCE. By: Er . Gurpreet Singh Assistant Professor Department of Information Technology, MIMIT Malout. Objective. On completion of this lecture, you will be able to: Explore the importance of Inheritance. Define derived classes. Explain Protected visibility mode.

Download Presentation

INHERITANCE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INHERITANCE By: Er. Gurpreet Singh Assistant Professor Department of Information Technology, MIMIT Malout

  2. Objective On completion of this lecture, you will be able to: • Explore the importance of Inheritance. • Define derived classes. • Explain Protected visibility mode. • Explain various types of Inheritance • Distinguish between overriding and overloading. • Create objects of derived classes. • Concept of constructors in the derived classes. • Declare an object of a class in another class.

  3. INTRODUCTION • Inheritance is a powerful feature of object-oriented programming. It is a process of creating a new class from existing class. • The class from which we create a new class is called the base class(Super class or Parent class) • The newly created class is known as Derived class also known as Sub Class or Child Class.

  4. INHERITANCE RELATIONSHIP Feature A Base Class Feature B Feature C Feature A Derived Class Feature B

  5. Defining Derived Class • Whenever a derived class is defined, we have to specify its relationship with the base class. The general form of specifying derived class: Class derived_class_name: visibility_modebase_class_name { ------ ------// members of derived class ------ }; • The colon(:) symbol shows the relationship of a derived class to its base class. The visibility mode may be private or public. The default mode is private.

  6. Public visibility mode Class d: public b { ---- ---- //members of derived class ---- }; In this example d is derived class & b is base class visibility mode is public means features of base class are publically derived or publically inherited to class d.

  7. Private visibility mode Class d: private b { ---- ---- //members of derived class ---- }; In this example d is derived class & b is base class visibility mode is private means features of base class are privately derived or privately inherited to class d.

  8. Remember • When base class is publically derived or publically inherited , each public member of the base class becomes public member in the derived class. The private members of the base class are not inherited. • When base class is privately derived or privately inherited , each public member of the base class becomes private member in the derived class. The private members of the base class are not inherited. • In protected derivation, both the public & protected members of the base class become protected members of the derived class. When a protected member is inherited in public mode, it becomes protected in derived & when protected member is inherited in private mode it becomes private in the derived class.

  9. Effect of Inheritance on the visibility of members. Class B Class D: private B Not Inheritable (A) Private derivation

  10. Effect of Inheritance on the visibility of members. Class B Class D: public B Not Inheritable (B) Public derivation

  11. Effect of Inheritance on the visibility of members. Class B Class D: protected B Not Inheritable (C) Protected derivation

  12. Review the access control to public, private & protected members of the class.

  13. TYPES OF INHERITANCE • Single Inheritance • Multiple Inheritance • Multilevel Inheritance • Hierarchical Inheritance • Hybrid Inheritance

  14. SINGLE INHERITANCE A Base Class B Derived Class One Base One Derived

  15. MULTIPLE INHERITANCE Base Class 1 Base Class 2 Base Class 3 A B C D Derived Class More Base One Derived

  16. MULTILEVEL INHERITANCE A Base Class 1 B Intermediate class C Derived Class Note: Intermediate class contains protected data members. Private will not work.

  17. HIERARCHICAL INHERITANCE A Base Class D3 D1 D2 Derived Class 1 Derived Class 2 Derived Class 3 One Base More Derived

  18. HYBRID INHERITANCE A Base Class 1 B C Intermediate class D Derived Class Combination of two or more types of inheritance. (Here combination of Multilevel & Multiple inheritance.)

  19. PRACTICAL SESSIONOFINHERITANCE

  20. SINGLE INHERITANCE private: char name[20]; intrno; public: void getstudent(); void displaystudent(); student private: char city[20]; public: void getaddress(); void displayaddress(); address

  21. #include<iostream.h> #include<conio.h> #include<stdio.h> class student { private: char name[20]; int rno; public: void getstudent() { cout<<”enter name of the student=”; cin>>name; cout<<”enter roll number of the student=”; cin>>rno; } void displaystudent() { cout<<”name of the student=”<<name; cout<<”\nroll number of the student=”<<rno; } };

  22. class address : public student { private: char city[20]; public: void getaddress() { getstudent(); cout<<”\nenter city=”; cin>>city; } void displayaddress() { displaystudent(); cout<<”\ncity=”<<city; } };

  23. void main() { class address a1; clrscr(); a1.getaddress(); clrscr(); a1.displayaddress(); getch(); }

  24. MULTIPLE INHERITANCE private: char name[20]; intrno; public: void getstudent(); void displaystudent(); private: char city[20]; public: void getaddress(); void displayaddress(); student address private: Inttfee,submit,balance; public: void getaccount(); void displayaccount(); account

  25. #include<iostream.h> #include<conio.h> #include<stdio.h> class student { private: char name[20]; int rno; public: void getstudent() { cout<<”enter name of the student=”; cin>>name; cout<<”enter roll number of the student=”; cin>>rno; } void displaystudent() { cout<<”name of the student=”<<name; cout<<”\nroll number of the student=”<<rno; } };

  26. class address { private: char city[20]; public: void getaddress() { cout<<”\nenter city=”; cin>>city; } void displayaddress() { cout<<”\ncity=”<<city; } };

  27. class account: public student, public address { private: int tfee,submit,balance; public: void getaccount() { getstudent(); getaddress(); cout<<“\nenter total fee=“; cin>>tfee; cout<<“\nenter submit fee=“ ; cin>>submit; }

  28. void displayaccount() { displaystudent(); displayaddress(); cout<<”\ntotal fee=”<<tfee; cout<<”\nsubmit fee=”<<submit; balance=tfee-submit; cout<<“\nbalance fee=“<<balance; } };

  29. void main() { class account a1; clrscr(); a1.getaccount(); clrscr(); a1.displayaccount(); getch(); }

  30. MULTILEVEL INHERITANCE class student { private: char name[20]; intrno; public: void getstudent() void displaystudent() student Base Class 1 class test: public student { protected: intmath,eng,sci; public: void gettest() void displaytest() test Intermediate class class result: public test { private: inttotal,avg; public: void getresult() void displayresult() Derived Class result Note: Intermediate class contains protected data members. Private will not work. 30

  31.  #include<iostream.h> #include<conio.h> #include<stdio.h> class student { private: char name[20]; int rno; public: void getstudent() { cout<<”enter name of the student=”; cin>>name; cout<<”enter roll number of the student=”; cin>>rno; } void getstudent() { cout<<”name of the student=”<<name; cout<<”\nroll number of the student=”<<rno; } };

  32. class test: public student { protected: int math,eng,sci; public: void gettest() { getstudent(); cout<<”enter math marks=”; cin>>math; cout<<”enter english marks=”; cin>>eng; cout<<”enter science marks=”; cin>>sci; } void displaytest() { displaystudent(); cout<<”\n math marks=”<<math; cout<<”\n english marks=”<<eng; cout<<”\nscience marks=”<<sci; } };

  33. class result : public test { private: int total,avg; public: void getresult() { gettest(); total=math+eng+sci; avg=total/3; } void displayaddress() { displaytest(); cout<<”\nTotal Marks=”<<total; cout<<”\n Average marks=”<<avg; } }; void main() { class result r1; clrscr(); r1.getresult(); clrscr(); r1.displayresult(); getch(); }

  34. HIERARCHICAL INHERITANCE One Base More Derived private: char name[20]; intrno; public: void getstudent(); void displaystudent(); student Base Class Derived Class 1 Derived Class 2 bsc ba class ba: public student { private: inthindi,punjabi; public: void getba() void displayba() class bsc: public student { private: intphy,chem,math; public: void getbsc() void displaybsc()

  35. #include<iostream.h> #include<conio.h> #include<stdio.h> class student { private: char name[20]; int rno; public: void getstudent() { cout<<”enter name of the student=”; cin>>name; cout<<”enter roll number of the student=”; cin>>rno; } void getstudent() { cout<<”name of the student=”<<name; cout<<”\nroll number of the student=”<<rno; } };

  36. class bsc: public student { private: int phy,chem,math; public: void getbsc() { getstudent(); cout<<”enter math marks=”; cin>>math; cout<<”enter physics marks=”; cin>>phy; cout<<”enter chemistry marks=”; cin>>chem; } void displaybsc() { displaystudent(); cout<<”\n math marks=”<<math; cout<<”\n physics marks=”<<phy; cout<<”\n chemistry marks=”<<chem; } };

  37. class ba: public student { private: int hindi,punjabi; public: void getba() { getstudent(); cout<<”enter hindi marks=”; cin>>hindi; cout<<”enter punjabi marks=”; cin>>punjabi; } void displayba() { displaystudent(); cout<<”\n hindi marks=”<<hindi; cout<<”\n punjabi marks=”<<punjabi; } };

  38. void main() { class bsc b1; class ba b2; int choice; clrscr(); cout<<”1. bsc 2. ba \n enter your stream=”; cin>>choice; if(choice==1) { b1.getbsc(); clrscr(); b1.displaybsc(); } else { b2.getba(); clrscr(); b2.displayba(); } getch(); }

  39. HYBRID INHERITANCE A Base Class 1 B C Intermediate class D Derived Class Combination of two or more types of inheritance. (Here combination of Multilevel & Multiple inheritance.)

  40. HYBRID INHERITANCE class student { private: char name[20]; intrno; public: void getstudent() void displaystudent() class address { private: char city[20]; public: void getaddress(); void displayaddress(); student class test: public student { protected: intmath,eng,sci; public: void gettest() void displaytest() test address class result: public test, public address { private: inttotal,avg; public: void getresult() void displayresult() result

  41. SOME IMPORTANT TERMS RELATED WITH INHERITANCE Abstract class: A class which is not used to create objects is called abstract class. For exp student class is abstract class which is only used as a base class no object created. Constructors & Destructor based inheritance: if both base class derived class have the default constructors, then the object of derived class first invokes base class constructor then the derived class constructor. The destructors are executed in reverse order i.e. the destructor of derived class is executed before the destructor of Its base class.

  42. OVERRIDING VS OVERLOADING With overloading many function of the same name with different signature are created. With overriding, the function in the derived class has the identical signature to the function in the base class. With overriding, a derived class implements its own version of a base class function. The derived class can selectively use some base class function as they are, and override others.

  43. OVERRIDING VS OVERLOADING Overloading..1.Same name but there are different definitions and parameters..2.Here, the definitions are extended.3.Seperate methods share the same name. 4.It is mainly for operators.5.It must have different method signatures.Overriding.1.Here replacement of methods.2.It is used in inheritance.3.subclass methods replaces the super class.4.It is mainly for functions.5.It must have same signature

  44. AMBIGUITY (A Problem) class A { public : void display() { cout<<”Class A \n”; }}; class B { public : void display() { cout <<”class B \n”;} };

  45. class D : public A, public B { void display() { display(); //Ambiguity, which display() function is used. } } In this case, an ambiguity arises. which display() function is used by the derived class when we inherit both classes A and B. We can solve this problem by specifying a class name with the scope resolution operation as shown below class D : public A, public B { public : void display() //override display() of A and B { B::display(); } };

  46. CONTAINERSHIP OR NESTING OF CLASS

  47. CONTAINERSHIP OR NESTING OF CLASSES A class definition can contains objects of another class. This kind of relationship is called Containership or nesting of classes. Inheritance and nesting of classes can serve the same purpose in some cases. Our new program shows how to use nesting how to use nesting of classes to get the similar output.

  48. #include<iostream.h> #include<iomanip.h> #include<conio.h> //Base class class student { int roll; char name[25]; public: void getstudent() { cout<<“Enter roll number“<<endl; cin>>roll; cout<<“Enter name of student”<<endl; cin>>name; } void displaystudent() { cout<<“Roll No : “<<roll <<endl; cout<<“Name :”<<name<<endl; } };

  49. //Derived class • class test • { • Int sub1,sub2; • student st; //containership • public: • void gettest(); • void displaytest(); • };

  50. void test :: gettest() { st.getstudent(); //student class function cout<<“Enter the marks of subject-1”<<endl; cin>>sub1; cout<<“Enter the marks of subject-2”<<endl; cin>>sub2; }

More Related