際際滷

際際滷Share a Scribd company logo
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
 Its 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
// doesnt 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

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 Its 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 // doesnt 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