问题描述
- 求大神帮忙改一下,改了好多次了
-
改了两天了,还是没改好,不知怎么改才好,错的地方有注释#include
#include
#include
using namespace std;
#define PI 3.1415class Shape
{
public:
virtual double area()=0;
virtual double volume()=0;//11 18 C:UsersAdministratorDesktop未.cpp [Note] virtual double Shape::volume()
virtual double surface()=0;//12 24 C:UsersAdministratorDesktop未.cpp [Note] virtual double Shape::surface()
};
class Circle:public Shape//圆形
{
public:
Circle(double r):radius(r){}
virtual double area(){return PI*radius*radius;}
virtual double volume(){return 0;}
virtual double surface(){return 0;}
protected:
double radius;
};
class Cylinder:public Circle//圆柱 //24 7 C:UsersAdministratorDesktop未.cpp [Note] Cylinder::Cylinder(const Cylinder&)
{
public:
Cylinder(double r,double h):Circle(r),height(h){} //27 3 C:UsersAdministratorDesktop未.cpp [Note] Cylinder::Cylinder(double, double)
virtual double surface(){return PI*radius*2*height+PI*radius*radius*2;}
virtual double volume(){return PI*(radius*2)*height;}
protected:
double height;};
class Sphere:public Circle//球
{
public:
Sphere(double r):Circle(r){}
double surface(){return 4*radius*radius*PI;}
double volume(){return (4/3)*PI*radius*radius*radius;}
protected:
double radius;};
class Cone:public Cylinder//圆锥
{
public:
Cone(double r,double h):Cylinder(r),Cylinder(h){}//45 49 C:UsersAdministratorDesktop未.cpp [Error] no matching function for call to 'Cylinder::Cylinder(double&)'
double volume(){return (1/3)*(PI*r*r*h);}
double surface(){return PI*r*r+PI*r*sqrt(r*r+h*h);}
protected:
double r,h;};
class Rectangle:public Shape//长方形 //51 7 C:UsersAdministratorDesktop未.cpp [Note] because the following virtual functions are pure within 'Rectangle':
{
public:
Rectangle(double w,double h):width(w),height(h){}
virtual double area(){return width*height;}
protected:
double width,height;
};
class Cuboid//长方体
{
public:
Cuboid(double l,double w,double h):length(l),width(w),height(h){}
double surface(){return 2*(l*w+l*h+w*h);}
double volume(){return l*w*h;}//64 26 C:UsersAdministratorDesktop未.cpp [Error] 'l' was not declared in this scope
protected:
double length,width,height;
};
int main()
{
while(true)
{
cout<<"1.圆形"<
cout
cout
cout
cout
cout
cout
int choice;
cin>>choice;
system("cls");
if(choice==0)
break;
default://84 3 C:UsersAdministratorDesktop未.cpp [Error] case label not within a switch statement
cout << "error" ;
system("pause");
system("cls") ;
switch(choice)
case 1:{double r=0;
cout<<"请输入圆形的半径:";
cin>>r;
Circle circle(r);
cout<<"圆形的面积:"<
system("pause");
system("cls") ;
break;
}
case 2:{double w=0,h=0;//98 3 C:UsersAdministratorDesktop未.cpp [Error] case label '2' not within a switch statement
cout
cout>h;
cout<<"宽:";cin>>w;
Rectangle rectangle(h,w);//102 15 C:UsersAdministratorDesktop未.cpp [Error] cannot declare variable 'rectangle' to be of abstract type 'Rectangle'
cout<<"长方形的面积:"<
system("pause");
system("cls") ;
break;
}
case 3:{ //108 3 C:UsersAdministratorDesktop未.cpp [Error] case label '3' not within a switch statement
double l=0,w=0,h=0;
cout
cout>l;
cout<<"宽:";cin>>w;
cout<<"高:";cin>>h;
Cuboid cuboid(l,w,h);
cout<<"长方体的表面积:"<
cout
system("pause");
system("cls") ;
break;
}
case 4:{//121 3 C:UsersAdministratorDesktop未.cpp [Error] case label '4' not within a switch statement
double r=0,h=0;
cout
cout>r;
cout<<"高:";cin>>h;
Cylinder cylinder(r,h);
cout<<"圆柱的表面积:"<
cout
system("pause");
system("cls") ;
break;
}
case 5:{//133 3 C:UsersAdministratorDesktop未.cpp [Error] case label '5' not within a switch statement
double r=0,h=0;
cout
cout>r;
cout<<"高:";cin>>h;
Cone cone(r,h);
cout<<"圆锥的表面积:"<
cout
system("pause");
system("cls") ;
break;
}
case 6:{//145 3 C:UsersAdministratorDesktop未.cpp [Error] case label '6' not within a switch statement
double r=0;
cout
cin>>r;
Sphere sphere(r);
cout<<"圆球的表面积:"<<sphere.area()<<endl;
cout<<"圆球的体积:"<<sphere.volume()<<endl;
system("pause");
system("cls") ;break;
}}
}
解决方案
粘贴错误太多,把代码放到代码片里。给你改了一部分:
class Sphere:public Circle//球
{
public:
Sphere(double r):Circle(r){}
double surface(){return 4*radius*radius*PI;}
double volume(){return (4/3.0)*PI*radius*radius*radius;}//修改
protected:
double radius;
};
class Cone:public Cylinder//圆锥
{
public:
Cone(double r,double h):Cylinder(r,h){}//修改
double volume(){return (1/3.0)*(PI*r*r*h);}//修改
double surface(){return PI*r*r+PI*r*sqrt(r*r+h*h);}
protected:
double r,h;
};
class Rectangle:public Shape//长方形 //51 7 C:UsersAdministratorDesktop未.cpp [Note] because the following virtual functions are pure within 'Rectangle':
{
public:
Rectangle(double w,double h):width(w),height(h){}
virtual double area(){return width*height;}
protected:
double width,height;
};
class Cuboid//长方体
{
public:
Cuboid(double l,double w,double h):length(l),width(w),height(h){}
double surface(){return 2*(length*width+length*height+width*height);}//修改
double volume(){return length*width*height;}//修改
protected:
double length,width,height;
};
解决方案三:
就是有错误,派生类哪里,搞了好久都搞不了
解决方案四:
你把编译器提示错误的地方标出来,减少其他回答者的工作量。