SlideShare a Scribd company logo
Object Oriented
Programming using C++
Inheritance
INHERITANCE
Inheritance
Inheritance is a phenomenon of acquiring
properties from predecessor.
In object oriented systems, it means
acquiring the properties of base class to the
derived class.
Hence the derived class gets specialized with
the properties of base class as well as its own
properties.
It involves the concept of code reusability.
Inheritance
Inheritance is the process by which new classes
called derived classes are created from existing
classes called base classes.
The derived classes have all the features of the
base class and the programmer can choose to
add new features specific to the newly created
derived class.
The idea of inheritance implements the is a
relationship. For example, mammal IS-A animal,
dog IS-A mammal hence dog IS-A animal as well
and so on.
Types of inheritance:
single level
multi level
Base Class A
Derived Class B
Class A
Intermediate
derived class B
Derived Class C
Types of inheritance:
Multiple inheritance
Hierarchical inheritance.
Base class A Base class B
Derived Class
C
Class A
Derived class B
Derived
class C
Derived class
D
Types of inheritance:
Hybrid inheritance
Class A
Intermediate
Derived class B
Intermediate
Derived class C
Derived class D
Class A
Class B
Class C
Class D
FEATURES /ADVANTAGES OF
INHERITANCE
Reusability of Code
Saves Time and Effort
Faster development, easier maintenance and
easy to extend
Capable of expressing the inheritance
relationship and its transitive nature which
ensures closeness with real world problems
Syntax for inheritance:
class Base-class-name
{
___ //
___ // definition of the class
};
class derived-class name : visibility-mode
base-class-name
{
….. //
….. // members of derived class
};
Visibility Mode
Here visibility mode can be public, private
or protected.
Private is the default mode. They are visible
to member functions within its class.
Protected visible to member functions of
its own class and derived class.
Public visible to all functions in the
program.
When a class is inherited in any
of these visibility modes, the
visibility of its members varies.
The visibility of inherited members is
as shown:
Single level
In this kind of inheritance, there is a single
base class and a single derived class.
Eg: class base
{
// class declaration
};
class derived : public base
{
//derive class declaration
};
Here, base class base is inherited into the derived class derived.
Multi level Inheritance
In this type of inheritance
there is more than one
levels of inheritance and
each derived class serves as
the base class for the other
derived class.
Eg. class base1
{
//base1 declaration
};
class base2: public base1
{
//base2 declaration
};
class derived : public base2
{
//derived class
declaration
};
Here, base1 is a base class that
is inherited into the derived
class base2, which in turn is
inherited into the derived
class derived.
Question to implement
A class A{
char name[] …….
int roll_no; ……
+void get();
+void show(); };
B class B: public A
float m1,m2,m3; {
+void take(); …..
+void put(); };
C
class C: public B
float percentage; {
+void cal_per(); …..
+void disp_result(); };
Multiple Inheritance –
In this kind of Inheritance, there are many base classes and
one derived class from these base classes.
Eg. Class base1{};
class base2{};
Class base 3{};
class derived : public base1, protected base2, public base3
{
// class declaration
};
Here, base1, base2 and base3 are base classes that are
inherited into the derived class derived.
Hierarchical Inheritance
In this type of Inheritance, there
is one base class and many
derived classes from this base
class
Eg. class base
{
// class declaration
};
class der1 : public base
{
// class declaration
};
class der2 : public base
{
// class declaration
};
Here, der1 and der2 are derived
classes that are derived from
the base class base.
Hybrid Inheritance
This is a combination of one or more
than one type of Inheritance, like a
combination of multiple and multilevel
inheritance.
Virtual Base Classes
In hybrid Inheritance, that is a combination of
multiple and hierarchical inheritance, there may
arise a case when the features of ancestor base
class are inherited via multiple paths in to the
ultimate derived class, resulting in deriving
duplicate set of members.
This duplication of inherited members can be
avoided in C++ by inheriting the ancestor base
class as virtual base class in to any derived class.
Hybrid inheritance
Class A
Intermediate
Derived class B1
Intermediate
Derived class B2
Derived class C
Class A
{
// (ancestor) class declaration
};
Class B1: virtual public A
{
//(intermediate derived) class
declaration
};
Class B2: virtual public A
{
//(intermediate derived) class
declaration
};
Class C: public B1, public B2
{// only one copy of the
members of class A is inherited
};
Syntax:
Class A
{
// (ancestor) class declaration
};
Class B1: virtual public A
{
//(intermediate derived) class declaration
};
Class B2: virtual public A
{
//(intermediate derived) class declaration
};
Class C: public B1, public B2
{// only one copy of the members of class A is inherited
};
Constructors in derived class:
• Whenever we have parameterized
constructors in Base class, it becomes
necessary to define a parameterized
constructor in derived class so that on the
creation of objects of derived class, we can
pass the parameters to the base constructors
through the derived constructor.
The format for the derived class constructor in this case
is a bit different as shown below:
class derived: <visibility mode> <base class name>
{
int A, B; //declaration of data members of class Derived
Public:
derived( arg 1, arg 2, arg 3…. arg N): base 1( arg a,arg b,,…,arg
d), base 2( arg e, arg f,… arg k),…base N( arg l, arg m…,arg p)
{
A= arg 1;
B= arg 2;
}
};
Abstract Classes
• Is one that is used to create objects.
• An abstract class is designed only to act as a
base class(to be inherited by other classes).

