Multi Level Inheritance in C++ hackerrank solution

Problem statement:

You are given three classes: Triangle, Isosceles, and Equilateral. The Triangle class is the base class, and Isosceles and Equilateral are derived classes.

Your task is to implement these classes in such a way that an object of Equilateral class inherits from Isosceles which in turn inherits from Triangle.

Each class should have a function called isEquilateral that returns a boolean value indicating whether the shape is equilateral or not.

      

Sample Input:

There is no input required for this problem.
-
      

Sample Output:

The output consists of a single line for each test case, which indicates whether the shape is equilateral or not. The output should be either "Equilateral" or "Not Equilateral".
Equilateral

Explanation:

In this example, we don't have any specific input. The program creates an object of Equilateral class, which inherits from Isosceles, and Isosceles inherits from Triangle.

Since an equilateral triangle is a special case of an isosceles triangle, the object is considered to be equilateral.

      
    #include <iostream>
    using namespace std;
    
    class Triangle {
    public:
        bool isEquilateral() {
            return false;
        }
    };
    
    class Isosceles : public Triangle {
    public:
        bool isEquilateral() {
            return false;
        }
    };
    
    class Equilateral : public Isosceles {
    public:
        bool isEquilateral() {
            return true;
        }
    };
    
    int main() {
        Equilateral eq;
        if (eq.isEquilateral()) {
            cout << "Equilateral" << endl;
        } else {
            cout << "Not Equilateral" << endl;
        }
    
        return 0;
    }
    

Code Explanation


We start by including the necessary header file, , for input/output operations.

The Triangle class is defined as the base class. It has a single function, isEquilateral(), which returns false indicating that a generic triangle is not equilateral.

The Isosceles class is defined as a derived class of Triangle. It also has a isEquilateral() function that returns false, indicating that an isosceles triangle is not equilateral.

The Equilateral class is defined as a derived class of Isosceles. It overrides the isEquilateral() function to return true, indicating that an equilateral triangle is indeed equilateral.

In the main() function, we create an object eq of the Equilateral class.

We then check whether eq is equilateral by calling its isEquilateral() function. If it returns true, we print "Equilateral". Otherwise, we print "Not Equilateral".

The solution demonstrates the concept of multi-level inheritance, where a derived class inherits from another derived class. It also shows how the isEquilateral() function can be overridden in each class to provide the appropriate behavior for the specific shape.

I hope this solution meets your requirements and provides a clear understanding of the problem and its solution!