I'm working on a university assignment about finding the array of different shapes using OOP. I have created all of my shape classes so they derive from my main Shape Class. Shape class is being used like an interface, so that all the shape classes derived from the shape class have to have a calculate area function, etc. I want to create an array of different shapes. I have declared an array with a type of shape, which is the parent class and i want to add new instances of each different shape, circle, square, rectangle and triangle to the array list so that all the information about each shape is stored in one array. I have the follow code but i have an error on the line aShapes[i] = square; saying that shape is an inaccessible base of square.
If anyone could help out that would be great.
Thanks in advanced.
Here is my code
#include <cstdlib>
#include <iostream>
#define M_PI 3.14159265358979323846
using namespace std;
class Shape{
public:
string sName;
float nArea;
void fnAddData();
float fnCalculateArea();
};
class Square : private Shape {
private:
float nSide;
void fnAddData()
{
cout << "Please enter the length of a side: ";
cin >> nSide;
}
float fnCalculateArea(float side)
{
return (side * side);
}
public:
Square()
{
sName = "Square";
fnAddData();
nArea = fnCalculateArea(nSide);
}
};
Shape aShapes[5];
/*
*
*/
int main(int argc, char** argv)
{
int decision;
Square square;
for (int i = 0; i < 5; i++)
{
cout << "Shape number";
cin >> decision;
switch (decision)
{
case 1:
aShapes[i] = square;
}
}
return 0;
}