Multi Level Inheritance C++ HackerRank solution

• Updated June 18, 2022

Inheritance is one of the crucial steps to learning Object-oriented programming. The Multi Level Inheritance in c++ is one of four Inheritances, thus by solving the problem you’ll learn how inheritance can be used in code reusability. This tutorial will help you with Multi Level Inheritance C++ HackerRank solution.

multi level inheritance c++ hackerrank solution

Problem statement

This challenge is an extension of a previous challenge named Inheritance-Introduction. We highly recommend solving the Inheritance Introduction before solving this problem…

You can check out the complete problem on Multi Level Inheritance at HackerRank.

Also Read: Hackerrank problem on for loop

Solution

We’ve created a base class named A which is later derived by a class named B. Later in the next step C is derived from class B creating a Multi Level inheritance. An object of the C class is created and print functions are executed respectively.

#include <iostream>
#include <algorithm>
using namespace std;

class A{
    public:
    void prnt(){
    cout << "I am a triangle";}
};

class B:public A{
    public: 
    void prntB(){
        cout << "I am an isosceles triangle\n";
    }
};

class C: public B{
    public: 
    void prntC(){
        cout<< "I am an equilateral triangle\n";
    }
};

int main() {
    C obj;
    obj.prntC();
    obj.prntB();
    obj.prnt();
    
    return 0;
}
Sharing Is Caring:

An aspiring BTech Electronics and Communication student, obsessed with Coding, Gadgets, and Blogging.

Leave a Comment