Top Banner
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
55

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

Dec 18, 2015

Download

Documents

Lawrence Wood
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: INHERITANCE By: Er. Gurpreet Singh Assistant Professor Department of Information Technology, Department of Information Technology, MIMIT Malout 1.

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

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

MIMIT MaloutMIMIT Malout

1

Page 2: INHERITANCE By: Er. Gurpreet Singh Assistant Professor Department of Information Technology, Department of Information Technology, MIMIT 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

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

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

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

Feature A

Feature A

Feature B

Feature C

Feature B

Base Class

Derived Class

INHERITANCE RELATIONSHIP

4

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

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

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

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

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

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

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

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

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

Effect of Inheritance on the visibility of members.

Private

Protected

Public

Private

Protected

Public

Not Inheritable

Class D: private BClass B

9

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

Effect of Inheritance on the visibility of members.

Private

Protected

Public

Private

Protected

Public

Not Inheritable

Class D: public BClass B

10

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

Effect of Inheritance on the visibility of members.

Private

Protected

Public

Private

Protected

Public

Not Inheritable

Class D: protected BClass B

11

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

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

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

TYPES OF INHERITANCE

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

13

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

SINGLE INHERITANCE

Base Class

Derived Class

One Base One Derived 14

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

MULTIPLE INHERITANCEBase Class 1

Derived Class

Base Class 3Base Class 2

More Base One Derived 15

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

MULTILEVEL INHERITANCE

Base Class 1

Derived Class

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

Intermediate class

16

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

HIERARCHICAL INHERITANCE

Base Class

Derived Class 3

One Base More Derived

Derived Class 2Derived Class 1

17

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

HYBRID INHERITANCE

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

Base Class 1

Derived Class

Intermediate class

18

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

PRACTICAL SESSIONOF

INHERITANCE

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

student

address

private:char name[20];int rno;

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

private:char city[20];

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

SINGLE INHERITANCE

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

HYBRID INHERITANCE

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

Base Class 1

Derived Class

Intermediate class

39

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

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

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

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.

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

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

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

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

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

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

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

AMBIGUITY (A Problem)

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

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

  

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

CONTAINERSHIP OR

NESTING OF CLASS

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

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

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

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

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

//Derived class

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

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

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

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

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

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

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

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

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.

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

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.

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

THANKS