SlideShare a Scribd company logo
1 of 19
by
G.GowriLatha,M.Sc (Info Tech),
Department of CS&IT
Nadar Saraswathi college of arts and
science, Theni
BA
A
B
C
(i) SINGLE INHERITANCE
(ii)MULTIPLE INHERRITANCE
A
B C
A
B
C
D
(iii) HIERACHICAL
INHERITANCE
(iv) MULTILEVEL
 Base class to derived as
the derived class.
A
B C
D
(v) Hybrid inheritance
Class derived-class name : visibility base
class-name
{
----------------------------------
Member function
----------------------------------
}
 The public data member and three public member
function.
 B base class and D derived class.
 The class D contains one private data member and two
public member function.
EXAMPLE PROGRAM:
#include<iostream.h >
#include<conio.h>
class student
{
public:
int rno;
char name[20];
void getdata()
}
Cout<<“Enter RollNo : -t”;
Cin>>rno;
Cout<<“enter name :-t”
Cin>>name;
}
};
class marks : public student
{
Public:
int m1,m2,m3,tot;
float per;
Void getmarks()
{
Getdata();
Cout<<“enter mark1:-t”;
Cin>>m1;
Cout<<“enter mark2:-t”;
Cin>>m2;
Cout<<“enter mark3:-t”;
Cin>>m3;
}
Void display()
{
Getmarks();
Cout<<“RollNo t namet
mark1 t mark2 t mark3 t
total t percentage”;
Cout<<rno<<“t”;<<name<<“
t”<<m1<<“t”<<m2<<“t”<<
m3<<“t”<<tot<<“t”<<per;
}
};
Void main()
{
Student std;
Clrscr();
Std.getmarks();
Std.display();
Getch();
}
 The mechanism of the
derived class D.
 Private derivation the
public members of the
base class become private
members of the derived
class.
 The private data need from inherited by a derived
class, the private member making it public.
 A member declared as protected is accessible by the
member function with
private : //optional.
--------------//visible to member function.
--------------//with in its class.
Protected:
---------------//write member function.
---------------//of its own and derived class.
Public:
---------------//write to all functions.
---------------//with in the program.
private
protected
public
private
private
private
protected
protected
protected
public
public
public
Class BNot inheritable
Class D1: public B
Class D2: private B
Class X: public D1protected D2
The access control to the
private and protected
member of a class. They
could be,
 A functions that is a friend
of a class.
 A member function of a
class that is a friend of a
class.
 A member function of a
derived class.
 The class A serve as a
base class for the
derived class B.
 The class B is known
as intermediate base
class provide a link for
the inheritance
between A and C.
All users
Derived class
Member
function
Own member function
Friend function
class
public
protected
private
A
B
C
Base class Grand father
Intermediate base class father
Derived class child
#include<iostream.h>
#include<conio.h>
Class student {
Protected:
int rno,m1,m2;
Public:
void get() {
cout<<“enter the roll no:”;
Cin>>rno;
Cout<<“enter the mark1:”:
Cin>>m1;
Cout<<“enter the mark2:”;
Cin>>m2;
} };
Class sports {
Protected:
Int sm;
Public:
Void getsm() {
Cout<<“enter the sports mark:”;
Cin>>sm;
} };
Class statement: public student,
public sports {
Int tot,avg;
Public:
Void display() {
Tot =(m1+m2+sm);
Avg=tot/3;
Cout<<“nnt roll
no:”<<rno<<“nt
total:”<<tot;
Cout<<“nt
average:”<<avg;
} };
Void main() {
clrscr();
Statement obj;
Obj.get();
Obj.getsm();
Obj.display();
Getch();
}
Output:
Enter the roll no:100
Enter two marks
90
80
Enter the sports mark:90
Roll no:100
Total :260
Average :86.66
 A class can inherit the
attributes of two or more
classes.
 This known as multiple
inheritance.
Syntax:
EXAMPLE PROGRAM:
#include<iostream.h>
using namespace std;
class A
{
public:
B-1 B-2 B-n
D
Class D: visibility B-1,visibility B-2 …..
{
--------------------------
-----(Body of D)-----
---------------------------
}
A() {
Cout<<“A ‘s constructor
called”<<end1; } };
Class B {
Public:
B() {
Cout<<“B ‘s constructor
called”<<end1; } };
Class C:public B,public A
{ public:
C() {
Cout<<“C ‘s constructor
called”<<end1; } };
Int main()
{
C c;
return 0;
}
Output:
B’s constructor called
A’s constructor called
C’s constructor called
 One class may be
inherited by more than
one class.
 This process is known as
hierarchical inheritance.
 When more than one
classes are derived from
a single base class , such
inheritance is known as
hierarchical inheritance,
where feature that are
common in lower level are
included in parent class.
student
arts
medical
engineering
mech elec civil
 Hybrid inheritance is also
called as virtual base
class.
 A class is derived from
classes as in multiple
inheritance.
 One of the parent
classes is not a base
class .It is a derived
class.
 Multiple or multilevel type
of inheritance are used
to derive a class
.
EXAMPLE PROGRAM:
#include<bits/stdc++.h>
using namespace std;
class parent
{ public:
Int id_p; };
Class child : public parent
{
public :
int id_c;
};
Int main()
{
child obj1;
obj1.id_c=7;
obj1.id_p=91;
Cout<<“child id
is”<<obj1.id_c<<end1;
Cout<<“parent id
is”<<obj1.id_p<<end1;
return 0;
}
OUTPUT:
child id is 7
parent id is 91
studen
t
test
result
sports
Inheritance

More Related Content

What's hot

Roadmap to Object Oriented Programming
Roadmap to Object Oriented ProgrammingRoadmap to Object Oriented Programming
Roadmap to Object Oriented Programmingssuser6fc8b1
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperDeepak Singh
 
Nonlinear analysis of fixed support beam with hinge by hinge method in c prog...
Nonlinear analysis of fixed support beam with hinge by hinge method in c prog...Nonlinear analysis of fixed support beam with hinge by hinge method in c prog...
Nonlinear analysis of fixed support beam with hinge by hinge method in c prog...Salar Delavar Qashqai
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritancezindadili
 
CBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question PaperCBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question PaperMalathi Senthil
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarSivakumar R D .
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Deepak Singh
 
Sample Paper Class XI (Informatics Practices)
Sample Paper Class XI (Informatics Practices)Sample Paper Class XI (Informatics Practices)
Sample Paper Class XI (Informatics Practices)Poonam Chopra
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++Jay Patel
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphismFALLEE31188
 
C program report tips
C program report tipsC program report tips
C program report tipsHarry Pott
 
CS50 Lecture4
CS50 Lecture4CS50 Lecture4
CS50 Lecture4昀 李
 

What's hot (20)

Roadmap to Object Oriented Programming
Roadmap to Object Oriented ProgrammingRoadmap to Object Oriented Programming
Roadmap to Object Oriented Programming
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
 
Nonlinear analysis of fixed support beam with hinge by hinge method in c prog...
Nonlinear analysis of fixed support beam with hinge by hinge method in c prog...Nonlinear analysis of fixed support beam with hinge by hinge method in c prog...
Nonlinear analysis of fixed support beam with hinge by hinge method in c prog...
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritance
 
C Structure and Union in C
C Structure and Union in CC Structure and Union in C
C Structure and Union in C
 
Programs of C++
Programs of C++Programs of C++
Programs of C++
 
L10
L10L10
L10
 
CBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question PaperCBSE Grade12, Computer Science, Sample Question Paper
CBSE Grade12, Computer Science, Sample Question Paper
 
Oop l2
Oop l2Oop l2
Oop l2
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009Cbse question-paper-computer-science-2009
Cbse question-paper-computer-science-2009
 
Sample Paper Class XI (Informatics Practices)
Sample Paper Class XI (Informatics Practices)Sample Paper Class XI (Informatics Practices)
Sample Paper Class XI (Informatics Practices)
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
C questions
C questionsC questions
C questions
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
Revision1schema C programming
Revision1schema C programmingRevision1schema C programming
Revision1schema C programming
 
C program report tips
C program report tipsC program report tips
C program report tips
 
CS50 Lecture4
CS50 Lecture4CS50 Lecture4
CS50 Lecture4
 

Similar to Inheritance (20)

Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Unit 4
Unit 4Unit 4
Unit 4
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
classes & objects.ppt
classes & objects.pptclasses & objects.ppt
classes & objects.ppt
 
