I've got few classes:
class Shape{
/* ... */
public:
virtual double field() = 0;
virtual double circumference() = 0;
};
class Circle: public Shape{
protected:
Point center;
double R;
public:
Shape(Point &p1, double R){
this->center=p1;
this->R = R;
}
double field(){
return M_PI *this->R * this->R;
}
double circumference(){
return 2* M_PI * this->R;
}
friend ostream & operator<<(ostream &ostr, const Circle &f);
};
The friend overloaded operator is:
ostream & operator<<(ostream &ostr, const Circle &f){
ostr<<"Radius: "<<f.R<<endl;
ostr<<"Circumference: "<<f.circumference()<<endl;
ostr<<"Field: "<<f.field()<<endl;
return ostr;
}
And the main code contains:
/* ... */
Point p = {0,0};
Circle c = Circle(p, 10);
cout<<c;
The error is inside ovefloaded operator<<
:
passing 'const Circle' as 'this' argument of 'virtual double Circle::field()'
But when I change double field(){
to double field() const{
I get:
Cannot allocate an object of abstract type 'Circle'
I suppose I don't fully understand usage of virtual
. Can someone please explain what am I doing wrong?