为了账号安全,请及时绑定邮箱和手机立即绑定

以下内容是关于C++面向对象程序设计问题,求解释~

以下内容是关于C++面向对象程序设计问题,求解释~

C++ C
宝慕林4294392 2021-11-20 15:11:17
题目:应用VC++6.0的编辑环境构造一个类Jhtx及其派生类,该类主要实现关于几何图形的基本操作。对于基类“几何图形”,有求面积、求体积的函数(纯虚函数),其派生类圆和矩形主要有初始化(构造函数),求面积,求周长操作,类圆的派生类圆球和圆柱有求表面积、体积操作。 试在主函数中分别定义圆、圆球、圆柱以及矩形的对象,并调用其成员函数实现其相应操作。以下是我的程序,但是有一些错误,都是相同的错误,请大家指教。#include<iostream.h>class Geometry{public:Geometry(double a,double b,double c){x=a,y=b,c=z;}~Geometry(){};virtual double GetArea()=0;double GetPerimeter();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 Getolume(){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{protected: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;}
查看完整描述

2 回答

?
MYYA

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;
}



查看完整回答
反对 回复 2021-11-24
?
慕桂英4014372

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;
}
改完了,能运行,自己看看



查看完整回答
反对 回复 2021-11-24
  • 2 回答
  • 0 关注
  • 397 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信