SlideShare a Scribd company logo
1 of 31
Contents
 Access Specifiers
 Derived Classes
 Type of Inheritance
 Derived class Constructors (Constructors in single
Inheritance)
 Multiple Inheritance
 Constructors in multiple Inheritance
 Constructors in single/multiple Inheritance with
arguments.
Inheritance
 Inheritance is the second most important feature of OOP.
 In inheritance, the code of existing classes is used for making new classes.
 This saves time for writing and debugging the entire code for a new class.
 To inherit means to receive.
 In inheritance a new class is written such that it can access or use the members of
an existing class.
 The new class that can access the members of an existing class is called the derived
class or child class. The existing class is called the base class or parent class.
Inheritance
 The derived class can use the data members and member functions of the
base class.
 It can also have its own data members and member functions.
 Thus a derived class can even be larger than a base class.
Inheritance
 The figure shows the relationship
between a derived class and the base
class.
 The arrow is drawn from the derived
class to the base class.
 The direction of the arrow indicates
that the derived class can access
members of the base class but base
class cannot access members of its
derived class.
 In the figure shown on the left, the
derived class has only one member of
its own. The two members shown in
dotted lines are the members of the
base class. The derived class can also
access these two members of the base
class. Thus, whereas an object of the
base class can access only two
members, an object of the derived
class can access three members. A
new class can be derived from one or
more existing Classes.
Specifiers
 Public [Already Discussed]
 Private [Already Discussed]
 Protected
 The public members of a class are accessible by all functions in the program and
the private members of a class are accessible only by member functions and friend
functions of that class. Similarly, the protected members of a class are accessible
by the member functions and friend functions of that Class.
 The protected members of a base class are, however, accessible by members of its
derived classes but the private members of the base class are not accessible
directly by members of its derived classes. This is the main difference between the
protected and the private access specifiers.
 The protected members of a base class fall between private and public member.
These members are public for the derived class but for the rest of the program,
these are treated as private.
Defining Derived Class
The syntax for defining a derived class is slightly different from the syntax of the
base class definition. The declaration of a derived class also includes the name
of the base class from which it is derived. The general syntax for defining a
derived class is:
class sub_class_name : specifier
base_class_name{ ………………………………….
members of derived class
………………………………………}
Where
sub_class_name : represents name of the derived class.
: (colon) sets relation between the classes.
specifier represents the access specifier. It may be public, private or
Protected, base_class_name : represents name of the base class.
For example, a class "student" is
defined as:
class student {
private: Char name[15], address[15];
public:
void input (void);
void show(void);
};
Example Explanation
 The class student has two data members and two member
functions. Suppose the marks obtained by a student in three
different subjects and the total marks of these subjects are to
be included as new data members in the above class. This is
done by adding new members in the class. There are two ways
in which these new members can be added to the class:
• Add new members in the original
• Define a new class that has the new members and that also
uses members of the existing "student" class. Using the
members of an existing class is the principle of inheritance.
The new class is the derived class. The existing class serves as
the base class for the derived class.
Defining Derived Class (cont…)
Driving a new class from the existing class reduces the size of the
program. It also eliminates duplication of code within the
program. For example, let the name of the new class be marks.
This class uses the members of the existing student class.
class marks : public student {
private:
int s1,s2,s3, total;
public:
void inputmarks(void);
void show_detail(void); };
Defining Derived Class (cont..)
 The cIass marks is derived from the class student. The class marks is the
derived class. The class student is the base class.
 The derived class has four data members of integer type and two member
functions.
 It also uses the code of the base class student.
 The derived class cannot directly access the private data members the base
class student by using the dot operator.
 These members are only accessible to the derived class through the
interface functions within the base class.
Types of Inheritance
 There are three kinds of inheritance.
• Public • Private • Protected
 Public Inheritance
