inheritance
Download
Skip this Video
Download Presentation
INHERITANCE

Loading in 2 Seconds...

play fullscreen
1 / 55

INHERITANCE - PowerPoint PPT Presentation


  • 47 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.
slide4
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
  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance
single inheritance
SINGLE INHERITANCE

A

Base Class

B

Derived Class

One Base One Derived

multiple inheritance
MULTIPLE INHERITANCE

Base Class 1

Base Class 2

Base Class 3

A

B

C

D

Derived Class

More Base One Derived

multilevel inheritance
MULTILEVEL INHERITANCE

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

A

Base Class

D3

D1

D2

Derived Class 1

Derived Class 2

Derived Class 3

One Base More Derived

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

slide20
SINGLE INHERITANCE

private:

char name[20];

intrno;

public:

void getstudent();

void displaystudent();

student

private:

char city[20];

public:

void getaddress();

void displayaddress();

address

slide21
#include

#include

#include

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=”<

cout<<”\nroll number of the student=”<

}

};

slide22
class address : public student

{

private:

char city[20];

public:

void getaddress()

{

getstudent();

cout<<”\nenter city=”;

cin>>city;

}

void displayaddress()

{

displaystudent();

cout<<”\ncity=”<

}

};

slide23
void main()

{

class address a1;

clrscr();

a1.getaddress();

clrscr();

a1.displayaddress();

getch();

}

slide24
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

slide25
#include

#include

#include

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=”<

cout<<”\nroll number of the student=”<

}

};

slide26
class address

{

private:

char city[20];

public:

void getaddress()

{

cout<<”\nenter city=”;

cin>>city;

}

void displayaddress()

{

cout<<”\ncity=”<

}

};

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

}

slide28
void displayaccount()

{

displaystudent();

displayaddress();

cout<<”\ntotal fee=”<

cout<<”\nsubmit fee=”<

balance=tfee-submit;

cout<<“\nbalance fee=“<

}

};

slide29
void main()

{

class account a1;

clrscr();

a1.getaccount();

clrscr();

a1.displayaccount();

getch();

}

multilevel inheritance1
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

slide31
 #include

#include

#include

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=”<

cout<<”\nroll number of the student=”<

}

};

slide32
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=”<

cout<<”\n english marks=”<

cout<<”\nscience marks=”<

}

};

slide33
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=”<

cout<<”\n Average marks=”<

}

};

void main()

{

class result r1;

clrscr();

r1.getresult();

clrscr();

r1.displayresult();

getch();

}

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

slide35
#include

#include

#include

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=”<

cout<<”\nroll number of the student=”<

}

};

slide36
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=”<

cout<<”\n physics marks=”<

cout<<”\n chemistry marks=”<

}

};

slide37
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=”<

cout<<”\n punjabi marks=”<

}

};

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

hybrid inheritance2
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

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

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

slide43
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

slide44
AMBIGUITY (A Problem)

class A

{

public :

void display()

{

cout<<”Class A \n”;

}};

class B

{

public :

void display()

{

cout <<”class B \n”;}

};

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

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

slide48
#include

#include

#include

//Base class

class student

{

int roll;

char name[25];

public:

void getstudent()

{

cout<<“Enter roll number“<

cin>>roll;

cout<<“Enter name of student”<

cin>>name;

}

void displaystudent()

{

cout<<“Roll No : “<

cout<<“Name :”<

}

};

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

{

st.getstudent(); //student class function

cout<<“Enter the marks of subject-1”<

cin>>sub1;

cout<<“Enter the marks of subject-2”<

cin>>sub2;

}

slide51
void test :: displaytest()

{

st.displaystudent(); //student class function

cout<<“Marks of Subject-1 : “<

cout<<“Marks of Subject-1 : “<

}

slide52
void main()

{

test x; //object of test class created

clrscr();

x.gettest();

x.displaytest();

getch();

}

slide53
Output

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.

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

ad