2

Does anyone know what i am doing wrong and explain to me why it wouldnt let me declare anything as a circle? main

int main(void){
...
    circle c;
...
}

circle.h

#include <string>
#include <iostream>
using namespace std;

class circle : public shape {

    double diameter, circum, radius;

public:

    virtual void draw(){
        cout<< "Circle"<< endl;
    }
};

shape.h

#include <string>
#include <iostream>

using namespace std;

class shape{

public:
    virtual void draw() const = 0;
};
0

4 Answers 4

8
virtual void draw() const { 
    cout<< "Circle"<< endl; 
} 

You should add the keyword const like in the example above.

4

The draw function in your shape class is declared as const, the one in your circle class is not. Therefore, the one in circle does not override the one in shape. So it's still abstract since it hasn't overridden a pure virtual function.

2

You're missing a const after the circle's definition of draw.

virtual void draw() const { cout<<"Circle"<<endl; }

When using abstract functions, the function prototype/signature has to match -exactly-

Edit: Blegh, beaten by 30 seconds.

-1

If you bother to look at you compiler output something like this would show:

g++ main.cpp
main.cpp: In function ‘int main()’:
main.cpp:5:12: error: cannot declare variable ‘c’ to be of abstract type ‘circle’
circle.h:6:29: note:   because the following virtual functions are pure within ‘circle’:
shape.h:9:18: note:     virtual void shape::draw() const

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.