More Related Content

PPTX
Class and object
PPTX
Polymorphism
PPTX
Friend functions
PPTX
Inheritance, friend function, virtual function, polymorphism
PPTX
Inheritance
PPTX
Inheritance
PPTX
Inheritance
PPT
friend function(c++)
Class and object
Polymorphism
Friend functions
Inheritance, friend function, virtual function, polymorphism
Inheritance
Inheritance
Inheritance
friend function(c++)

What's hot (20)

PPTX
inheritance c++
PPT
Inheritance
PPTX
Friend function & friend class
PPTX
Inheritance in c++
PPTX
OOP C++
PPS
Inheritance chepter 7
PPTX
Inheritance
PPTX
C# classes objects
PPT
Java: Inheritance
PPT
Inheritance, polymorphisam, abstract classes and composition)
PPTX
Multiple inheritance possible in Java
PPT
Inheritance in C++
PPT
Class and object in C++
PPSX
PPTX
EASY TO LEARN INHERITANCE IN C++
DOCX
JAVA Notes - All major concepts covered with examples
PPTX
Object as function argument , friend and static function by shahzad younas
PPT
Inheritance : Extending Classes
PPTX
Inheritance in oops
PPT
Friends function and_classes
inheritance c++
Inheritance
Friend function & friend class
Inheritance in c++
OOP C++
Inheritance chepter 7
Inheritance
C# classes objects
Java: Inheritance
Inheritance, polymorphisam, abstract classes and composition)
Multiple inheritance possible in Java
Inheritance in C++
Class and object in C++
EASY TO LEARN INHERITANCE IN C++
JAVA Notes - All major concepts covered with examples
Object as function argument , friend and static function by shahzad younas
Inheritance : Extending Classes
Inheritance in oops
Friends function and_classes
Ad

Similar to Inheritance (20)

PPTX
Inheritance
PPTX
Inheritance (with classifications)
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
PPTX
Inheritance in c++ by Manan Pasricha
PPS
Inheritance
PDF
PPTX
Ritik (inheritance.cpp)
PDF
OOP Assign No.03(AP).pdf
PPTX
Introduction to inheritance and different types of inheritance
PDF
Inheritance
PPTX
Inheritance
PPT
Inheritance
PDF
lecture 6.pdf
PPSX
Inheritance and Polymorphism in Oops
PPT
Inheritance.ppt
PPTX
Inheritance in C++ (Programming Fundamentals)
PPTX
Inheritance in c++
DOCX
oop database doc for studevsgdy fdsyn hdf
PPTX
Object Oriented Design and Programming Unit-03
Inheritance
Inheritance (with classifications)
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
Inheritance in c++ by Manan Pasricha
Inheritance
Ritik (inheritance.cpp)
OOP Assign No.03(AP).pdf
Introduction to inheritance and different types of inheritance
Inheritance
Inheritance
Inheritance
lecture 6.pdf
Inheritance and Polymorphism in Oops
Inheritance.ppt
Inheritance in C++ (Programming Fundamentals)
Inheritance in c++
oop database doc for studevsgdy fdsyn hdf
Object Oriented Design and Programming Unit-03
Ad

Recently uploaded (20)

PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPT
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
PDF
737-MAX_SRG.pdf student reference guides
PDF
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
Abrasive, erosive and cavitation wear.pdf
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
UNIT - 3 Total quality Management .pptx
PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
PPT on Performance Review to get promotions
PPT
Occupational Health and Safety Management System
PDF
Analyzing Impact of Pakistan Economic Corridor on Import and Export in Pakist...
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PPTX
Current and future trends in Computer Vision.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
III.4.1.2_The_Space_Environment.p pdffdf
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
737-MAX_SRG.pdf student reference guides
UNIT no 1 INTRODUCTION TO DBMS NOTES.pdf
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
Fundamentals of safety and accident prevention -final (1).pptx
Abrasive, erosive and cavitation wear.pdf
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
UNIT - 3 Total quality Management .pptx
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PPT on Performance Review to get promotions
Occupational Health and Safety Management System
Analyzing Impact of Pakistan Economic Corridor on Import and Export in Pakist...
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
Current and future trends in Computer Vision.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...

