SlideShare a Scribd company logo
1 of 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

More Related Content

What's hot

What's hot (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
 

Viewers also liked

Презентація Скидан С.Д.
Презентація Скидан С.Д.Презентація Скидан С.Д.
Презентація Скидан С.Д.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
 

Viewers also liked (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 to Inheritance

Similar to 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
 

Recently uploaded

CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 

Recently uploaded (20)

CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 

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: : : }