<\/span><\/h2>\n\n\n\nOne 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.Check out the code below:<\/p>\n\n\n\n
class Triangle{\n public:\n void triangle(){\n cout<<\"I am a triangle\\n\";\n }\n};<\/code><\/pre>\n\n\n\nThe class Triangle has a function called triangle(). Now we create a class derived from the base class Triangle called Isosceles.<\/p>\n\n\n\n
class Isosceles : public Triangle{\n public:\n void isosceles(){\n cout<<\"I am an isosceles triangle\\n\";\n }\n};<\/code><\/pre>\n\n\n\nNow we can create a derived class object and use it to access the functions of the base class.<\/p>\n\n\n\n
int main(){\n Isosceles isc;\n isc.isosceles();\n isc.triangle();\n return 0;\n}<\/code><\/pre>\n\n\n\nThis code will print:<\/p>\n\n\n\n
I am an isosceles triangle\nI am a triangle<\/code><\/pre>\n\n\n\nNow write a function in Isosceles class such that the output is as given below.<\/p>\n\n\n\n
<\/span>Sample Output<\/strong><\/span><\/h2>\n\n\n\nI am an isosceles triangle\nIn an isosceles triangle two sides are equal\nI am a triangle<\/code><\/pre>\n\n\n\n<\/span>Solution – Inheritance Introduction in C++<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <cmath>\n#include <cstdio>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\n\nclass Triangle{\n public:\n \tvoid triangle(){\n \t\tcout<<\"I am a triangle\\n\";\n \t}\n};\n\nclass Isosceles : public Triangle{\n public:\n \tvoid isosceles(){\n \t\tcout<<\"I am an isosceles triangle\\n\";\n \t}\n \t\t\/\/Write your code here.\n void description()\n {\n cout<<\"In an isosceles triangle two sides are equal\\n\";\n }\n};\n\nint main(){\n Isosceles isc;\n isc.isosceles();\n \tisc.description();\n isc.triangle();\n return 0;\n}<\/pre>\n\n\n\n