SlideShare ist ein Scribd-Unternehmen logo
1 von 41
A.SANGEETHA
M.sc(Info Tech)
DEPARTMENT OF CS&IT
NADAR SARASWATHI COLLEGE OF ARTS
AND SCIENCE,THENI

 Reusability is another important feature of oop
. It is nice if we could reuse something that
already exists rather than trying to create the
same all over again.
 The mechanism of deriving a new class from an
old one is called inheritance.
 The old class is referred to as the base class and
the new one is called the derived class or sub
class.
INTRODUCTION

 In inheritance, some of the base class data element and
member function are “inherited” into the derived class.
Base class
Derived class

Single inheritance
Multiple inheritance
Multilevel inheritance
Hierarchical inheritance
Hybrid inheritance
TYPES OF INHERITANCE:

In this inheritance , a derived class is created from
a single base class. The class which inherits the
properties of another class is called derived or
child or subclass and the class whose properties
are inherited is called base or parent or super
class .
When a single class is derived from a single
parent class, it is called single inhertiance.
Single Inheritance:

Class name
{
…………..
………… ..
};
Class age : public name
{
……….
……….
};
SYNTAX FOR SINGLE INHERITANCE:

One base class with one derived class is
called single inheritance.
Parent
class
Child
class

#include<iostream.h>
#include<conio.h>
Using namespace std;
Class staff
{
private:
Void getdata();
Void display();
};
Class typist:public staff
{
Program for single inheritance:

Private:
int speed;
Public:
Void getdata();
Void display();
};
Void staff::getdata()
{
Cout<<“Name”;
Cin>>code;
}
Void staff::display()
{

Cout<<“Name:”<<name<<endln;;
Cout<<“code:”<<code<<endln;
}
Void typist::getdata()
{
Cout<<“speed:”;
Cin>>speed;
}
Void typist::display()
{

Cout<<“speed:”<<speed<<endln;
}
Int main()
{
Typist t;
Cout<<“enter data”<<endln;
t.staff::getdata();
t.getdata();
Cout<<endln<<“display data”<<endln;
t.staff::display();
t.display();
Getch();
Return 0;
}

 A class can inherit the attributes of two or more classes .
This is known as multiple inheritance.
 Multiple inheritance allows us to combine the features
of several existing classes as a starting point for
defining new classes.
 It like a child inheriting the physical features of one
parent and the intelligence of another.
 A class can be derived from more than one parent.
Multiple inheritance:

#include<iostream.h>
Using namespace std;
Class mammal{
Public:
Mammal()
{
Cout<<“mammals can give direct birth.”<<endln;
}
};
Class winged animal{
Public:
Program For Multiple Inheritance:

Winged animal()
{
Cout<<“winged animal can flap.”<<endln;
}
};
Class bat:public mammal,public wingedanimal{
};
Int main()
{
Bat b1;
Return 0;
}

 The class A serves as a base lass for thee
derived class B , which in turns serves as a base
class for the derived class C. The class B is
known as intermediate base class.
 In c ++ programming, not only you can derive
a class from the base class but you can also
derive a class from the derived class. This form
of inheritance is known as multilevel
inheritance.
Multilevel Inheritance:

Class A
{
……..
};
Class B: public A
{
………
};
Class C:public A
{
…….
};
Syntax For Multilevel Inheritance:

For Example:
A
B
C

#include <iostream>
using namespace std;
class A
{
public:
void display()
{
cout<<"Base class content.";
}
};
class B :
public A
{
};
Program For Multilevel Inheritance:

class B :
public A
{
}
;
class C :
public B
{
};
int main()
{
C obj;
obj.display();
return 0;
}

 Hybrid inheritance is the combination of two or
more inheritance.
 When you have a hybrid inheritance then a
Diamond problem may arise. In this problem a
Derived class will have multiple paths to a Base
class. This will result in duplicate inherited
members of the Base class
HYBRID INHERITANCE:

Class sports
{
Protected:
Float score;
Public:
Void get_score(float);
Void put_score(void);
};
SYNTAX FOR HYBRID INHERITANCE:

FOR EXAMPLE:
Base class
Derived
class 1
Derived
class 2
Derived
class 3

#include<iostream>
using namespace std;
int a,b,c,d,e;
class A
{
protected:
Program for hybrid inheritance:

public:
void getab()
{
cout<<"nEnter a and b value:";
cin>>a>>b;
}
};

class B:
public A
{
protected:
public:
void get c()

{
cout<<"Enter c value:";
cin>>c;
}
};
class C

{
protected:
public:
void getd()
{
cout<<"Enter d value:";
cin>>d;
}
};

class D:
public B,
public C
{
protected:
public:
void result()
{
getab(); getc();
getd(); e=a+b+c+d;
cout<<"n Addition is :"<<e;
}

};
int main()
{
D d1;
d1.result();
return 0;
}

 Hierarchical Inheritance is used as a support to a
