SlideShare una empresa de Scribd logo
1 de 28
Inheritance provides the mechanism to use
the attributes and properties of a class by
an other class
“the mechanism by which one class
acquires
the properties of another class”
•Base Class (or superclass): the class
being inherited from
•Derived Class (or subclass): the class
that inherits
Parent ClassParent Class
Child Class
Base Class
Derived Class
Generalization
Specialization
A
B
Suppose we want a new class which should have all
the properties of class A and some its own
properties.
There two ways to do this
1. lets modify the class A and add desired functions .
But this approach is not good because class A may
be using in other functions .
2. Second is copy all the code of class A and add
desired functions . This approach is not good
because sometime code of class A may not be
available to us .
•The base class might be used by other part of the program and by
other programs, and we want its original behavior to remain intact for
those objects that already uses it. By deriving a new class, the original
base class remains unaffected.
•The source code of the base class may not be available to us. To use
the class, we only need its declaration in header file. Though we can
not access the code, but we can use it by inheritance.
•The base class might define a component in a class library that
supports a wide community of users. So, we must not modify the base
class even if we can.
•The base class may be an abstract base class, which is a class
designed to be a base class only. A class hierarchy can contain
general-purpose classes that do nothing on their own. Their purpose is
to define the behavior of a generic data structure to which derived
classes and implementation details.
•We might be building a class hierarchy to derive the benefits of the
object-oriented approach.
When a class inherits from another class, there
are three benefits:
(1) You can reuse the methods and data of the
existing class
(2) You can extend the existing class by adding
new data and new methods
(3) You can modify the existing class by
overloading its methods with your own
implementations
There are two classification schemes:
Classification A: There are three types of inheritance:
•Public Inheritance – it is the most common to use.
•Private Inheritance – used for terminating inheritance
chain.
•Protected Inheritance
Classification B: There are five types of inheritance:
•Simple Inheritance
•Multilevel Inheritance
•Multiple Inheritance
•Hybrid Inheritance
•Hierarchical Inheritance
Access
specifier
Access from
own class
Access
from
derived
class
Access in
remaining
program
Private

X X
Protected   X
Public   
Base class
member
access
specifier
Type of inheritance
public
inheritance
protected
inheritance
private
inheritance
Public
public in derived class.
Can be accessed directly by any
non-static member functions,
friend functions and non-
member functions.
protected in derived class.
Can be accessed directly by all
non-static member functions
and friend functions.
private in derived class.
Can be accessed directly by all
non-static member functions
and friend functions.
Protected
protected in derived class.
Can be accessed directly by all
non-static member functions
and friend functions.
protected in derived class.
Can be accessed directly by all
non-static member functions
and friend functions.
private in derived class.
Can be accessed directly by all
non-static member functions
and friend functions.
Private
Hidden in derived class.
Can be accessed by non-static
member functions and friend
functions through public or
protected member functions
of the base class.
Hidden in derived class.
Can be accessed by non-static
member functions and friend
functions through public or
protected member functions
of the base class.
Hidden in derived class.
Can be accessed by non-static
member functions and friend
functions through public or
protected member functions
of the base class.
CLASSIFICATION B
•
•
•
•
•
SIMPLE INHERITANCE
In this inheritance, a derived class is created from a
single base class.
Class A
Class B
Simple inheritance
HIERARCHICAL INHERITANCE
In this type of inheritance multiple derived classes inherit
from a single base class.
A
B C D
MULTILEVEL INHERITANCE:
Class A derives class B, class B derives class C and so on. It is called
multi-level inheritance. For multi-level inheritance to be meaningful:
Data members should be protected.
Public inheritance should be used.
A
C
B
MULTIPLE INHERITANCE:
A class can be derived by more than one
classes, its called multiple inheritance.
A
C
B
HYBRID INHERITANCE:
It is a combination of hierarchical and multilevel inheritance.
A
D
B C
ALLOCATION IN MEMORY
•
Data members of
base class
Data members of
derived class
base member1
base member2
...
derived member1
derived member2
...
CONSTRUCTOR
A constructor is a member function of a class that is
automatically called as soon as the object is created. It is used
to:
•Initialize the member data of the object.
•To dynamically and automatically allocate memory.
Following are the main properties of the constructor:
1. The constructor has the same name as that of class ( case
sensitivity is there).
2. It does not have any return type.
3. A constructor can be overloaded, i.e., there can be more
than one constructor in the class.
4. Default arguments can be used in constructor.
5. It must be public type.
DESTRUCTOR
Destructor is a special class function which destroys the object as
soon as the scope of the object ends.
An example :
Class A
{
A() { cout << “Constructor called”;}
~A(){ cout<< “Destructor called”;}
};
int main ()
{ A obj 1; // Constructor called
int x=1
if (x)
{
A obj 2; //Constructor called
} // Destructor called for obj 2
CALLING OF CONSTRUCTORS AND
DESTRUCTORS IN INHERITANCE
When the object of the derived class is
instantiated, following is the sequence:
•The space for member data of the object is
allocated.
•The constructor of derived class is called (but
body is not executed)
•The constructor of base class is called and its
body is also executed.
•The body of derived class constructor is executed.
•The execution return to the function.
•Scope of derived class object goes out,
destructor of derived is called and executed.
•Destructor of base is executed.
•Space for the object is released.
DCounter class derived from Counter
class DCounter : public Counter {
public:
DCounter(int c = 0) : Counter(c)
//explicit call to the parameterized constructor of base class
{ }
//decrement function
DCounter operator--( ) {
DCounter temp;
temp.count = --count ;
return temp ;
}
}; //DCounter class
int main( ){
Counter c(100);
DCounter dc(500);
++c; c.display( );
++dc; dc.display( );
--dc; dc.display();
return 0;
}
OVERRIDING OF A BASE CLASS FUNCTION IN
DERIVED CLASS
The derived class may have a function with same name as that
in the base class. When the function is called with the object of
base class, the base class function is called. When the function is
called with the object of derived class, the derived class function
is called.
s1.display( ); //call to display( ) of base class
s2.display( ); //call to display( ) of derived class
TYPES OF AMBIGUITY
I) Multiple Occurrence of Same Function:
There are two classes A and B, both
have same function f( ). A class C is
derived from both the classes. When an
object of c is created and function
f( ) is called with this object, it will be
an ambiguity. A compiler will unable
to decide which f( ) is called , f( ) of
A or f( ) of B.
TYPES OF AMBIGUITY
II) Diamond Shape Ambiguity (Hybrid Inheritance Ambiguity):
There is a class A. It derives two classes B and C. Class A
contains a function f(). A class D is derived from B and C
both. When an object of D is created and the function f( )
is called with the object of D, it will be an ambiguous call,
because the compiler doesn’t know, which function f( ) to
call, through class B or through class C.
A
B
D
C
CONTAINERSHIP OR COMPOSITION
There are two classes A and B. Class B contains object of
class A as a data member. It is called containership.
Containership is also called “has a“ relationship or “ is-part-
of “ relationship.
Ex. class A {
:
};
class B {
protected :
A aobj ; //containership
public:
:
:
}
Inheritance

