2 回答
TA贡献1868条经验 获得超4个赞
我改了一下,已经能通过并且运行了:
#include<iostream.h>
class Geometry{
public:
Geometry(double a,double b,double c)
{x=a,y=b,c=z;}
~Geometry(){};
virtual double GetArea()=0;
virtual double GetPerimeter()=0;//这里有改动
virtual double GetColume()=0;
virtual void Show()=0;
protected:
double x,y,z;
};
class Circle:public Geometry{
protected:
double r;
public:
Circle(double a):Geometry(a,0,0)
{r=a;}
~Circle(){};
double GetArea()
{return 3.14*r*r;}
double GetPerimeter()
{return 6.28*r;}
virtual double GetColume()
{return 0;}
void Show()
{cout<<"Area is:"<<GetArea()<<endl;
cout<<"Perimeter is:"<<GetPerimeter()<<endl;}
};
class Rectangle:public Geometry{
public:
Rectangle(double a,double b):Geometry(a,b,0)
{}
~Rectangle(){};
double GetArea()
{return x*y;}
double GetPerimeter()
{return x+y;}
virtual double GetColume()//这里有改动
{return 0;}
void Show()
{cout<<"Area is:"<<GetArea()<<endl;
cout<<"Perimeter is:"<<GetPerimeter()<<endl;}
};
class Ball:public Circle{
public:
Ball(double a):Circle(a)
{};
~Ball(){};
double GetArea()
{return 4*3.14*x*x;}
double GetColume()
{return (4/3)*3.14*x*x*x;}
double GetPerimeter()
{return 0;}
void Show()
{cout<<"Area is:"<<GetArea()<<endl;
cout<<"Getcolume is:"<<GetColume()<<endl;}
};
class Column:public Circle{
public://这里改为public
double h;
Column(double a,double h):Circle(a),h(h){}
~Column(){};
double GetArea()
{return 2*3.14*x*x+2*3.14*x*h;}
double GetColume()
{return 3.14*x*x*h;}
double GetPerimeter()
{return 0;}
void Show()
{cout<<"Area is:"<<GetArea()<<endl;
cout<<"Getcolume is:"<<GetColume()<<endl;}
};
int main()
{ Geometry *p;
Circle ob1(1);
Rectangle ob2(2,2);
Ball ob3(3);
Column ob4(4,3);
p=&ob1;
p->GetArea();
p->GetPerimeter();
p->Show();
p=&ob2;
p->GetArea();
p->GetPerimeter();
p->Show();
p=&ob3;
p->GetArea();
p->GetPerimeter();
p->Show();
p=&ob4;
p->GetArea();
p->GetPerimeter();
p->Show();
return 0;
}
TA贡献1871条经验 获得超13个赞
#include<iostream>
using namespace std;
class Geometry{
public:
Geometry(double a,double b,double c)
{x=a,y=b,c=z;}
virtual ~Geometry(){};
virtual double GetArea()=0;
virtual double GetPerimeter()=0;
virtual double GetColume()=0;
virtual void Show()=0;
protected:
double x,y,z;
};
class Circle:public Geometry{
protected:
double r;
public:
Circle(double a):Geometry(a,0,0)
{r=a;}
virtual ~Circle(){};
double GetArea()
{return 3.14*r*r;}
double GetPerimeter()
{return 6.28*r;}
virtual double GetColume()
{return 0;}
void Show()
{cout<<"Area is:"<<GetArea()<<endl;
cout<<"Perimeter is:"<<GetPerimeter()<<endl;}
};
class Rectangle:public Geometry{
public:
Rectangle(double a,double b):Geometry(a,b,0)
{}
~Rectangle(){};
double GetArea()
{return x*y;}
double GetPerimeter()
{return x+y;}
virtual double GetColume()
{return 0;}
void Show()
{cout<<"Area is:"<<GetArea()<<endl;
cout<<"Perimeter is:"<<GetPerimeter()<<endl;}
};
class Ball:public Circle{
public:
Ball(double a):Circle(a)
{};
~Ball(){};
double GetArea()
{return 4*3.14*x*x;}
double GetColume()
{return (4/3)*3.14*x*x*x;}
double GetPerimeter()
{return 0;}
void Show()
{cout<<"Area is:"<<GetArea()<<endl;
cout<<"Getcolume is:"<<GetColume()<<endl;}
};
class Column:public Circle{
public:
double h;
Column(double a,double h):Circle(a){
this->h = h;
}
~Column(){};
double GetArea()
{return 2*3.14*x*x+2*3.14*x*h;}
double GetColume()
{return 3.14*x*x*h;}
double GetPerimeter()
{return 0;}
void Show()
{cout<<"Area is:"<<GetArea()<<endl;
cout<<"Getcolume is:"<<GetColume()<<endl;}
};
int main()
{ Geometry *p;
Circle ob1(1);
Rectangle ob2(2,2);
Ball ob3(3);
Column ob4(4,3);
p=&ob1;
p->GetArea();
p->GetPerimeter();
p->Show();
p=&ob2;
p->GetArea();
p->GetPerimeter();
p->Show();
p=&ob3;
p->GetArea();
p->GetPerimeter();
p->Show();
p=&ob4;
p->GetArea();
p->GetPerimeter();
p->Show();
return 0;
}
改完了,能运行,自己看看
- 2 回答
- 0 关注
- 384 浏览
添加回答
举报