Inheritance Introduction C++ HackerRank solution

• Updated June 23, 2022

Inheritance in the Object oriented programming brings the concept of code reusability. By mastering it you’ll come close to mastering the OOP concept in c++. This tutorial will help you with the Inheritance Introduction C++ HackerRank solution.

Inheritance Introduction c++ hackerrank solution

Problem statement

One of the important topics of Object Oriented Programming is Inheritance. Inheritance allows us to define a class in terms of another class, which allows us in the reusability of the code…

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

Also Read: Hackerrank problem on Multi Level Inheritance

Solution

A class named Triangle is created which simply prints out a statement. Later another class named Icosceles is derived from the previous class which prints another statement. An object from the derived class is capable of calling the previous class functions without directly owning them.

#include <iostream>
using namespace std;

class Triangle{
    public:
        void triangle(){
            cout<<"I am a triangle\n";
        }
};

class Isosceles : public Triangle{
    public:
        void isosceles(){
            cout<<"I am an isosceles triangle\n";
        }
};

int main() {
    Isosceles isc;
    isc.isosceles();
    cout << "In an isosceles triangle two sides are equal\n";
    isc.triangle();
    return 0;
}
Sharing Is Caring:

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

Leave a Comment