Más contenido relacionado

La actualidad más candente

La actualidad más candente (19)

Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Class and object
Class and objectClass and object
Class and object
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance, Object Oriented Programming
Inheritance, Object Oriented ProgrammingInheritance, Object Oriented Programming
Inheritance, Object Oriented Programming
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
inheritance
inheritanceinheritance
inheritance
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance in c++theory
Inheritance in c++theoryInheritance in c++theory
Inheritance in c++theory
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
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 and its types In Java
Inheritance and its types In JavaInheritance and its types In Java
Inheritance and its types In Java
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 

Destacado

Презентація Скидан С.Д.
Презентація Скидан С.Д.Презентація Скидан С.Д.
Презентація Скидан С.Д.lanschool
 
MichelleArticleHorizontalWorkoutAugust2015
MichelleArticleHorizontalWorkoutAugust2015MichelleArticleHorizontalWorkoutAugust2015
MichelleArticleHorizontalWorkoutAugust2015Michelle Rober
 
Ahmed_CV_Ethylene
Ahmed_CV_EthyleneAhmed_CV_Ethylene
Ahmed_CV_EthyleneAhmed Hamdy
 
factors of production and joint stock companies
factors of production and joint stock companiesfactors of production and joint stock companies
factors of production and joint stock companiessourav verma
 
кондрацької л.б.
кондрацької  л.б.кондрацької  л.б.
кондрацької л.б.lanschool
 
лектронна пошта
лектронна пошталектронна пошта
лектронна поштаlanschool
 
Software engineering project(srs)!!
Software engineering project(srs)!!Software engineering project(srs)!!
Software engineering project(srs)!!sourav verma
 
презентація Білик В Я
презентація Білик В Япрезентація Білик В Я
презентація Білик В Яlanschool
 
Linux booting process!!
Linux booting process!!Linux booting process!!
Linux booting process!!sourav verma
 
Мала академія наук
Мала академія наукМала академія наук
Мала академія наукlanschool
 
Michelle Rober Consciously Creating Wellness in the Workplace
Michelle Rober Consciously Creating Wellness in the WorkplaceMichelle Rober Consciously Creating Wellness in the Workplace
Michelle Rober Consciously Creating Wellness in the WorkplaceMichelle Rober
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and GraphsIntro C# Book
 

