I am new to object oriented programming in C++ and i've been wondering if there is a way of calling function of child class from parent class
#include <iostream>
using namespace std;
class Shape
{
public:
void display()
{
cout<< "This is a shape\n";
}
};
class Polygon: public Shape
{
public:
void display()
{
Shape::display();
cout<< "Polygon is a shape\n";
}
};
class Rectangle : public Polygon
{
public:
void display()
{
Polygon::display();
cout<< "Rectangle is a polygon\n";
}
};
class Triangle: public Polygon
{
public:
virtual void display()
{
cout<<"Triangle is a polygon\n";
}
};
class Square: public Rectangle
{
public:
void display()
{
Rectangle::display();
cout<<"Square is a rectangle\n";
}
};
int main() {
Square shape;
shape.display();
return 0;
}
I want to call the display function in Triangle class from the Polygon class. Or is there any other way to call the display of every class by using a single object and not changing the given inheritance level?