inhertance c++
inhertance c++inhertance c++
inhertance c++
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
What is inheritance
What is inheritanceWhat is inheritance
What is inheritance
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
Hierarchical inheritance
Hierarchical inheritanceHierarchical inheritance
Hierarchical inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
inheritance in OOPM
inheritance in OOPMinheritance in OOPM
inheritance in OOPM
 
Bc0037
Bc0037Bc0037
Bc0037
 
2.6 Types of Inheritance in OOP C++.pptx
2.6 Types of Inheritance in OOP C++.pptx2.6 Types of Inheritance in OOP C++.pptx
2.6 Types of Inheritance in OOP C++.pptx
 
Lecture4
Lecture4Lecture4
Lecture4
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Lecture07
Lecture07Lecture07
Lecture07
 

More from GowriLatha1

Filtering in frequency domain
Filtering in frequency domainFiltering in frequency domain
Filtering in frequency domainGowriLatha1
 
Demand assigned and packet reservation multiple access
Demand assigned and packet reservation multiple accessDemand assigned and packet reservation multiple access
Demand assigned and packet reservation multiple accessGowriLatha1
 
Software engineering
Software engineeringSoftware engineering
Software engineeringGowriLatha1
 
Web services & com+ components
Web services & com+ componentsWeb services & com+ components
Web services & com+ componentsGowriLatha1
 
Comparison with Traditional databases
Comparison with Traditional databasesComparison with Traditional databases
Comparison with Traditional databasesGowriLatha1
 
Comparison with Traditional databases
Comparison with Traditional databasesComparison with Traditional databases
Comparison with Traditional databasesGowriLatha1
 
Inter process communication
Inter process communicationInter process communication
Inter process communicationGowriLatha1
 
computer network
computer networkcomputer network
computer networkGowriLatha1
 
Operating System
Operating SystemOperating System
Operating SystemGowriLatha1
 
Data mining query language
Data mining query languageData mining query language
Data mining query languageGowriLatha1
 

More from GowriLatha1 (20)

Filtering in frequency domain
Filtering in frequency domainFiltering in frequency domain
Filtering in frequency domain
 
Demand assigned and packet reservation multiple access
Demand assigned and packet reservation multiple accessDemand assigned and packet reservation multiple access
Demand assigned and packet reservation multiple access
 
Software engineering
Software engineeringSoftware engineering
Software engineering
 
Shadow paging
Shadow pagingShadow paging
Shadow paging
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Hive
HiveHive
Hive
 
Web services & com+ components
Web services & com+ componentsWeb services & com+ components
Web services & com+ components
 
Comparison with Traditional databases
Comparison with Traditional databasesComparison with Traditional databases
Comparison with Traditional databases
 
Recovery system
Recovery systemRecovery system
Recovery system
 
Comparison with Traditional databases
Comparison with Traditional databasesComparison with Traditional databases
Comparison with Traditional databases
 
Static analysis
Static analysisStatic analysis
Static analysis
 
Hema dm
Hema dmHema dm
Hema dm
 
Data reduction
Data reductionData reduction
Data reduction
 
Inter process communication
Inter process communicationInter process communication
Inter process communication
 
computer network
computer networkcomputer network
computer network
 
Operating System
Operating SystemOperating System
Operating System
 
Data mining query language
Data mining query languageData mining query language
Data mining query language
 
Enterprice java
Enterprice javaEnterprice java
Enterprice java
 
Ethernet
EthernetEthernet
Ethernet
 
Java script
Java scriptJava script
Java script
 

Recently uploaded

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
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
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
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
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
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
 
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
 
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
 
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
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Recently uploaded (20)

CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
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
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
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
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
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
 
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...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
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
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

