0

i am getting an error message "Allocating an object of abstract class type 'Shape'". Circle(double r):Shape("Circle").

#include <iostream>
using namespace std;

class Shape{
    char name[20];
    Shape(char *){};
    virtual double getPerimeter()=0;
    virtual double getArea()=0;
};

class Circle : public Shape{
    double rad;
    static const double PI;
public:
    Circle(double r):Shape("Circle"){
        rad = r;
    }
    double getRadius(){
        return rad;
    }
    double getPerimeter(double rad){
        return 2 * PI * rad;
    }
    double getArea(double rad){
        return PI * (rad * rad);
    }
};

class Square : public Shape{
    double side;
public:
    Square(double s):Shape("Square"){
        side = s;
    }
    double getPerimeter(double side){
        return side * 4;
    }
    double getArea(double side){
        return side * side;
    }

};

const double Circle::PI = 3.1415;

int main(){

}

Is this a problem with the constructor in Class Circle/Square? Im not sure and a bit lost. At this point any hints of what i should research to find the answer would be great.

Thanks!

Celery
  • 109
  • 12

2 Answers2

1

To answer your question :

Your constructor Shape(char *){}; is private. You cannot reuse it in your derived class.

But there are several problems in your code here are some tips : use member initialization list, use std::string (not char*), use const correctness, do not reinvent the wheel use M_PI from <cmath>

A working and better code :

#include <iostream>
#include <string>
#include <cmath>

class Shape {
public:
  Shape(std::string n) : name(n) {}
  // virtual double getPerimeter() const = 0;
  virtual double getArea() const = 0;
protected:
  std::string name;
};

class Circle : public Shape {
public:
  Circle(double r) : Shape("Circle"), rad(r) {}
  double getPerimeter() const { return  2 * M_PI * rad; }
  double getArea() const { return M_PI * rad * rad; } ;
private:
  double rad;
};

class Square : public Shape {
public:
  Square(double s) : Shape("Square"), side(s) {}
  double getPerimeter() const { return  4 * side; }
  double getArea() const { return side * side; } ;
private:
  double side;
};

int main() {
    Square square(4.0);
    std::cout << square.getPerimeter() << " " << square.getArea() << '\n';
    Circle circle(1.0);
     std::cout << circle.getPerimeter() << " " << circle.getArea() << '\n';
}

Output :

16 16
6.28319 3.14159

coincoin
  • 4,595
  • 3
  • 23
  • 47
0

The signatures of getArea and getPerimeter differs from those of the base class. Use the same signature, otherwise you're just hiding the base methods, not overriding them.

You don't need to pass in the dimensions, you already have them as members.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Good point but actually this is not the cause of this error. – coincoin Nov 21 '15 at 02:19
  • @coincoin for that error, yes it is. The class is abstract because the pure virtual methods aren't implemented in the derived class. If not this, what is the cause? I read your answer and you also provide this solution, in addition to others which wouldn't cause this error (the private constructor would cause a compiler error, so I doubt that's his actual code), or would cause no error at all (your other tips). – Luchian Grigore Nov 23 '15 at 17:28