Destacado (13)

Презентація Скидан С.Д.
Презентація Скидан С.Д.Презентація Скидан С.Д.
Презентація Скидан С.Д.
 
MichelleArticleHorizontalWorkoutAugust2015
MichelleArticleHorizontalWorkoutAugust2015MichelleArticleHorizontalWorkoutAugust2015
MichelleArticleHorizontalWorkoutAugust2015
 
Ahmed_CV_Ethylene
Ahmed_CV_EthyleneAhmed_CV_Ethylene
Ahmed_CV_Ethylene
 
factors of production and joint stock companies
factors of production and joint stock companiesfactors of production and joint stock companies
factors of production and joint stock companies
 
кондрацької л.б.
кондрацької  л.б.кондрацької  л.б.
кондрацької л.б.
 
лектронна пошта
лектронна пошталектронна пошта
лектронна пошта
 
Software engineering project(srs)!!
Software engineering project(srs)!!Software engineering project(srs)!!
Software engineering project(srs)!!
 
презентація Білик В Я
презентація Білик В Япрезентація Білик В Я
презентація Білик В Я
 
Linux booting process!!
Linux booting process!!Linux booting process!!
Linux booting process!!
 
Мала академія наук
Мала академія наукМала академія наук
Мала академія наук
 
Lecture8 data structure(graph)
Lecture8 data structure(graph)Lecture8 data structure(graph)
Lecture8 data structure(graph)
 
Michelle Rober Consciously Creating Wellness in the Workplace
Michelle Rober Consciously Creating Wellness in the WorkplaceMichelle Rober Consciously Creating Wellness in the Workplace
Michelle Rober Consciously Creating Wellness in the Workplace
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 

Similar a Inheritance

Similar a Inheritance (20)

11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
session 24_Inheritance.ppt
session 24_Inheritance.pptsession 24_Inheritance.ppt
session 24_Inheritance.ppt
 
inheritance
inheritanceinheritance
inheritance
 
week14 (1).ppt
week14 (1).pptweek14 (1).ppt
week14 (1).ppt
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
29c
29c29c
29c
 
29csharp
29csharp29csharp
29csharp
 
Lecturespecial
LecturespecialLecturespecial
Lecturespecial
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Bca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritanceBca 2nd sem u-3 inheritance
Bca 2nd sem u-3 inheritance
 
Mca 2nd sem u-3 inheritance
Mca 2nd  sem u-3 inheritanceMca 2nd  sem u-3 inheritance
Mca 2nd sem u-3 inheritance
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 

Último

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 

Último (20)

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 

