Shapes using inheritance and polymorphic

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
> "undefined reference to `Shape::Shape()'"

So define it. Or declare it as defaulted and let the compiler generate it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Shape
{
    public:
        virtual double area() 
        {
            return 0;
        }
        
        Shape () = default ;

        ~Shape()
        {
            cout <<"object shape is destroyed\n";
        }
};


Ideally, make area() const correct: virtual double area() const
See: https://isocpp.org/wiki/faq/const-correctness#const-member-fns
thank you <3
Topic archived. No new replies allowed.