Inheritance

  • 3. Inheritance Inheritance is a phenomenon of acquiring properties from predecessor. In object oriented systems, it means acquiring the properties of base class to the derived class. Hence the derived class gets specialized with the properties of base class as well as its own properties. It involves the concept of code reusability.
  • 4. Inheritance Inheritance is the process by which new classes called derived classes are created from existing classes called base classes. The derived classes have all the features of the base class and the programmer can choose to add new features specific to the newly created derived class. The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
  • 5. Types of inheritance: single level multi level Base Class A Derived Class B Class A Intermediate derived class B Derived Class C
  • 6. Types of inheritance: Multiple inheritance Hierarchical inheritance. Base class A Base class B Derived Class C Class A Derived class B Derived class C Derived class D
  • 7. Types of inheritance: Hybrid inheritance Class A Intermediate Derived class B Intermediate Derived class C Derived class D Class A Class B Class C Class D
  • 8. FEATURES /ADVANTAGES OF INHERITANCE Reusability of Code Saves Time and Effort Faster development, easier maintenance and easy to extend Capable of expressing the inheritance relationship and its transitive nature which ensures closeness with real world problems
  • 9. Syntax for inheritance: class Base-class-name { ___ // ___ // definition of the class }; class derived-class name : visibility-mode base-class-name { ….. // ….. // members of derived class };
  • 10. Visibility Mode Here visibility mode can be public, private or protected. Private is the default mode. They are visible to member functions within its class. Protected visible to member functions of its own class and derived class. Public visible to all functions in the program.
  • 11. When a class is inherited in any of these visibility modes, the visibility of its members varies.
  • 12. The visibility of inherited members is as shown:
  • 13. Single level In this kind of inheritance, there is a single base class and a single derived class. Eg: class base { // class declaration }; class derived : public base { //derive class declaration }; Here, base class base is inherited into the derived class derived.
  • 14. Multi level Inheritance In this type of inheritance there is more than one levels of inheritance and each derived class serves as the base class for the other derived class. Eg. class base1 { //base1 declaration }; class base2: public base1 { //base2 declaration }; class derived : public base2 { //derived class declaration }; Here, base1 is a base class that is inherited into the derived class base2, which in turn is inherited into the derived class derived.
  • 15. Question to implement A class A{ char name[] ……. int roll_no; …… +void get(); +void show(); }; B class B: public A float m1,m2,m3; { +void take(); ….. +void put(); }; C class C: public B float percentage; { +void cal_per(); ….. +void disp_result(); };
  • 16. Multiple Inheritance – In this kind of Inheritance, there are many base classes and one derived class from these base classes. Eg. Class base1{}; class base2{}; Class base 3{}; class derived : public base1, protected base2, public base3 { // class declaration }; Here, base1, base2 and base3 are base classes that are inherited into the derived class derived.
  • 17. Hierarchical Inheritance In this type of Inheritance, there is one base class and many derived classes from this base class Eg. class base { // class declaration }; class der1 : public base { // class declaration }; class der2 : public base { // class declaration }; Here, der1 and der2 are derived classes that are derived from the base class base.
  • 18. Hybrid Inheritance This is a combination of one or more than one type of Inheritance, like a combination of multiple and multilevel inheritance.
  • 19. Virtual Base Classes In hybrid Inheritance, that is a combination of multiple and hierarchical inheritance, there may arise a case when the features of ancestor base class are inherited via multiple paths in to the ultimate derived class, resulting in deriving duplicate set of members. This duplication of inherited members can be avoided in C++ by inheriting the ancestor base class as virtual base class in to any derived class.
  • 20. Hybrid inheritance Class A Intermediate Derived class B1 Intermediate Derived class B2 Derived class C Class A { // (ancestor) class declaration }; Class B1: virtual public A { //(intermediate derived) class declaration }; Class B2: virtual public A { //(intermediate derived) class declaration }; Class C: public B1, public B2 {// only one copy of the members of class A is inherited };
  • 21. Syntax: Class A { // (ancestor) class declaration }; Class B1: virtual public A { //(intermediate derived) class declaration }; Class B2: virtual public A { //(intermediate derived) class declaration }; Class C: public B1, public B2 {// only one copy of the members of class A is inherited };
  • 22. Constructors in derived class: • Whenever we have parameterized constructors in Base class, it becomes necessary to define a parameterized constructor in derived class so that on the creation of objects of derived class, we can pass the parameters to the base constructors through the derived constructor.
  • 23. The format for the derived class constructor in this case is a bit different as shown below: class derived: <visibility mode> <base class name> { int A, B; //declaration of data members of class Derived Public: derived( arg 1, arg 2, arg 3…. arg N): base 1( arg a,arg b,,…,arg d), base 2( arg e, arg f,… arg k),…base N( arg l, arg m…,arg p) { A= arg 1; B= arg 2; } };
  • 24. Abstract Classes • Is one that is used to create objects. • An abstract class is designed only to act as a base class(to be inherited by other classes).