如下内容:error C2512请问该怎么解决?

#include<iostream>
using namespace std;
class Shape
{
public:
virtual double printArea()const{return 0.0;}
virtual double printVol()const{return 0.0;}
};

class Circle:public Shape
{
public:
Circle(double r):rad(r){}
double printArea(){return 3.14*rad*rad;}
private:
double rad;
};

class Rect:public Shape
{
public:
Rect(double l,double w):len(l),wid(w){}
double printArea(){return len*wid;}
private:
double len,wid;
};

class Tri:public Shape
{
public:
Tri(double l,double h):len(l),hei(h){}
double printArea(){return 0.5*len*hei;}
private:
double len,hei;
};

class Cy:public Circle
{
public:
Cy(double r,double h):rad(r),hei(h){}
double printArea(){return (2*3.14*rad*rad+2*3.14*rad*hei);}
double printVol(){return 3.14*rad*rad*hei;}
private:
double rad,hei;
};

class Rec:public Rect
{
public:
Rec(double l,double k,double h):len(l),kuan(k),hei(h){}
double printArea(){return (2*len*kuan+2*len*hei+2*hei*kuan);}
double printVol(){return len*kuan*hei;}
private:
double len,kuan,hei;
};
int main()
{
Circle a(10);
Rect b(10,20);
Tri c(10,20);
Cy d(10,20);
Rec e(10,10,10);
Shape * p;
p=&a;
cout<<p->printArea()<<endl;
p=&b;
cout<<p->printArea()<<endl;
p=&c;
cout<<p->printArea()<<endl;
p=&d;
cout<<p->printArea()<<endl;
cout<<p->printVol()<<endl;
p=&e;
cout<<p->printArea()<<endl;
cout<<p->printVol()<<endl;
return 0;
}

慕森卡
浏览 250回答 2
2回答

万千封印

改两处,基类的构造函数没有默认的,要么加上默认的,要么显式调用带参版本的基类构造。另外,你可能看不到你想要的结果,所有子类的printArea函数并没有重写父类Sharp的printArea函数,所以,需要将每个子类的printArea函数带上const修饰。在C++中,函数重写,要满足四个等同条件:返回值类型函数名称参数列表函数属性(常函数/非常函数)当然还有最重要的一点就是,父类的函数得是虚函数,也就是前边得有virtual关键字修饰。

Smart猫小萌

rad并不是一个函数吧,在circle的构造函数那里
打开App,查看更多内容
随时随地看视频慕课网APP