Inheritance in C++

Inheritance in C++

The derived class is the specialized class for the base class.

Supported Inheritance in C++

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance
nheritance in c++ AndroWep-Tutorials
Inheritance in c++ AndroWep-Tutorials

Derived Class and Base Class (Follow This link)

Access specifiers c++ (Follow This link)

Single Inheritance

Single inheritance enables a derived class to inherit properties and behavior from a single parent class. It allows a derived class to inherit the properties and behavior of a base class, thus enabling code reusability as well as adding new features to the existing code.

Single
#include <iostream>  
using namespace std;  
 class Account {  
   public:  
   float salary = 90000;   
 };  
   class Programmer: public Account {  
   public:  
   float bonus = 25000;    
   };       
int main(void) {  
     Programmer p1;  
     cout<<"Salary: "<<p1.salary<<endl;    
     cout<<"Bonus: "<<p1.bonus<<endl;    
    return 0;  
}  
Salary: 90000
Bonus: 25000

Multilevel Inheritance

Multilevel  in C++ Programming. … When a class is derived from a class which is also derived from another class, i.e. a class having more than one parent classes, such inheritance is called Multilevel Inheritance. The level of inheritance can be extended to any number of level depending upon the relation.

Multilevel inhearitance
#include <iostream>

using namespace std;
class Animal {
    public:
        void eat() {
            cout << "jumping..." << endl;
        }
};
class Dog: public Animal {
    public: void bark() {
        cout << "singing..." << endl;
    }
};
class BabyDog: public Dog {
    public: void weep() {
        cout << "going...";
    }
};
int main(void) {
    BabyDog d1;
    d1.eat();
    d1.bark();
    d1.weep();
    return 0;
}
//Output
jumping...
singing...
going...

Multiple Inheritance

Multiple is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited.

Multiple Inheritance in C++ AndroWep-Tutorials
Multiple Inheritance in C++ AndroWep-Tutorials
#include <iostream>

using namespace std;
class A {
    protected:
        int a;
    public:
        void get_a(int n) {
            a = n;
        }
};

class B {
    protected:
        int b;
    public:
        void get_b(int n) {
            b = n;
        }
};
class C: public A, public B {
    public: void display() {
        std::cout << "The value of a is : " << a << std::endl;
        std::cout << "The value of b is : " << b << std::endl;
        cout << "Addition of a and b is : " << a + b;
    }
};
int main() {
    C c;
    c.get_a(10);
    c.get_b(20);
    c.display();

    return 0;
}
The value of a is : 20
The value of b is : 40
Addition of a and b is : 60

Hybrid Inheritance

Hybrid Inheritance in c++ AndroWep-Tutorials
Hybrid Inheritance in c++ AndroWep-Tutorials
#include <iostream>

using namespace std;
class A {
    protected:
        int a;
    public:
        void get_a() {
            std::cout << "Enter the value of 'a' : " << std::endl;
            cin >> a;
        }
};

class B: public A {
    protected: int b;
    public: void get_b() {
        std::cout << "Enter the value of 'b' : " << std::endl;
        cin >> b;
    }
};
class C {
    protected:
        int c;
    public:
        void get_c() {
            std::cout << "Enter the value of c is : " << std::endl;
            cin >> c;
        }
};

class D: public B, public C {
    protected: int d;
    public: void mul() {
        get_a();
        get_b();
        get_c();
        std::cout << "Multiplication of a,b,c is : " << a * b * c << std::endl;
    }
};
int main() {
    D d;
    d.mul();
    return 0;
}
Enter the value of 'a' :
10              
Enter the value of 'b' :    
20      
Enter the value of c is :   
30  
Multiplication of a,b,c is : 6000

Hierarchical Inheritance

Inheritance is the process of inheriting properties of objects of one class by objects of another class. … When more than one classes are derived from a single base class, such inheritance is known as Hierarchical Inheritance, where features that are common in lower level are included in parent class.

Hierarchical Inheritance in c++ AndroWep-Tutorials
Hierarchical Inheritance in c++ AndroWep-Tutorials
#include <iostream>

using namespace std;
class Shape // Declaration of base class.  
{
    public:
        int a;
    int b;
    void get_data(int n, int m) {
        a = n;
        b = m;
    }
};
class Rectangle: public Shape // inheriting Shape class  
{
    public: int rect_area() {
        int result = a * b;
        return result;
    }
};
class Triangle: public Shape // inheriting Shape class  
{
    public: int triangle_area() {
        float result = 0.5 * a * b;
        return result;
    }
};
int main() {
    Rectangle r;
    Triangle t;
    int length, breadth, base, height;
    std::cout << "Enter the length and breadth of a rectangle: " << std::endl;
    cin >> length >> breadth;
    r.get_data(length, breadth);
    int m = r.rect_area();
    std::cout << "Area of the rectangle is : " << m << std::endl;
    std::cout << "Enter the base and height of the triangle: " << std::endl;
    cin >> base >> height;
    t.get_data(base, height);
    float n = t.triangle_area();
    std::cout << "Area of the triangle is : " << n << std::endl;
    return 0;
}
Enter the length and breadth of a rectangle:
23  
20  
Area of the rectangle is : 460          
Enter the base and height of the triangle:  
2   
5
Area of the triangle is : 5