I have a parent Shape class that has a pure virtual method Area() which prints the area of a given shape and 2 child classes that inherit from this parent class, Square and Circle. Where should I include Square and Circle?
Shape.cpp
#include <string>
class Shape
{
public:
virtual void Area() = 0;
};
class Square : public Shape
{
private:
int height, width;
public:
Square()
{
height = 5;
width = 5;
}
// Inherited via Shape
virtual void Area() override
{
printf("%i\n", height * width);
}
};
class Circle : public Shape
{
private:
int radius;
public:
Circle()
{
radius = 10;
}
// Inherited via Shape
virtual void Area() override
{
double x = pow(radius, 2);
printf("%lf\n", 3.14 * x);
}
};
ShapeTest.cpp
#include <iostream>
#include "Shape.cpp"
int main()
{
Square square{};
Circle circ{};
square.Area();
circ.Area();
}
My preferred option would to include them in their own separate files, something like
- Shape.cpp
- Square.cpp
- Circle.cpp
but I seem to run into link issues with this, so I when I include them under the one file it works.
This would obviously get unwieldy as the number of child classes increases, so what is the correct way to do this?