Feb 8, 2021 at 3:21am UTC
HEY! I have the following code(bottom) which is 90% complete ..I'm trying to calculate shapes' areas using inheritance and polymorphic pointer(in main).
there's a problem with my parent class (shape), when I try to run the code it gives me 3 error, one for each class
"undefined reference to `Shape::Shape()'"
can someone figure out how to fix this? please
#include <iostream>
using namespace std;
class Shape{
public:
virtual double area(){
return 0;
}
Shape ();
~Shape(){
cout <<"object shape is destroyed\n";
}
};
class Rectangle : public Shape
{
double width, hight;
public:
double area(){
return width*hight;
}
Rectangle(double w, double h):Shape(){
width=w;
hight=h;
cout <<"inside Rectangle consructor\n";
}
};
class Square : public Shape
{
double Side;
public:
double area(){
return Side*Side;
}
Square(double s):Shape(){
Side=s;
cout <<"inside Square consructor\n";
}
};
class Circle : public Shape
{
float pi;
float radius;
public:
double area(){
return pi * (radius*radius);
}
Circle(float p, float r):Shape(){
pi=p;
radius=r;
}
};
int main()
{
Shape *a;
a = new Rectangle(4,5);
cout<<a->area()<<endl;
a = new Square(3);
cout<<a->area()<<endl;
a = new Circle (3.14,2.4);
cout<<a->area()<<endl;
delete[] a;
return 0;
}
Last edited on Feb 8, 2021 at 3:42am UTC