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

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

Transcript of INHERITANCE By: Er. Gurpreet Singh Assistant Professor Department of Information Technology,...

INHERITANCE By: Er. Gurpreet SinghBy: Er. Gurpreet Singh

Assistant ProfessorAssistant Professor Department of Information Technology, Department of Information Technology,

MIMIT MaloutMIMIT Malout

1

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.

2

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.

3

Feature A

Feature A

Feature B

Feature C

Feature B

Base Class

Derived Class

INHERITANCE RELATIONSHIP

4

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_mode base_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. 5

Public visibility modeClass 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. 6

Private visibility modeClass 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. 7

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.

8

Effect of Inheritance on the visibility of members.

Private

Protected

Public

Private

Protected

Public

Not Inheritable

Class D: private BClass B

9

Effect of Inheritance on the visibility of members.

Private

Protected

Public

Private

Protected

Public

Not Inheritable

Class D: public BClass B

10

Effect of Inheritance on the visibility of members.

Private

Protected

Public

Private

Protected

Public

Not Inheritable

Class D: protected BClass B

11

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

Access specifier

Accessible from own class

Accessible from derived

class

Accessible from objects outside class

Public Yes Yes Yes Protected Yes Yes No

Private Yes No No

12

TYPES OF INHERITANCE

1. Single Inheritance 2. Multiple Inheritance3. Multilevel Inheritance4. Hierarchical Inheritance5. Hybrid Inheritance

13

SINGLE INHERITANCE

Base Class

Derived Class

One Base One Derived 14

MULTIPLE INHERITANCEBase Class 1

Derived Class

Base Class 3Base Class 2

More Base One Derived 15

MULTILEVEL INHERITANCE

Base Class 1

Derived Class

Note: Intermediate class contains protected data members. Private will not work.

Intermediate class

16

HIERARCHICAL INHERITANCE

Base Class

Derived Class 3

One Base More Derived

Derived Class 2Derived Class 1

17

HYBRID INHERITANCE

Combination of two or more types of inheritance.(Here combination of Multilevel & Multiple inheritance.)

Base Class 1

Derived Class

Intermediate class

18

PRACTICAL SESSIONOF

INHERITANCE

student

address

private:char name[20];int rno;

public:void getstudent();void displaystudent();

private:char city[20];

public:void getaddress();void displayaddress();

SINGLE INHERITANCE

#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;}};

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

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

student address

private:char name[20];int rno;public:void getstudent();void displaystudent();

private:char city[20];public:void getaddress();void displayaddress();

MULTIPLE INHERITANCE

account

private:Int tfee,submit,balance;public:void getaccount();void displayaccount();

#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;}};

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

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;}

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

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

Note: Intermediate class contains protected data members. Private will not work.

class result: public test{private:int total,avg;public:void getresult()void displayresult()

30

MULTILEVEL INHERITANCE

Base Class 1

Derived Class

Intermediate class

30

class student{private: char name[20];int rno;public:void getstudent()void displaystudent()

class test: public student{protected: int math,eng,sci;public:void gettest()void displaytest()

 #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;}};

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;}};

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();}

student Base Class

One Base More Derived

Derived Class 2Derived Class 1

34

private:char name[20];int rno;public:void getstudent();void displaystudent();

class bsc: public student{private:int phy,chem,math;public:void getbsc()void displaybsc()

HIERARCHICAL INHERITANCE

class ba: public student{private: int hindi,punjabi;public:void getba()void displayba()

#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;}};

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;}};

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;}};

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();}

HYBRID INHERITANCE

Combination of two or more types of inheritance.(Here combination of Multilevel & Multiple inheritance.)

Base Class 1

Derived Class

Intermediate class

39

40

HYBRID INHERITANCEclass student{private: char name[20];int rno;public:void getstudent()void displaystudent()

class test: public student{protected: int math,eng,sci;public:void gettest()void displaytest()

class result: public test, public address{private:int total,avg;public:void getresult()void displayresult()

class address{private:char city[20];public:void getaddress();void displayaddress();

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.

SOME IMPORTANT TERMS RELATED WITH INHERITANCE

Constructors & Destructor based inheritance:

if both base class derived class have the default constructors, then the object of derived class first invokes base class constructorthen the derived class constructor.

The destructors are executed in reverse order i.e. the destructorof derived class is executed before the destructor of Its base class.

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.

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

OVERRIDING VS OVERLOADING

class A{public :void display(){ cout<<”Class A \n”;}};

class B{ public :void display(){ cout <<”class B \n”;}};

AMBIGUITY (A Problem)

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();}};

  

CONTAINERSHIP OR

NESTING OF CLASS

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.

CONTAINERSHIP OR NESTING OF CLASSES

#include<iostream.h>#include<iomanip.h>#include<conio.h>//Base classclass 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; }};

//Derived class

class test{Int sub1,sub2;student st; //containershippublic:void gettest();void displaytest();};

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

void test :: displaytest(){st.displaystudent(); //student class functioncout<<“Marks of Subject-1 : “<<sub1<<endl;cout<<“Marks of Subject-1 : “<<sub1<<endl;}

void main(){test x; //object of test class createdclrscr();x.gettest();x.displaytest();getch();}

Output

Enter roll number 501Enter name of student sony Enter the marks of subject-1 89Enter the marks of subject-2 87

Roll No : 501Name : sonyMarks of subject -1 : 89Marks of subject -2 : 87

We see that the output st is created in the class test . The member functions of student are accessed through the object st. No class is inherited from another class.

SOME IMPORTANT QUESTIONS RELATED TO INHERITANCE FOR EXAMS.

Q1. Explain Inheritance & its types.

Q2. Differentiate Overloading & Overriding.

Q3. What is ambiguity how this problem can be solve?

Q4. What is abstract class?

Q5. Is nested class equal to inherited class?

Q6. Explain visibility modes & access specifications of inheritance.

THANKS