Inheritance

  • 1.
  • 2. Inheritance provides the mechanism to use the attributes and properties of a class by an other class “the mechanism by which one class acquires the properties of another class” •Base Class (or superclass): the class being inherited from •Derived Class (or subclass): the class that inherits
  • 3. Parent ClassParent Class Child Class Base Class Derived Class
  • 5. Suppose we want a new class which should have all the properties of class A and some its own properties. There two ways to do this 1. lets modify the class A and add desired functions . But this approach is not good because class A may be using in other functions . 2. Second is copy all the code of class A and add desired functions . This approach is not good because sometime code of class A may not be available to us .
  • 6. •The base class might be used by other part of the program and by other programs, and we want its original behavior to remain intact for those objects that already uses it. By deriving a new class, the original base class remains unaffected. •The source code of the base class may not be available to us. To use the class, we only need its declaration in header file. Though we can not access the code, but we can use it by inheritance. •The base class might define a component in a class library that supports a wide community of users. So, we must not modify the base class even if we can. •The base class may be an abstract base class, which is a class designed to be a base class only. A class hierarchy can contain general-purpose classes that do nothing on their own. Their purpose is to define the behavior of a generic data structure to which derived classes and implementation details. •We might be building a class hierarchy to derive the benefits of the object-oriented approach.
  • 7. When a class inherits from another class, there are three benefits: (1) You can reuse the methods and data of the existing class (2) You can extend the existing class by adding new data and new methods (3) You can modify the existing class by overloading its methods with your own implementations
  • 8. There are two classification schemes: Classification A: There are three types of inheritance: •Public Inheritance – it is the most common to use. •Private Inheritance – used for terminating inheritance chain. •Protected Inheritance Classification B: There are five types of inheritance: •Simple Inheritance •Multilevel Inheritance •Multiple Inheritance •Hybrid Inheritance •Hierarchical Inheritance
  • 9. Access specifier Access from own class Access from derived class Access in remaining program Private  X X Protected   X Public   
  • 10. Base class member access specifier Type of inheritance public inheritance protected inheritance private inheritance Public public in derived class. Can be accessed directly by any non-static member functions, friend functions and non- member functions. protected in derived class. Can be accessed directly by all non-static member functions and friend functions. private in derived class. Can be accessed directly by all non-static member functions and friend functions. Protected protected in derived class. Can be accessed directly by all non-static member functions and friend functions. protected in derived class. Can be accessed directly by all non-static member functions and friend functions. private in derived class. Can be accessed directly by all non-static member functions and friend functions. Private Hidden in derived class. Can be accessed by non-static member functions and friend functions through public or protected member functions of the base class. Hidden in derived class. Can be accessed by non-static member functions and friend functions through public or protected member functions of the base class. Hidden in derived class. Can be accessed by non-static member functions and friend functions through public or protected member functions of the base class.
  • 12. SIMPLE INHERITANCE In this inheritance, a derived class is created from a single base class. Class A Class B Simple inheritance
  • 13. HIERARCHICAL INHERITANCE In this type of inheritance multiple derived classes inherit from a single base class. A B C D
  • 14. MULTILEVEL INHERITANCE: Class A derives class B, class B derives class C and so on. It is called multi-level inheritance. For multi-level inheritance to be meaningful: Data members should be protected. Public inheritance should be used. A C B
  • 15. MULTIPLE INHERITANCE: A class can be derived by more than one classes, its called multiple inheritance. A C B
  • 16. HYBRID INHERITANCE: It is a combination of hierarchical and multilevel inheritance. A D B C
  • 17. ALLOCATION IN MEMORY • Data members of base class Data members of derived class base member1 base member2 ... derived member1 derived member2 ...
  • 18. CONSTRUCTOR A constructor is a member function of a class that is automatically called as soon as the object is created. It is used to: •Initialize the member data of the object. •To dynamically and automatically allocate memory. Following are the main properties of the constructor: 1. The constructor has the same name as that of class ( case sensitivity is there). 2. It does not have any return type. 3. A constructor can be overloaded, i.e., there can be more than one constructor in the class. 4. Default arguments can be used in constructor. 5. It must be public type.
  • 19. DESTRUCTOR Destructor is a special class function which destroys the object as soon as the scope of the object ends. An example : Class A { A() { cout << “Constructor called”;} ~A(){ cout<< “Destructor called”;} }; int main () { A obj 1; // Constructor called int x=1 if (x) { A obj 2; //Constructor called } // Destructor called for obj 2
  • 20. CALLING OF CONSTRUCTORS AND DESTRUCTORS IN INHERITANCE When the object of the derived class is instantiated, following is the sequence: •The space for member data of the object is allocated. •The constructor of derived class is called (but body is not executed) •The constructor of base class is called and its body is also executed. •The body of derived class constructor is executed. •The execution return to the function. •Scope of derived class object goes out, destructor of derived is called and executed. •Destructor of base is executed. •Space for the object is released.
  • 21.
  • 22. DCounter class derived from Counter class DCounter : public Counter { public: DCounter(int c = 0) : Counter(c) //explicit call to the parameterized constructor of base class { } //decrement function DCounter operator--( ) { DCounter temp; temp.count = --count ; return temp ; } }; //DCounter class int main( ){ Counter c(100); DCounter dc(500); ++c; c.display( ); ++dc; dc.display( ); --dc; dc.display(); return 0; }
  • 23. OVERRIDING OF A BASE CLASS FUNCTION IN DERIVED CLASS The derived class may have a function with same name as that in the base class. When the function is called with the object of base class, the base class function is called. When the function is called with the object of derived class, the derived class function is called. s1.display( ); //call to display( ) of base class s2.display( ); //call to display( ) of derived class
  • 24.
  • 25. TYPES OF AMBIGUITY I) Multiple Occurrence of Same Function: There are two classes A and B, both have same function f( ). A class C is derived from both the classes. When an object of c is created and function f( ) is called with this object, it will be an ambiguity. A compiler will unable to decide which f( ) is called , f( ) of A or f( ) of B.
  • 26. TYPES OF AMBIGUITY II) Diamond Shape Ambiguity (Hybrid Inheritance Ambiguity): There is a class A. It derives two classes B and C. Class A contains a function f(). A class D is derived from B and C both. When an object of D is created and the function f( ) is called with the object of D, it will be an ambiguous call, because the compiler doesn’t know, which function f( ) to call, through class B or through class C. A B D C
  • 27. CONTAINERSHIP OR COMPOSITION There are two classes A and B. Class B contains object of class A as a data member. It is called containership. Containership is also called “has a“ relationship or “ is-part- of “ relationship. Ex. class A { : }; class B { protected : A aobj ; //containership public: : : }