Why does my code return error? I've defined a parent class called Shape and two derived classes. I'm trying to store objects defined by the classes and store them inside a list.
# include <iostream>
using namespace std;
class Shape{
public:
virtual double area(const double &height, const double &weight) const = 0;
};
class Triangle: public Shape{
public:
double height, weight;
Triangle(double height, double weight): height(height), weight(weight){}
double area(){
return (height*weight)/2;
}
};
class Rectangle: public Shape{
public:
double height, weight;
Rectangle(double height, double weight): height(height), weight(weight){}
double area(){
return height*weight;
}
};
Shape *shapes[3];
shapes[0] = new Triangle(2, 1);
shapes[1] = new Rectangle(3, 2);
shapes[2] = new Rectangle(5, 2);
double * show(Shape *shapes){
double arr[3];
for (int i=0; i < 3; i++){
arr[i] = shapes[i].area();
}
return arr;
}
int main(){
double arr[3] = show(shapes);
cout << arr[0] << endl;
cout << arr[1] << endl;
cout << arr[2] << endl;
}
But I receive these two errors:
error: 'shapes' does not name a type Shapes[0] = new Trangle(2,1);
error: cannot convert 'shape**' to 'shape*' double arr[3] = show(shapes);