HackerRank C++ Multi Level Inheritance




n the previous problem we learned about Inheritance and how can a derived class object use the member functions of the base class.

In this we will see what multi-level inheritance is. Suppose we have a class A which is the base class and we have a class B which is derived from class A and we have a class C which is derived from class B, we can access the functions of both class A and class B by creating an object for class C. This is called Multi-Level inheritance. (B inherits A and C inherits B.)

Now create a class called Equilateral which inherits from Isosceles and should have a function such that the output is as given below.



  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7. class Triangle
  8. {
  9. public:
  10. void triangle()
  11. {
  12. cout<<"I am a triangle\n";
  13. }
  14. };
  15. class Isosceles : public Triangle
  16. {
  17. public:
  18. void isosceles()
  19. {
  20. cout<<"I am an isosceles triangle\n";
  21. }
  22. };
  23. class Equilateral:public Isosceles
  24. {
  25. public:
  26. void equilateral()
  27. {
  28. cout<<"I am an equilateral triangle\n";
  29. }
  30. };
  31. int main()
  32. {
  33. Equilateral eqr;
  34. eqr.equilateral();
  35. eqr.isosceles();
  36. eqr.triangle();
  37. return 0;
  38. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube