SlideShare a Scribd company logo
5
Most read
6
Most read
8
Most read
Inheritance in C++
Dr.Mirza Waseem Hussain 1
By: Dr. Mirza Waseem Hussain
Lecturer Computer Science
Table of Contents
1. Concept of Inheritance
2. Advantages of Inheritance
3. General Syntax of Inheritance
4. Inheritance Access specifier/ Visibility modes.
๏ถPublic mode.
๏ถProtected mode.
๏ถPrivate mode.
Dr.Mirza Waseem Hussain 2
1. Concept of inheritance:
๏ƒ˜Inheritance is one of the feature of Object Oriented Programming
System(OOPs), it allows the child class to acquire the properties (the data
members) and functionality (the member functions) of parent class.
๏ƒ˜Inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically.
๏ƒ˜The technique of deriving a new class from an old one is called inheritance.
๏ƒ˜Inheritance is a mechanism in which one class acquires the property of
another class.
๏‚ง For example, a child inherits the traits of his/her parents.
๏ƒ˜It allows user to create a new class (derived class, subclass, child class) from
an existing class(base class, super class, parent class).
๏‚ง The derived class inherits all the features from the base class and can have additional
features of its own.
Dr.Mirza Waseem Hussain 3
2. Advantages of inheritance:
a) Reusability
๏ถWhen child class inherits the properties and functionality of parent class, we need not to
write the same code again in child class.
b) Readability
๏ถ Itโ€™s easier to reuse the code, makes us write the less code and the code becomes much
more readable.
3. General Syntax of inheritance:
class base_class1{ //Member functions of base class };
class base_classN{ //Member functions of base class };
derived_class:: visibility_mode base_class1, visibility_mode base_class2, โ€ฆ.,
visibility_mode base_classN { โ€ฆโ€ฆ. //Member functions of derived
class.
};
Dr.Mirza Waseem Hussain 4
Note: A class that inherits another class is known
as child class, it is also known as derived class or
subclass.
Note: The class that is being inherited by other
class is known as parent class, super class or base
class.
4. Inheritance Access specifier/ Visibilty modes:
๏ƒ˜Access Specifiers defines the visibility of the Property /Fields/ Variable/ Methods of a
class.
๏ƒ˜Access modifiers (or access specifiers) are keywords in object-oriented languages that
set the accessibility of classes, methods, and other members.
๏ƒ˜There are three types of Access specifiers in C++ to define the visibility modes of
inheritance.
โ€ข Private.
โ€ข Public .
โ€ข Protected.
A. Public mode:
๏ƒ˜If we derive a sub class from a public base class. Then the public member of the base
class will become public in the derived class and protected members of the base class
will become protected in derived class.
syntax:
class derive_class::public base_class{ โ€ฆ //Member functions of derived class
};
Dr.Mirza Waseem Hussain 5
B. Protected:
๏ƒ˜If we derive a sub class from a Protected base class. Then both public member
and protected members of the base class will become protected in derived class.
syntax:
class derive_class::protected base_class{ โ€ฆ //Member functions of derived class
};
C. Private:
๏ƒ˜If we derive a sub class from a Private base class. Then both public member and
protected members of the base class will become Private in derived class.
syntax:
class derive_class::private base_class{ โ€ฆ //Member functions of derived class
};
Dr.Mirza Waseem Hussain 6
Note:The private members in the base class cannot be directly accessed in the derived class, while
public and protected members can be directly accessed.
๏ƒ˜Table 1 summarizes the visibility modes of inheritance.
Dr.Mirza Waseem Hussain 7
Base
Class
Derived
Class
Public
Public
Protected
Protected
Private
Public Protected
Private
Protected Protected Private
Private
Not Accessible Not Accessible Not Accessible
Table 1: Visibility modes of inheritance
Max(L1,L2)
L2
L1
L2
L3
L1 L2 L3
Max(L2,L3)
L3
// C++ Implementation to show that a derived class
// doesnโ€™t inherit access to private data members.
// However, it does inherit a full parent object
class A
{
public:
int x;
protected:
int y;
private:
int z;
};
class B : public A
{
// x is public
// y is protected
// z is not accessible from B
};
class C : protected A
{
// x is protected
// y is protected
// z is not accessible from C
};
class D : private A // 'private' is default for classes
{
// x is private
// y is private
// z is not accessible from D
};
Dr.Mirza Waseem Hussain 8
Dr.Mirza Waseem Hussain 9
Dr.Mirza Waseem Hussain 10
Source:
โ€ข https://www.javatpoint.com/cpp-inheritance
โ€ข https://www.w3schools.in/cplusplus-tutorial/inheritance/
โ€ข https://www.tutorialcup.com/cplusplus/inheritance.htm
โ€ข https://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/
โ€ข https://beginnersbook.com/2017/08/cpp-inheritance/
โ€ข https://www.programiz.com/cpp-programming/inheritance
โ€ข https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
โ€ข https://www.geeksforgeeks.org/inheritance-in-c/
Dr.Mirza Waseem Hussain 11
Dr. Mirza Waseem Hussain 12

More Related Content

PPTX
classes and objects in C++
PPT
Operator Overloading
PPTX
Scope rules : local and global variables
PDF
Python programming : Classes objects
PPTX
Exception handling c++
PPTX
Inheritance in java
ย 
PPTX
array of object pointer in c++
classes and objects in C++
Operator Overloading
Scope rules : local and global variables
Python programming : Classes objects
Exception handling c++
Inheritance in java
ย 
array of object pointer in c++

What's hot (20)

PPTX
Inheritance in c++
PPTX
[OOP - Lec 08] Encapsulation (Information Hiding)
PPT
Function overloading(c++)
PPTX
Inheritance in c++
PPTX
Data members and member functions
PPTX
Characteristics of OOPS
PPT
Class and object in C++
PPTX
Stream classes in C++
PPTX
Templates in c++
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
PPTX
Function overloading and overriding
PDF
C++ Files and Streams
PPTX
Intro to c++
PPTX
Python-Inheritance.pptx
PDF
Function overloading ppt
PPTX
Inheritance in java
PPTX
Chapter 07 inheritance
PPTX
Mandatory access control for information security
PPTX
Procedural vs. object oriented programming
Inheritance in c++
[OOP - Lec 08] Encapsulation (Information Hiding)
Function overloading(c++)
Inheritance in c++
Data members and member functions
Characteristics of OOPS
Class and object in C++
Stream classes in C++
Templates in c++
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Function overloading and overriding
C++ Files and Streams
Intro to c++
Python-Inheritance.pptx
Function overloading ppt
Inheritance in java
Chapter 07 inheritance
Mandatory access control for information security
Procedural vs. object oriented programming
Ad

Similar to Inheritance in c++ part1 (20)

PPT
Inheritance in C++
PPTX
Inheritance_with_its_types_single_multi_hybrid
PDF
Chapter25 inheritance-i
PPTX
Introduction to Inheritance
PPTX
00ps inheritace using c++
PPTX
Lecture 5.mte 407
PDF
lecture 6.pdf
PPT
Inheritance
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
PDF
chapter-10-inheritance.pdf
PPT
Inheritance, Object Oriented Programming
PDF
Inheritance
PPTX
Mca 2nd sem u-3 inheritance
PPTX
Inheritance
PPTX
inheritance_OOPC_datastream.ppttttttttttttttx
PDF
Inheritance
PPT
inheritance
PPT
Inheritance : Extending Classes
PPT
11 Inheritance.ppt
PPT
Inheritance in C++
Inheritance in C++
Inheritance_with_its_types_single_multi_hybrid
Chapter25 inheritance-i
Introduction to Inheritance
00ps inheritace using c++
Lecture 5.mte 407
lecture 6.pdf
Inheritance
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
chapter-10-inheritance.pdf
Inheritance, Object Oriented Programming
Inheritance
Mca 2nd sem u-3 inheritance
Inheritance
inheritance_OOPC_datastream.ppttttttttttttttx
Inheritance
inheritance
Inheritance : Extending Classes
11 Inheritance.ppt
Inheritance in C++
Ad

More from Mirza Hussain (6)

PDF
Constructive Teaching & Learning with Technology( CTLT.pdf
PPTX
Generations of computers
PPTX
History of computers
PPTX
Destructors
PPTX
Constructors
PDF
Library function in c++ specially designed for clas 11th students
Constructive Teaching & Learning with Technology( CTLT.pdf
Generations of computers
History of computers
Destructors
Constructors
Library function in c++ specially designed for clas 11th students

Recently uploaded (20)

PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Internet of Things (IOT) - A guide to understanding
DOCX
573137875-Attendance-Management-System-original
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Geodesy 1.pptx...............................................
PPT
Project quality management in manufacturing
PDF
composite construction of structures.pdf
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Arduino robotics embedded978-1-4302-3184-4.pdf
CYBER-CRIMES AND SECURITY A guide to understanding
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
CH1 Production IntroductoryConcepts.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Embodied AI: Ushering in the Next Era of Intelligent Systems
Lesson 3_Tessellation.pptx finite Mathematics
Internet of Things (IOT) - A guide to understanding
573137875-Attendance-Management-System-original
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Geodesy 1.pptx...............................................
Project quality management in manufacturing
composite construction of structures.pdf
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...

Inheritance in c++ part1

  • 1. Inheritance in C++ Dr.Mirza Waseem Hussain 1 By: Dr. Mirza Waseem Hussain Lecturer Computer Science
  • 2. Table of Contents 1. Concept of Inheritance 2. Advantages of Inheritance 3. General Syntax of Inheritance 4. Inheritance Access specifier/ Visibility modes. ๏ถPublic mode. ๏ถProtected mode. ๏ถPrivate mode. Dr.Mirza Waseem Hussain 2
  • 3. 1. Concept of inheritance: ๏ƒ˜Inheritance is one of the feature of Object Oriented Programming System(OOPs), it allows the child class to acquire the properties (the data members) and functionality (the member functions) of parent class. ๏ƒ˜Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. ๏ƒ˜The technique of deriving a new class from an old one is called inheritance. ๏ƒ˜Inheritance is a mechanism in which one class acquires the property of another class. ๏‚ง For example, a child inherits the traits of his/her parents. ๏ƒ˜It allows user to create a new class (derived class, subclass, child class) from an existing class(base class, super class, parent class). ๏‚ง The derived class inherits all the features from the base class and can have additional features of its own. Dr.Mirza Waseem Hussain 3
  • 4. 2. Advantages of inheritance: a) Reusability ๏ถWhen child class inherits the properties and functionality of parent class, we need not to write the same code again in child class. b) Readability ๏ถ Itโ€™s easier to reuse the code, makes us write the less code and the code becomes much more readable. 3. General Syntax of inheritance: class base_class1{ //Member functions of base class }; class base_classN{ //Member functions of base class }; derived_class:: visibility_mode base_class1, visibility_mode base_class2, โ€ฆ., visibility_mode base_classN { โ€ฆโ€ฆ. //Member functions of derived class. }; Dr.Mirza Waseem Hussain 4 Note: A class that inherits another class is known as child class, it is also known as derived class or subclass. Note: The class that is being inherited by other class is known as parent class, super class or base class.
  • 5. 4. Inheritance Access specifier/ Visibilty modes: ๏ƒ˜Access Specifiers defines the visibility of the Property /Fields/ Variable/ Methods of a class. ๏ƒ˜Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. ๏ƒ˜There are three types of Access specifiers in C++ to define the visibility modes of inheritance. โ€ข Private. โ€ข Public . โ€ข Protected. A. Public mode: ๏ƒ˜If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class. syntax: class derive_class::public base_class{ โ€ฆ //Member functions of derived class }; Dr.Mirza Waseem Hussain 5
  • 6. B. Protected: ๏ƒ˜If we derive a sub class from a Protected base class. Then both public member and protected members of the base class will become protected in derived class. syntax: class derive_class::protected base_class{ โ€ฆ //Member functions of derived class }; C. Private: ๏ƒ˜If we derive a sub class from a Private base class. Then both public member and protected members of the base class will become Private in derived class. syntax: class derive_class::private base_class{ โ€ฆ //Member functions of derived class }; Dr.Mirza Waseem Hussain 6 Note:The private members in the base class cannot be directly accessed in the derived class, while public and protected members can be directly accessed.
  • 7. ๏ƒ˜Table 1 summarizes the visibility modes of inheritance. Dr.Mirza Waseem Hussain 7 Base Class Derived Class Public Public Protected Protected Private Public Protected Private Protected Protected Private Private Not Accessible Not Accessible Not Accessible Table 1: Visibility modes of inheritance Max(L1,L2) L2 L1 L2 L3 L1 L2 L3 Max(L2,L3) L3
  • 8. // C++ Implementation to show that a derived class // doesnโ€™t inherit access to private data members. // However, it does inherit a full parent object class A { public: int x; protected: int y; private: int z; }; class B : public A { // x is public // y is protected // z is not accessible from B }; class C : protected A { // x is protected // y is protected // z is not accessible from C }; class D : private A // 'private' is default for classes { // x is private // y is private // z is not accessible from D }; Dr.Mirza Waseem Hussain 8
  • 11. Source: โ€ข https://www.javatpoint.com/cpp-inheritance โ€ข https://www.w3schools.in/cplusplus-tutorial/inheritance/ โ€ข https://www.tutorialcup.com/cplusplus/inheritance.htm โ€ข https://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/ โ€ข https://beginnersbook.com/2017/08/cpp-inheritance/ โ€ข https://www.programiz.com/cpp-programming/inheritance โ€ข https://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm โ€ข https://www.geeksforgeeks.org/inheritance-in-c/ Dr.Mirza Waseem Hussain 11
  • 12. Dr. Mirza Waseem Hussain 12