0% found this document useful (0 votes)
0 views3 pages

Inheritance Updated

Inheritance is a key feature of Object Oriented Programming that allows a subclass to derive properties from a superclass, reducing code duplication and increasing reusability. There are different modes of inheritance (public, protected, private) and types (single, multiple, multilevel, hierarchical, hybrid) in C++. The document provides examples and explanations of how inheritance works in C++ programming.

Uploaded by

J
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

Inheritance Updated

Inheritance is a key feature of Object Oriented Programming that allows a subclass to derive properties from a superclass, reducing code duplication and increasing reusability. There are different modes of inheritance (public, protected, private) and types (single, multiple, multilevel, hierarchical, hybrid) in C++. The document provides examples and explanations of how inheritance works in C++ programming.

Uploaded by

J
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Inheritance

The capability of a class to derive properties and characteristics from another


class is called Inheritance. Inheritance is one of the most important feature
of Object Oriented Programming.
Sub Class: The class that inherits properties from another class is called
Sub class or Derived Class.
Super Class:The class whose properties are inherited by sub class is called
Base Class or Super class.

You can clearly see that above process results in duplication of same code 3
times. This increases the chances of error and data redundancy. To avoid
this type of situation, inheritance is used. If we create a class Vehicle and
write these three functions in it and inherit the rest of the classes from the
vehicle class, then we can simply avoid the duplication of data and increase
re-usability.
class subclass_name : access_mode base_class_name
{
//body of subclass
};
Note: A derived class doesn’t inherit access to private data members.
However, it does inherit a full parent object, which contains any private
members which that class declares.
#include <bits/stdc++.h>
using namespace std;

//Base class
class Parent
{
public:
int id_p;
};

// Sub class inheriting from Base Class(Parent)


class Child : public Parent
{
public:
int id_c;
};

//main function
int main()
{

Child obj1;
// An object of class child has all data members and member functions of
class parent

obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is " << obj1.id_c << endl;
cout << "Parent id is " << obj1.id_p << endl;

return 0;
}

Output
Child id is 7
Parent id is 91

Modes of Inheritance
1. 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.
2. Protected mode: 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.
3. Private mode: 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.

Types of Inheritance in C++


1. Single Inheritance: In single inheritance, a class is allowed to inherit from
only one class. i.e. one sub class is inherited by one base class only.
2. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a
class can inherit from more than one classes. i.e one sub class is inherited
from more than one base classes.
Multilevel Inheritance: In this type of inheritance, a derived class is created
from another derived class.
Hierarchical Inheritance: In this type of inheritance, more than one sub
class is inherited from a single base class. i.e. more than one derived class
is created from a single base class.
Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by
combining more than one type of inheritance. For example: Combining
Hierarchical inheritance and Multiple Inheritance.

You might also like