The title sucks I know. The problem here is that I have a 2D and 3D shape class that inherits from a shape class
class Shape {
public:
virtual double area() = 0;//Calculates the area of some kind of shape
~Shape() { delete this; }
};
class Shape2D :public Shape {
public:
virtual double perimeter() = 0;//Calculates the perimeter of some kind of 2D-shape
~Shape2D() {};
};
class Shape3D :public Shape {
public:
virtual double volume() = 0;//Calculates the volume of some kind of 3D-shape, area function will return surface area
~Shape3D() {};
};
It is decided that all shapes will default have an area. In 2D shapes, it'll have a virtual perimeter method and also and area from Shape. In 3D shapes, it'll have a volume and the vitual area method will be treated as a surface area.
The way that I've gone about is that within a menu that can choose 2d or 3d shapes: within the 2d menu, I initiate:
Shape2D * s = nullptr;
and within the 3d menu, I'll initiate:
Shape3D * s = nullptr;
and then to display whatever information, I use the methods:
void displayShape2D(Shape2D *)
and
void displayShape3D(Shape3D *)
However, the way that I want to go about it is to declare:
Shape *s = nullputr;
at the beginning of main and then at whatever shape the user chooses, I can just set:
s= new Triangle()
or
s = new cube();
The initialization works but when I try to make a display method, that's where I run into a problem. I want to be able to do:
displayShape(Shape *s)
When given a 2d shape and within the method I try:
cout <<s->perimeter();
It'll say that the perimeter member is not within the shape class. The problem then is trying to be able to determine if a the shape is 2d or 3d and then display the area and perimeter for 2d or surface area and volume for 3d. Is this possible or is creating the shape types within the dedicated menu and then having dedicated display methods the only way out?