hierarchical design of a class program.
 Many programming problems can be cast into a hierarchy
where certain features of one level are shared by many
others below that level.
 The base class includes all the features that are common to
the sub class. A sub class can be constructed by inheriting
the features of base class and so on.
Hierarchical inheritance:

class base_classname
{
properties;
methods;
};
class derived_class1:
visibility_mode base_
Classname
SYNTAX:

{
properties;
methods;
};
class derived_class2:
visibility_mode base_
classname
{

properties;
methods;
};
... ... ... ... ... ...
class derived_classN:visibility_mode base_classname
{
properties;
methods;
};


#include <iostream>
#include <conio.h>
using namespace std;
class person /*Parent class*/
{
private:
char fname[100],lname[100],gender[10];
protected:
int age;
Program for hirarchical inheritance:

public: void input_person();
void display_person();
};
class student: public person /*Child class*/
{
private:
char college_name[100];
char level[20];
public: void input_student();
void display_student();
};

void person::input_person()
{
cout<<"First Name: ";
cin>>fname;
cout<<"Last Name: ";
cin>>name;
cout<<"Gender: ";
cin>>gender;
cout<<"Age: ";
cin>>age;
}

void person::display_person()
{
cout<<"First Name : "<<fname<<endl;
cout<<"Last Name : "<<lname<<endl;
cout<<"Gender : "<<gender<<endl;
cout<<"Age : "<<age<<endl;
}

void student::input_student()
{
person::input_person();
cout<<"College: "; fflush(stdin);
gets(college_name);
cout<<"Level: ";
cin>>level;
}

void student::display_student()
{
person::display_person();
cout<<"College : "<<college_name<<endl;
cout<<"Level : "<<level<<endl;
}

int main()
{
student s;
cout<<"Input data"<<endl;
s.input_student();
cout<<endl<<"Display data"<<endl; s.display_student();
getch();
return 0;
}

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
OOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdfOOP Assign No.03(AP).pdf
OOP Assign No.03(AP).pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
Introduction to Inheritance
Introduction to InheritanceIntroduction to Inheritance
Introduction to Inheritance
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++EASY TO LEARN INHERITANCE IN C++
EASY TO LEARN INHERITANCE IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Dr. Rajeshree Khande : Java Inheritance
Dr. Rajeshree Khande : Java InheritanceDr. Rajeshree Khande : Java Inheritance
Dr. Rajeshree Khande : Java Inheritance
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in c++ part1
Inheritance in c++ part1Inheritance in c++ part1
Inheritance in c++ part1
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 

Ähnlich wie Inheritance

Ähnlich wie Inheritance (20)

Inheritance
InheritanceInheritance
Inheritance
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
oop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdfoop database doc for studevsgdy fdsyn hdf
oop database doc for studevsgdy fdsyn hdf
 
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptxinheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance
InheritanceInheritance
Inheritance
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Inheritance (with classifications)
Inheritance (with classifications)Inheritance (with classifications)
Inheritance (with classifications)
 
E -COMMERCE.ppt
E -COMMERCE.pptE -COMMERCE.ppt
E -COMMERCE.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).ppt
 
Inheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ optInheritance chapter-6-computer-science-with-c++ opt
Inheritance chapter-6-computer-science-with-c++ opt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 

Mehr von SangeethaSasi1 (20)

L4 multiplexing &amp; multiple access 16
L4 multiplexing &amp; multiple access 16L4 multiplexing &amp; multiple access 16
L4 multiplexing &amp; multiple access 16
 
Image processing using matlab
Image processing using matlab Image processing using matlab
Image processing using matlab
 
Mc ppt
Mc pptMc ppt
Mc ppt
 
Mc ppt
Mc pptMc ppt
Mc ppt
 
Dip pppt
Dip ppptDip pppt
Dip pppt
 
Web techh
Web techhWeb techh
Web techh
 
Web tech
Web techWeb tech
Web tech
 
Vani wt
Vani wtVani wt
Vani wt
 
Vani dbms
Vani dbmsVani dbms
Vani dbms
 
Hema wt (1)
Hema wt (1)Hema wt (1)
Hema wt (1)
 
Hema rdbms
Hema rdbmsHema rdbms
Hema rdbms
 
Web tech
Web techWeb tech
Web tech
 
Web tech
Web techWeb tech
Web tech
 
Dbms
DbmsDbms
Dbms
 
Vani
VaniVani
Vani
 
Hema se
Hema seHema se
Hema se
 
Software
SoftwareSoftware
Software
 
Operating system
Operating systemOperating system
Operating system
 
Dataminng
DataminngDataminng
Dataminng
 
System calls
System callsSystem calls
System calls
 

Kürzlich hochgeladen

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Kürzlich hochgeladen (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 

Inheritance