In Public Inheritance, the public members of the base class
become the public members of the derived class.
Thus the objects of the derived class can access public
members (both data and functions) of the base class.
Similarly, the protected data members of the base class also
become the protected members of derived class.
The general syntax for deriving a public class
from base class is:
class sub_class_name : public base_class_name {
…………………
…………………
) ;
where
public: specifies the public inheritance.
sub_class__name: represents name of the derived class.
base_class_name: represents name of the base class.
Example: Public Inheritance
#include<iostream>
using namespace std;
class A {
private:
int a1,a2;
protected:
int pa1,pa2;
public:
void ppp (void){
cout<<"Value of pal of class
A="<<pa1<<endl;
cout<<"Value of pa2 of class
A="<<pa2<<endl; }};
class B : public A {
public:
void get(void){
cout << "Enter value pal ? ";
cin>>pa1;
cout<<"Enter value pa2 ?";
cin>>pa2;}};
main (){
B ob;
ob.get();
ob.ppp();
};
Example Explanation
In the above program the class B is publicly derived from class
A. The object of the class B
 Cannot access the private data member’s a1 & a2 of base class
A.
 Can access the public function member ppp of base class A.
 Can access the protected data members pa1 & pa2 of base
class A.
Private Inheritance
In private Inheritance the objects of the derived class cannot
access the public members of the base class. Its objects can only
access the protected data members of the base class.
The general syntax for deriving private class from base class is:
class sub_class_name : private base_class_name {
………………………………….}
Private: specifies private inheritance.
sub_class_name: represents name of the derived class.
base_class_name: represents name of the base class.
Example: Private Inheritance
#include<iostream>
using namespace std;
class A {
private:
int a1,a2;
protected:
int pa1,pa2;
public:
void ppp (void){
cout<<"Value of pa1 of class
A="<<pa1<<endl;
cout<<"Value of pa2 of class
A="<<pa2<<endl;}
};
class B : private A
{ public:
void get(void)
{cout<<"Enter value of pa1 ";
cin>>pa1;
cout<<"Enter value of pa2 =";
cin>>pa2;
cout<<"Value of pa1 of class
A="<<pa1<<endl;
cout<<"Value of pa2 of class
A="<<pa2<<endl;}};
main ( ){
B ob;
ob.get(); }
Example Explanation
In the above program, the class B is derived as private for the
base class A. The objects of class B:
 cannot access the private data members a1 & a2 of base class
A.
 cannot access the public function member ppp of base class A.
 can only access the protected data members pal & pa2 of base
class A.
Protected Inheritance
The object of the class that is derived as protected can access
only the protected member of the base class. The general syntax
for deriving a protected Class from base class is:
class sub_class_name: protected base_class_name {
…………………
…………………
};
where protected: specifies protected inheritance.
sub_class_name: represents name of the derived class.
Base_class_name: represents name of the base class.
Example: Protected Inheritance
#include<iostream>
using namespace std;
class A
{
private :
int a1,a2;
protected:
int pa1,pa2;
public:
void get(void){
cout<<"value of pa1 of class A ";
cout<<"value of pa2 of class A ?";}};
class B: protected A{
public:
void get (void){
cout<<" Enter value of pa1=";
cin>>pa1;
cout<<"Value of pa2";
cin>>pa2;
cout<<"Value of pa1 of class
A="<<pa1<<endl;
cout<<"Value of pa2 of class
A="<<pa2<<endl;}};
main(){
B ob;
ob.get(); }
Example Explanation
In the above program, thc class B is derived as protected from the
base class A. The objects of class B:
 can only access the protected data members pal &.pa2 of base
class A.
Derived Class Constructors
 In inheritance, a constructor of the derived class as well the
constructor functions of the base class are automatically
executed when an object of the derived class is created.
 The following example explains the concept of constructor
functions in single inheritance.
Example: Derived Class Constructors
#include<iostream>
using namespace std;
class bb{
public:
bb(void){
cout<<"Constructor of
base class"<<endl;
}
};
class dd:public bb{
public:
dd(void){
cout<<"Constructor of
derived class"<<endl;
}
};
main(){
dd abc;
}
Example Explanation
 In the above program, when an object of the derived class dd
is created, the constructors of both the derived class as well as
the base class are executed.
 
Multiple Inheritance
 In Multiple inheritances, a class is derived by using more than
one classes.
 In multiple inheritance the derived class receives the members
of two or more base classes.
 Multiple inheritance is a powerful feature of Object Oriented
Programming.
 This technique reduces the program size and save
programmer's. time. The programmer uses the existing base
classes in the program coding to solve problems.
 The figure shown below illustrates the relation of derived class
with base classes.
 
Multiple Inheritance
 
Multiple Inheritance
In the figure, Class C is derived from two base classes A & B. The syntax of
multiple inheritance is similar to that of single inheritance class. The name of
base classes are written separated by comma(,).
Syntax:
Class sub_class : sp1 b_class_1, sp2 b_class_2……..
{
………………………..
};
Where :
sp1 represents the access specifier of the first base class.
sp2 represents the access specifier of the second base class.
Example: Multiple Inheritance
#include<iostream>
using namespace std;
class student {
private:
char name[20],address[20];
public:
void input (void){
cout<<"Enter name:";cin>>name;
cout<<"Enter address:";cin>>address;
}
void print(void){
cout<<"Name:"<<name<<endl;
cout<<"Address:"<<address<<endl;
}
};
class marks {
private:
int s1,s2,total;
public:
void inputmarks(void){
cout<<"Enter marks of
S1=";cin>>s1;
cout<<"Enter marks of
S2=";cin>>s2;
total=s1+s2;}
void showmarks(void){
cout<<"Marks in s1="<<s1<<endl;
Example: Multiple Inheritance (Cont..)
cout<<"Marks in
s2="<<s2<<endl;
cout<<"Total
Marks="<<total<<endl;
}
};
class show: public student,
public marks{
public:
void showrec(){
cout<<"Record is"<<endl;
print();
showmarks();
}
};
main(){
show m;
m.input();
m.inputmarks();
m.showrec();
}
Constructor in Multiple Inheritance
 When a class is derived from more than one base classes, the constructor of
the derived class as well as all of its base classes are executed when an
object of the derived class is created.
 If the constructor functions have no parameters then first the constructor
functions of the base class are executed and then the constructor functions
of the derived class is executed.
Example: Constructor in Multiple Inheritance
#include<iostream>
using namespace std;
class aa{
public:
aa(){
cout<<"Class aa"<<endl;
}
};
class bb{
public:
bb(){
cout<<"Class bb"<<endl;
};
class cc:public aa,public bb{
public:
cc(){
cout<<"Class cc"<<endl;
}
};
main(){
cc o;
}
Example Explanation
 In the above program, three classes are created.
 The cc class is publicly derived from two class aa and bb.
 All the classes have constructor functions are executed in the
following sequence.
1. Constructor of class aa is executed first.
2. Constructor of class bb is executed second.
3. Constructor of class cc is executed last.

More Related Content

What's hot (20)

Two-dimensional array in java
Two-dimensional array in javaTwo-dimensional array in java
Two-dimensional array in java
 
Class and object
Class and objectClass and object
Class and object
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Encapsulation in C++
Encapsulation in C++Encapsulation in C++
Encapsulation in C++
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Class 10
Class 10Class 10
Class 10
 
L11 array list
L11 array listL11 array list
L11 array list
 
Abstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core JavaAbstract Class & Abstract Method in Core Java
Abstract Class & Abstract Method in Core Java
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Methods and constructors in java
Methods and constructors in javaMethods and constructors in java
Methods and constructors in java
 

Viewers also liked

Abp Presentation
Abp PresentationAbp Presentation
Abp Presentationpintsin2001
 
Alok tiwari main profile presentation
Alok tiwari main profile presentationAlok tiwari main profile presentation
Alok tiwari main profile presentationAlok Tiwari
 
Seminar IM Interactive Workbook Progress
Seminar IM Interactive Workbook ProgressSeminar IM Interactive Workbook Progress
Seminar IM Interactive Workbook ProgressJ Au
 
5 tips for better search
5 tips for better search5 tips for better search
5 tips for better searchIntranätverk
 
A sintaxe da gramatica normativa ii
A sintaxe da gramatica normativa iiA sintaxe da gramatica normativa ii
A sintaxe da gramatica normativa iiIury Alberth
 
Sejarah Perkembangan Keperawatan
Sejarah Perkembangan KeperawatanSejarah Perkembangan Keperawatan
Sejarah Perkembangan Keperawatanpjj_kemenkes
 
Inês quite e desforrada - Gil Vicente
Inês quite e desforrada - Gil VicenteInês quite e desforrada - Gil Vicente
Inês quite e desforrada - Gil VicenteGijasilvelitz 2
 
Koneski Formação do feudalismo Corrigido
Koneski Formação do feudalismo CorrigidoKoneski Formação do feudalismo Corrigido
Koneski Formação do feudalismo CorrigidoTavinho Koneski Westphal
 
20170221 Estudio AudioOnline(V_Corta)
20170221 Estudio AudioOnline(V_Corta)20170221 Estudio AudioOnline(V_Corta)
20170221 Estudio AudioOnline(V_Corta)OptimediaSpain
 

Viewers also liked (15)

Abp Presentation
Abp PresentationAbp Presentation
Abp Presentation
 
Alok tiwari main profile presentation
Alok tiwari main profile presentationAlok tiwari main profile presentation
Alok tiwari main profile presentation
 
Seminar IM Interactive Workbook Progress
Seminar IM Interactive Workbook ProgressSeminar IM Interactive Workbook Progress
Seminar IM Interactive Workbook Progress
 
5 tips for better search
5 tips for better search5 tips for better search
5 tips for better search
 
Tipos de sujeito
Tipos de sujeitoTipos de sujeito
Tipos de sujeito
 
A sintaxe da gramatica normativa ii
A sintaxe da gramatica normativa iiA sintaxe da gramatica normativa ii
A sintaxe da gramatica normativa ii
 
Sejarah Perkembangan Keperawatan
Sejarah Perkembangan KeperawatanSejarah Perkembangan Keperawatan
Sejarah Perkembangan Keperawatan
 
Inês quite e desforrada - Gil Vicente
Inês quite e desforrada - Gil VicenteInês quite e desforrada - Gil Vicente
Inês quite e desforrada - Gil Vicente
 
Cantigas de amigo
Cantigas de amigoCantigas de amigo
Cantigas de amigo
 
Koneski Grécia antiga
Koneski Grécia antigaKoneski Grécia antiga
Koneski Grécia antiga
 
Koneski Formação do feudalismo Corrigido
Koneski Formação do feudalismo CorrigidoKoneski Formação do feudalismo Corrigido
Koneski Formação do feudalismo Corrigido
 
20170221 Estudio AudioOnline(V_Corta)
20170221 Estudio AudioOnline(V_Corta)20170221 Estudio AudioOnline(V_Corta)
20170221 Estudio AudioOnline(V_Corta)
 
O que é e o que não ebd
O que é e o que não ebdO que é e o que não ebd
O que é e o que não ebd
 
A vida do apóstolo dos gentios
A vida do apóstolo dos gentiosA vida do apóstolo dos gentios
A vida do apóstolo dos gentios
 
Llegir per gust. Escollim els premis de Sant Jordi
Llegir per gust. Escollim els premis de Sant JordiLlegir per gust. Escollim els premis de Sant Jordi
Llegir per gust. Escollim els premis de Sant Jordi
 

Similar to inheritance

Similar to inheritance (20)

Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
Inheritance Inheritance
Inheritance
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
OOPs Lecture 2
OOPs Lecture 2OOPs Lecture 2
OOPs Lecture 2
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
Inheritance
InheritanceInheritance
Inheritance
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Class properties
Class propertiesClass properties
Class properties
 
Class and object
Class and objectClass and object
Class and object
 
Inheritance
InheritanceInheritance
Inheritance
 

Recently uploaded

Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 

Recently uploaded (20)

Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 

inheritance

  • 1. Contents  Access Specifiers  Derived Classes  Type of Inheritance  Derived class Constructors (Constructors in single Inheritance)  Multiple Inheritance  Constructors in multiple Inheritance  Constructors in single/multiple Inheritance with arguments.
  • 2. Inheritance  Inheritance is the second most important feature of OOP.  In inheritance, the code of existing classes is used for making new classes.  This saves time for writing and debugging the entire code for a new class.  To inherit means to receive.  In inheritance a new class is written such that it can access or use the members of an existing class.  The new class that can access the members of an existing class is called the derived class or child class. The existing class is called the base class or parent class.
  • 3. Inheritance  The derived class can use the data members and member functions of the base class.  It can also have its own data members and member functions.  Thus a derived class can even be larger than a base class.
  • 4. Inheritance  The figure shows the relationship between a derived class and the base class.  The arrow is drawn from the derived class to the base class.  The direction of the arrow indicates that the derived class can access members of the base class but base class cannot access members of its derived class.  In the figure shown on the left, the derived class has only one member of its own. The two members shown in dotted lines are the members of the base class. The derived class can also access these two members of the base class. Thus, whereas an object of the base class can access only two members, an object of the derived class can access three members. A new class can be derived from one or more existing Classes.
  • 5. Specifiers  Public [Already Discussed]  Private [Already Discussed]  Protected  The public members of a class are accessible by all functions in the program and the private members of a class are accessible only by member functions and friend functions of that class. Similarly, the protected members of a class are accessible by the member functions and friend functions of that Class.  The protected members of a base class are, however, accessible by members of its derived classes but the private members of the base class are not accessible directly by members of its derived classes. This is the main difference between the protected and the private access specifiers.  The protected members of a base class fall between private and public member. These members are public for the derived class but for the rest of the program, these are treated as private.
  • 6. Defining Derived Class The syntax for defining a derived class is slightly different from the syntax of the base class definition. The declaration of a derived class also includes the name of the base class from which it is derived. The general syntax for defining a derived class is: class sub_class_name : specifier base_class_name{ …………………………………. members of derived class ………………………………………} Where sub_class_name : represents name of the derived class. : (colon) sets relation between the classes. specifier represents the access specifier. It may be public, private or Protected, base_class_name : represents name of the base class.
  • 7. For example, a class "student" is defined as: class student { private: Char name[15], address[15]; public: void input (void); void show(void); };
  • 8. Example Explanation  The class student has two data members and two member functions. Suppose the marks obtained by a student in three different subjects and the total marks of these subjects are to be included as new data members in the above class. This is done by adding new members in the class. There are two ways in which these new members can be added to the class: • Add new members in the original • Define a new class that has the new members and that also uses members of the existing "student" class. Using the members of an existing class is the principle of inheritance. The new class is the derived class. The existing class serves as the base class for the derived class.
  • 9. Defining Derived Class (cont…) Driving a new class from the existing class reduces the size of the program. It also eliminates duplication of code within the program. For example, let the name of the new class be marks. This class uses the members of the existing student class. class marks : public student { private: int s1,s2,s3, total; public: void inputmarks(void); void show_detail(void); };
  • 10. Defining Derived Class (cont..)  The cIass marks is derived from the class student. The class marks is the derived class. The class student is the base class.  The derived class has four data members of integer type and two member functions.  It also uses the code of the base class student.  The derived class cannot directly access the private data members the base class student by using the dot operator.  These members are only accessible to the derived class through the interface functions within the base class.
  • 11. Types of Inheritance  There are three kinds of inheritance. • Public • Private • Protected  Public Inheritance In Public Inheritance, the public members of the base class become the public members of the derived class. Thus the objects of the derived class can access public members (both data and functions) of the base class. Similarly, the protected data members of the base class also become the protected members of derived class.
  • 12. The general syntax for deriving a public class from base class is: class sub_class_name : public base_class_name { ………………… ………………… ) ; where public: specifies the public inheritance. sub_class__name: represents name of the derived class. base_class_name: represents name of the base class.
  • 13. Example: Public Inheritance #include<iostream> using namespace std; class A { private: int a1,a2; protected: int pa1,pa2; public: void ppp (void){ cout<<"Value of pal of class A="<<pa1<<endl; cout<<"Value of pa2 of class A="<<pa2<<endl; }}; class B : public A { public: void get(void){ cout << "Enter value pal ? "; cin>>pa1; cout<<"Enter value pa2 ?"; cin>>pa2;}}; main (){ B ob; ob.get(); ob.ppp(); };
  • 14. Example Explanation In the above program the class B is publicly derived from class A. The object of the class B  Cannot access the private data member’s a1 & a2 of base class A.  Can access the public function member ppp of base class A.  Can access the protected data members pa1 & pa2 of base class A.
  • 15. Private Inheritance In private Inheritance the objects of the derived class cannot access the public members of the base class. Its objects can only access the protected data members of the base class. The general syntax for deriving private class from base class is: class sub_class_name : private base_class_name { ………………………………….} Private: specifies private inheritance. sub_class_name: represents name of the derived class. base_class_name: represents name of the base class.
  • 16. Example: Private Inheritance #include<iostream> using namespace std; class A { private: int a1,a2; protected: int pa1,pa2; public: void ppp (void){ cout<<"Value of pa1 of class A="<<pa1<<endl; cout<<"Value of pa2 of class A="<<pa2<<endl;} }; class B : private A { public: void get(void) {cout<<"Enter value of pa1 "; cin>>pa1; cout<<"Enter value of pa2 ="; cin>>pa2; cout<<"Value of pa1 of class A="<<pa1<<endl; cout<<"Value of pa2 of class A="<<pa2<<endl;}}; main ( ){ B ob; ob.get(); }
  • 17. Example Explanation In the above program, the class B is derived as private for the base class A. The objects of class B:  cannot access the private data members a1 & a2 of base class A.  cannot access the public function member ppp of base class A.  can only access the protected data members pal & pa2 of base class A.
  • 18. Protected Inheritance The object of the class that is derived as protected can access only the protected member of the base class. The general syntax for deriving a protected Class from base class is: class sub_class_name: protected base_class_name { ………………… ………………… }; where protected: specifies protected inheritance. sub_class_name: represents name of the derived class. Base_class_name: represents name of the base class.
  • 19. Example: Protected Inheritance #include<iostream> using namespace std; class A { private : int a1,a2; protected: int pa1,pa2; public: void get(void){ cout<<"value of pa1 of class A "; cout<<"value of pa2 of class A ?";}}; class B: protected A{ public: void get (void){ cout<<" Enter value of pa1="; cin>>pa1; cout<<"Value of pa2"; cin>>pa2; cout<<"Value of pa1 of class A="<<pa1<<endl; cout<<"Value of pa2 of class A="<<pa2<<endl;}}; main(){ B ob; ob.get(); }
  • 20. Example Explanation In the above program, thc class B is derived as protected from the base class A. The objects of class B:  can only access the protected data members pal &.pa2 of base class A.
  • 21. Derived Class Constructors  In inheritance, a constructor of the derived class as well the constructor functions of the base class are automatically executed when an object of the derived class is created.  The following example explains the concept of constructor functions in single inheritance.
  • 22. Example: Derived Class Constructors #include<iostream> using namespace std; class bb{ public: bb(void){ cout<<"Constructor of base class"<<endl; } }; class dd:public bb{ public: dd(void){ cout<<"Constructor of derived class"<<endl; } }; main(){ dd abc; }
  • 23. Example Explanation  In the above program, when an object of the derived class dd is created, the constructors of both the derived class as well as the base class are executed.
  • 24.   Multiple Inheritance  In Multiple inheritances, a class is derived by using more than one classes.  In multiple inheritance the derived class receives the members of two or more base classes.  Multiple inheritance is a powerful feature of Object Oriented Programming.  This technique reduces the program size and save programmer's. time. The programmer uses the existing base classes in the program coding to solve problems.  The figure shown below illustrates the relation of derived class with base classes.
  • 26.   Multiple Inheritance In the figure, Class C is derived from two base classes A & B. The syntax of multiple inheritance is similar to that of single inheritance class. The name of base classes are written separated by comma(,). Syntax: Class sub_class : sp1 b_class_1, sp2 b_class_2…….. { ……………………….. }; Where : sp1 represents the access specifier of the first base class. sp2 represents the access specifier of the second base class.
  • 27. Example: Multiple Inheritance #include<iostream> using namespace std; class student { private: char name[20],address[20]; public: void input (void){ cout<<"Enter name:";cin>>name; cout<<"Enter address:";cin>>address; } void print(void){ cout<<"Name:"<<name<<endl; cout<<"Address:"<<address<<endl; } }; class marks { private: int s1,s2,total; public: void inputmarks(void){ cout<<"Enter marks of S1=";cin>>s1; cout<<"Enter marks of S2=";cin>>s2; total=s1+s2;} void showmarks(void){ cout<<"Marks in s1="<<s1<<endl;
  • 28. Example: Multiple Inheritance (Cont..) cout<<"Marks in s2="<<s2<<endl; cout<<"Total Marks="<<total<<endl; } }; class show: public student, public marks{ public: void showrec(){ cout<<"Record is"<<endl; print(); showmarks(); } }; main(){ show m; m.input(); m.inputmarks(); m.showrec(); }
  • 29. Constructor in Multiple Inheritance  When a class is derived from more than one base classes, the constructor of the derived class as well as all of its base classes are executed when an object of the derived class is created.  If the constructor functions have no parameters then first the constructor functions of the base class are executed and then the constructor functions of the derived class is executed.
  • 30. Example: Constructor in Multiple Inheritance #include<iostream> using namespace std; class aa{ public: aa(){ cout<<"Class aa"<<endl; } }; class bb{ public: bb(){ cout<<"Class bb"<<endl; }; class cc:public aa,public bb{ public: cc(){ cout<<"Class cc"<<endl; } }; main(){ cc o; }
  • 31. Example Explanation  In the above program, three classes are created.  The cc class is publicly derived from two class aa and bb.  All the classes have constructor functions are executed in the following sequence. 1. Constructor of class aa is executed first. 2. Constructor of class bb is executed second. 3. Constructor of class cc is executed last.