Inheritance

  • 1. by G.GowriLatha,M.Sc (Info Tech), Department of CS&IT Nadar Saraswathi college of arts and science, Theni
  • 2.
  • 3. BA A B C (i) SINGLE INHERITANCE (ii)MULTIPLE INHERRITANCE A B C A B C D (iii) HIERACHICAL INHERITANCE (iv) MULTILEVEL
  • 4.  Base class to derived as the derived class. A B C D (v) Hybrid inheritance Class derived-class name : visibility base class-name { ---------------------------------- Member function ---------------------------------- }
  • 5.  The public data member and three public member function.  B base class and D derived class.  The class D contains one private data member and two public member function. EXAMPLE PROGRAM: #include<iostream.h > #include<conio.h> class student { public: int rno;
  • 6. char name[20]; void getdata() } Cout<<“Enter RollNo : -t”; Cin>>rno; Cout<<“enter name :-t” Cin>>name; } }; class marks : public student { Public: int m1,m2,m3,tot; float per; Void getmarks() { Getdata(); Cout<<“enter mark1:-t”; Cin>>m1; Cout<<“enter mark2:-t”; Cin>>m2; Cout<<“enter mark3:-t”; Cin>>m3; } Void display() { Getmarks();
  • 7. Cout<<“RollNo t namet mark1 t mark2 t mark3 t total t percentage”; Cout<<rno<<“t”;<<name<<“ t”<<m1<<“t”<<m2<<“t”<< m3<<“t”<<tot<<“t”<<per; } }; Void main() { Student std; Clrscr(); Std.getmarks(); Std.display(); Getch(); }  The mechanism of the derived class D.  Private derivation the public members of the base class become private members of the derived class.
  • 8.  The private data need from inherited by a derived class, the private member making it public.  A member declared as protected is accessible by the member function with private : //optional. --------------//visible to member function. --------------//with in its class. Protected: ---------------//write member function. ---------------//of its own and derived class. Public: ---------------//write to all functions. ---------------//with in the program.
  • 10. The access control to the private and protected member of a class. They could be,  A functions that is a friend of a class.  A member function of a class that is a friend of a class.  A member function of a derived class.  The class A serve as a base class for the derived class B.  The class B is known as intermediate base class provide a link for the inheritance between A and C.
  • 11. All users Derived class Member function Own member function Friend function class public protected private A B C Base class Grand father Intermediate base class father Derived class child
  • 12. #include<iostream.h> #include<conio.h> Class student { Protected: int rno,m1,m2; Public: void get() { cout<<“enter the roll no:”; Cin>>rno; Cout<<“enter the mark1:”: Cin>>m1; Cout<<“enter the mark2:”; Cin>>m2; } }; Class sports { Protected: Int sm; Public: Void getsm() { Cout<<“enter the sports mark:”; Cin>>sm; } }; Class statement: public student, public sports { Int tot,avg; Public: Void display() { Tot =(m1+m2+sm); Avg=tot/3;
  • 13. Cout<<“nnt roll no:”<<rno<<“nt total:”<<tot; Cout<<“nt average:”<<avg; } }; Void main() { clrscr(); Statement obj; Obj.get(); Obj.getsm(); Obj.display(); Getch(); } Output: Enter the roll no:100 Enter two marks 90 80 Enter the sports mark:90 Roll no:100 Total :260 Average :86.66
  • 14.  A class can inherit the attributes of two or more classes.  This known as multiple inheritance. Syntax: EXAMPLE PROGRAM: #include<iostream.h> using namespace std; class A { public: B-1 B-2 B-n D Class D: visibility B-1,visibility B-2 ….. { -------------------------- -----(Body of D)----- --------------------------- }
  • 15. A() { Cout<<“A ‘s constructor called”<<end1; } }; Class B { Public: B() { Cout<<“B ‘s constructor called”<<end1; } }; Class C:public B,public A { public: C() { Cout<<“C ‘s constructor called”<<end1; } }; Int main() { C c; return 0; } Output: B’s constructor called A’s constructor called C’s constructor called
  • 16.  One class may be inherited by more than one class.  This process is known as hierarchical inheritance.  When more than one classes are derived from a single base class , such inheritance is known as hierarchical inheritance, where feature that are common in lower level are included in parent class. student arts medical engineering mech elec civil
  • 17.  Hybrid inheritance is also called as virtual base class.  A class is derived from classes as in multiple inheritance.  One of the parent classes is not a base class .It is a derived class.  Multiple or multilevel type of inheritance are used to derive a class . EXAMPLE PROGRAM: #include<bits/stdc++.h> using namespace std; class parent { public: Int id_p; }; Class child : public parent { public : int id_c; }; Int main()
  • 18. { child obj1; obj1.id_c=7; obj1.id_p=91; Cout<<“child id is”<<obj1.id_c<<end1; Cout<<“parent id is”<<obj1.id_p<<end1; return 0; } OUTPUT: child id is 7 parent id is 91 studen t test result sports