Inheritance in c++
Type
Inheritance
Inheritance is an important pillar of OOP(Object Oriented
Programming). It is the mechanism in c++ by which one class
is allow to inherit the features(fields and methods) of another
class.
In C++, it is possible to inherit attributes and methods from one
class to another. We group the "inheritance concept" into two
categories:
derived class (child) - the class that inherits from another class
base class (parent) - the class being inherited from
To inherit from a class, use the : symbol.(:)
In the example below, the Car class (child) inherits the
attributes and methods from the Vehicle class (parent):
Understand the need of
Inheritance
Class is used to describe properties and
behavior of an object.
Property Name and Value.
Behavior means action .
1. class parent_class
2. {
3. //class definition of the parent class
4. };
5. class child_class : visibility_mode parent_class
6. {
7. //class definition of the child class
8. };
Car
Propertie Method Price Fuel
s •setPrice() _type
•Price •setFuel()
•Fuel •setEngine(
_type ) Engine
•Engine •setColor() Color
•Color •Setcapacit
•capacity y()
•getPrice() capacity
•getFuel()
•getEngine(
)
•getColor()
•getcapacit
y()
SportsCar
Properties Method Alarm navigator
•Price •setPrice()
•Fuel _type •setFuel() Price Fuel
•Engine •setEngine() _type
•Color •setColor()
•Capacity •Setcapacity A
•Alarm () Engine
•navigator •getPrice() Color
•getFuel()
•getEngine()
•getColor() capacity
•getcapacity
()
class Car
{
//class definition of the parent class
};
class SportsCar : public Car
{
//class definition of the child class
};
What Are Child and Parent classes?
To clearly understand the concept of Inheritance, you must learn
about two terms on which the whole concept of inheritance is based -
Child class and Parent class.
Child class: The class that inherits the characteristics of another class
is known as the child class or derived class. The number of child
classes that can be inherited from a single parent class is based upon
the type of inheritance. A child class will access the data members of
the parent class according to the visibility mode specified during the
declaration of the child class.
Parent class: The class from which the child class inherits its
properties is called the parent class or base class. A single parent class
can derive multiple child classes (Hierarchical Inheritance) or multiple
parent classes can inherit a single base class (Multiple Inheritance).
This depends on the different types of inheritance in C++.
The syntax for defining the child class and parent class in
all types of Inheritance in C++ is given below:
class parent_class
{
//class definition of the parent class
};
class child_class : visibility_mode parent_class
{
//class definition of the child class
};
Syntax Description
parent_class: Name of the base class or the parent
class.
child_class: Name of the derived class or the child
class.
visibility_mode: Type of the visibility mode (i.e.,
private, protected, and public) that specifies how the
data members of the child class inherit from the parent
class.
Visibility Modes
The visibility mode specifies how the
features of the base class will be inherited
by the derived class. There are three types
of visibility modes for all types of
Inheritance in C++:
Public
Private
Protected
Base class Derived class visibility
visibility
Public Private Protected
Private Not Inherited Not Inherited Not Inherited
Protected Protected Private Protected
Public Public Private Protected
•Public: When the member is declared as public, it is accessible to all the functions
of the program.
•Private: When the member is declared as private, it is accessible within the class
only.
•Protected: When the member is declared as protected, it is accessible within its own
class as well as the class immediately derived from it.
#include <iostream>
#include <string>
using namespace std;
// Base class
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut! \n" ;
}
};
// Derived class
class Car: public Vehicle {
public:
string model = "Mustang";
};
int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
It is useful for code reusability: reuse attributes and methods of an existing class
when you create a new class.
Addition of Two Numbers Using Inheritance in C++ ?
#include <iostream>
using namespace std;
class base
{
public:
int a;
void get_a()
{
cout<<"Enter the value of a: "<<endl;
cin>>a;
}
};
class sub : public base
{
int b;
public:
void get_b()
{
cout<<"Enter the value of b: "<<endl;
cin>>b;
}
void display()
{
cout<<"Addition of "<<a<<" and "<<b<<" is "<<(a+b);
}
};
int main()
{
sub s;
s.get_a();
s.get_b();
s.display();
return 0;
}
Inheritance is nothing but a mechanism of inheriting
properties of one class into another class. Parent class /
Base class is the class from which child class or derived
class is formed. That means child class extends
properties of base class. There are five types of
inheritance in c++ programming and they are
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritance
Hybrid Inheritance
Single Inheritance
Single Inheritance is the most primitive among all the types of
inheritance in C++. In this inheritance, a single class inherits
the properties of a base class. All the data members of the base
class are accessed by the derived class according to the
visibility mode (i.e., private, protected, and public) that is
specified during the inheritance.
Syntax
class base_class_1
{
// class definition
};
class derived_class: visibility_mode base_class_1
{
// class definition
};
class subclassname : accessspecifier superclassname
{
//class specific code;
};
#include<iostream>
using namespace std;
class AddData //Base Class
{
protected:
int num1, num2;
public:
void accept()
{
cout<<"\n Enter First Number : ";
cin>>num1;
cout<<"\n Enter Second Number : ";
cin>>num2;
}
};
class Addition: public AddData //Class Addition – Derived Class
{
int sum;
public:
void add()
{
sum = num1 + num2;
}
void display()
{
cout<<"\n Addition of Two Numbers : "<<sum;
}
};
int main()
{
Addition a;
a.accept();
a.add();
a.display();
return 0;
}
C++ Program to add two numbers using multilevel
inheritance
#include<iostream>
using namespace std;
class First {
public: int a, b, s; Enter two numbers:10 20 Sum is:
void input() {
cout << "Enter two numbers:";
cin >> a>>b;
} };
class Second : public First
{ public: void add()
{ s = a + b;
} };
class Third : public Second {
public:
void display()
{ cout << "Sum is:" << s;
} };
int main() {
Third th; th.input();
th.add();
th.display();
return 0; }
Multiple
#include<iostream> class C : public A,public
using namespace std; B
class A { public: void
{ protected:
display()
int a; { std::cout<<"The
public: value of a is :
void get_a(int n) "<<a<<std::endl;
{ std::cout<<"The value
a=n; of b is :
}}; "<<b<<std::endl;
class B{ cout<<"Addition of a
protected: int b; and b is :
public: void get_b(int n) "<<a+b; }};int main()
{ { C c; c.get_a(10);
b=n; c.get_b(20); c.display();
}}; return 0;}
Multilevel inheritance
Multilevel Inheritance in C++ is the process of
deriving a class from another derived class. When one
class inherits another class it is further inherited by
another class. It is known as multi-level inheritance.
For example, if we take Grandfather as a base class
then Father is the derived class that has features of
Grandfather and then Child is the also derived class
that is derived from the sub-class Father which inherits
all the features of Father.