Question

I am learning C++ and I am trying to implement a simple c++ program to create a abstract class as below: Please explain why i am getting this error after compile? Please explain in simple language i am not a pro with terminologies and still learning OOPS concepts. Thank You in advance (code and error below)

CODE:

/*
 * shape.cpp
 *
 *  Created on: Mar 9, 2014
 *      Author: Drix
*/


#include <iostream>
using namespace std;

class shape{
public:
virtual void Draw(void)=0;
};


class circle:public shape{
public:
void Draw(double radius){
    radii = radius;
    cout << "The radius of the circle is "  << radii<<endl;
}
private:
double radii;
};

class square:public shape{
public:
void Draw(double side){
    s = side;
    cout << "The length of side of sqare is "  << s<<endl;
}
private:
double s;
};


int main(){

cout <<"Welcome to shape drwaing program"<<endl;
cout <<"Enter 1 to draw a sqare or 2 to draw a circle"<<endl;
int input;

cin>>input;
if(input == 1)
{
    cout << "Please enter the radius of the circle: ";
    double radius;
    cin >> radius;
    circle *p = new circle;
    p->Draw(radius);
}
if(input == 2)
    {
        cout << "Please enter the length of the side of a square: ";
        double side;
        cin >> side;
        square *t = new square;
        t->Draw(side);
    }
 }

ERROR:

10:58:15 **** Incremental Build of configuration Debug for project Shape ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o shape.o "..\\shape.cpp" 
..\shape.cpp: In function 'int main()':
..\shape.cpp:51:19: error: cannot allocate an object of abstract type 'circle'
..\shape.cpp:18:7: note:   because the following virtual functions are pure within 'circle':
..\shape.cpp:14:15: note:   virtual void shape::Draw()
..\shape.cpp:59:20: error: cannot allocate an object of abstract type 'square'
..\shape.cpp:28:7: note:   because the following virtual functions are pure within 'square':
..\shape.cpp:14:15: note:   virtual void shape::Draw()

10:58:16 Build Finished (took 884ms)
Was it helpful?

Solution

The problem is the following: in the class shape you declared draw as taking no parameter:

virtual void Draw(void)=0;

whereas the subclass circle and square has draw expecting a double:

void Draw(double radius)

In circle, I think radius (together with probably something like center) should be passed to the constructor and draw should accept nothing. For example:

class circle:public shape{
private:
    double radii;
public:
    circle(double radius) radii(radius) {};
    void Draw(){
        cout << "The radius of the circle is "  << radii<<endl;
    }
};

Then you use it as

circle *p = new circle(radius);
p->Draw();

Or if you don't need dynamic allocation:

circle c(radius);
c.Draw()

Of course class square has the same problem.

OTHER TIPS

The argument of the Draw method doesn't match.

in shape:

virtual void Draw(void)=0;

while in circle and square:

void Draw(double radius)

If you want to have virtual method the arguments must match.

This method in Shape should be (do not need the void)

class shape{ 
public: 
    virtual void Draw()=0;
};

When you declare Circle you need a Draw method (i.e one without the radius bit)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top