Inheritance
Download
1 / 55

INHERITANCE - PowerPoint PPT Presentation


  • 46 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' INHERITANCE ' - hillary-french


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Inheritance

INHERITANCE

By: Er. Gurpreet Singh

Assistant Professor

Department of Information Technology,

MIMIT Malout


Objective
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.


Introduction
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.


INHERITANCE RELATIONSHIP

Feature A

Base Class

Feature B

Feature C

Feature A

Derived Class

Feature B


Defining derived class
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.


Public visibility mode
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.


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


Remember
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.


Effect of inheritance on the visibility of members
Effect of Inheritance on the visibility of members.

Class B

Class D: private B

Not Inheritable

(A) Private derivation


Effect of inheritance on the visibility of members1
Effect of Inheritance on the visibility of members.

Class B

Class D: public B

Not Inheritable

(B) Public derivation


Effect of inheritance on the visibility of members2
Effect of Inheritance on the visibility of members.

Class B

Class D: protected B

Not Inheritable

(C) Protected derivation



Types of inheritance
TYPES OF INHERITANCE members of the class.

  • Single Inheritance

  • Multiple Inheritance

  • Multilevel Inheritance

  • Hierarchical Inheritance

  • Hybrid Inheritance


Single inheritance
SINGLE INHERITANCE members of the class.

A

Base Class

B

Derived Class

One Base One Derived


Multiple inheritance
MULTIPLE INHERITANCE members of the class.

Base Class 1

Base Class 2

Base Class 3

A

B

C

D

Derived Class

More Base One Derived


Multilevel inheritance
MULTILEVEL INHERITANCE members of the class.

A

Base Class 1

B

Intermediate class

C

Derived Class

Note: Intermediate class contains protected data members.

Private will not work.


Hierarchical inheritance
HIERARCHICAL INHERITANCE members of the class.

A

Base Class

D3

D1

D2

Derived Class 1

Derived Class 2

Derived Class 3

One Base More Derived


Hybrid inheritance
HYBRID INHERITANCE members of the class.

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


Practical session of inheritance
PRACTICAL SESSION members of the class.OFINHERITANCE


SINGLE INHERITANCE members of the class.

private:

char name[20];

intrno;

public:

void getstudent();

void displaystudent();

student

private:

char city[20];

public:

void getaddress();

void displayaddress();

address


#include<iostream.h> members of the class.

#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 members of the class.

{

private:

char city[20];

public:

void getaddress()

{

getstudent();

cout<<”\nenter city=”;

cin>>city;

}

void displayaddress()

{

displaystudent();

cout<<”\ncity=”<<city;

}

};


void main() members of the class.

{

class address a1;

clrscr();

a1.getaddress();

clrscr();

a1.displayaddress();

getch();

}


MULTIPLE INHERITANCE members of the class.

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


#include<iostream.h> members of the class.

#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 members of the class.

{

private:

char city[20];

public:

void getaddress()

{

cout<<”\nenter city=”;

cin>>city;

}

void displayaddress()

{

cout<<”\ncity=”<<city;

}

};


class account: public student, public address members of the class.

{

private:

int tfee,submit,balance;

public:

void getaccount()

{

getstudent();

getaddress();

cout<<“\nenter total fee=“;

cin>>tfee;

cout<<“\nenter submit fee=“ ;

cin>>submit;

}


void displayaccount() members of the class.

{

displaystudent();

displayaddress();

cout<<”\ntotal fee=”<<tfee;

cout<<”\nsubmit fee=”<<submit;

balance=tfee-submit;

cout<<“\nbalance fee=“<<balance;

}

};


void main() members of the class.

{

class account a1;

clrscr();

a1.getaccount();

clrscr();

a1.displayaccount();

getch();

}


Multilevel inheritance1
MULTILEVEL INHERITANCE members of the class.

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


 #include<iostream.h> members of the class.

#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 members of the class.

{

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 members of the class.

{

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

}


Hierarchical inheritance1
HIERARCHICAL INHERITANCE members of the class.

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


#include<iostream.h> members of the class.

#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 members of the class.

{

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 members of the class.

{

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() members of the class.

{

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 inheritance1
HYBRID INHERITANCE members of the class.

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


Hybrid inheritance2
HYBRID INHERITANCE members of the class.

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


SOME IMPORTANT TERMS RELATED WITH INHERITANCE members of the class.

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.


OVERRIDING VS OVERLOADING members of the 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 members of the class.

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


AMBIGUITY (A Problem) members of the class.

class A

{

public :

void display()

{

cout<<”Class A \n”;

}};

class B

{

public :

void display()

{

cout <<”class B \n”;}

};


class D : public A, public B members of the class.

{

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

CONTAINERSHIP members of the class.OR NESTING OF CLASS


CONTAINERSHIP OR NESTING OF CLASSES members of the 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.


#include<iostream.h> members of the class.

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

}

};


  • //Derived class members of the class.

  • class test

    • {

    • Int sub1,sub2;

    • student st; //containership

    • public:

    • void gettest();

    • void displaytest();

    • };


void test :: gettest() members of the class.

{

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;

}


void test :: displaytest() members of the class.

{

st.displaystudent(); //student class function

cout<<“Marks of Subject-1 : “<<sub1<<endl;

cout<<“Marks of Subject-1 : “<<sub1<<endl;

}


void main() members of the class.

{

test x; //object of test class created

clrscr();

x.gettest();

x.displaytest();

getch();

}


Output members of the class.

Enter roll number 501

Enter name of student sony

Enter the marks of subject-1 89

Enter the marks of subject-2 87

Roll No : 501

Name : sony

Marks of subject -1 : 89

Marks 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 members of the class.

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 members of the class.


ad