Inheritance in c++
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
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
•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.
#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