关于C++语言编程!跪谢。。。
- 提问者网友:溺爱和你
- 2021-05-09 11:35
- 五星知识达人网友:梦中风几里
- 2021-05-09 12:25
class Shape
{
public:
Shape(){}
virtual double GetArea()=0;
virtual double Getperim()=0;
};
class Rectangle:public Shape
{
private:
double h;
double c;
public:
Rectangle(){h=c=0;}
Rectangle(double hi,double ch){h=hi;c=ch;}
double GetArea(){return c*h;}
double Getperim(){return 2*c+2*h;}
};
class Circle:public Shape
{
private:
double r;
const double pi=3.14;
public:
Circle(){r=0;}
Circle(double ri){r=ri;}
double GetArea(){return r*r*pi;}
double Getperim(){return 2*r*pi;}
};
其余的调用方式自己试做写吧!
- 1楼网友:舊物识亽
- 2021-05-09 13:17
class Shape
{
public:
virtual int GetAra()=0;
virtual int GetPerim()=0;
};
class Rectanglr:public Shape
{
public:
int GetArea(int a ,int b)
{
int s;
s=a*b;
return s;
}
int GetPerim(............)
{
.................
}
};
class Circle:public Shape
{
public:
int GetArea(..........)
{
.......................
}
int GetPerim(...........)
{
...........................
}
};
- 2楼网友:迟山
- 2021-05-09 12:58
//定义一个Shape抽象类,由它派生出Rectanglr和Circle类, //二者都有GetArea( )函数计算对象的面积,GetPerim( ) 函数计算对象的周长。
#include<iostream.h> class Shape { public: int getarea(); int getperim(); };
class Rectanglr:public Shape ////矩形 { public: int getarea(int c, int k) { cout<<"矩形的面积是:"<<c*k<<endl; return 0; } int getperim(int c, int k) { cout<<"矩形的周长是:"<<2*(c+k)<<endl; return 0; } }; class Circle:public Shape ////圆形 { public: int getarea(int r) { cout<<"圆形的面积是:"<<2*3.14*r<<endl; return 0; } int getperim(int r) { cout<<"圆形的周长是:"<<3.14*r*r<<endl; return 0; } };
void main() { Circle a; a.getarea(2); a.getperim(2);
Rectanglr b; b.getarea(3,4); b.getperim(3,4); }
随便搞的下,能做出来,但是不知道满不满足你的要求,我也很久没做过了