设计一个圆类(Circle),属性包括char *Name,double R。其中Name为圆的名称指针变量,R为圆的半径。
发布网友
发布时间:2022-05-10 21:23
我来回答
共1个回答
热心网友
时间:2023-11-02 02:14
#include<iostream.h>
const double PI=3.1415926;
class Shapes
{
protected:
double x,y;
public:
Get_value(double xx=0,double yy=0)
{x=xx;y=yy;}
virtual void area()=0;
virtual void perim()=0;
};
class Rectangle:public Shapes
{
public:
void area()
{cout<<"The area of Rectangle is:"<<x*y<<endl;}
void perim()
{cout<<"The perim of Rectangle is:"<<2*(x+y)<<endl;}
};
class Circle:public Shapes
{
public:
void area()
{cout<<"The area of Circle is:"<<PI*x*x<<endl;}
void perim()
{cout<<"The perim of Circle is:"<<2*PI*x<<endl;}
};
void main()
{
double a,b,r;
Shapes *ptr[2];
Rectangle rect;
Circle cir;
ptr[0]=▭
cout<<"Please input the len and wid of Rectangle: "<<endl;
cin>>a>>b;
ptr[0]->Get_value(a,b);
ptr[0]->area();
ptr[0]->perim();
cout<<endl;
ptr[1]=○
cout<<"Please input the value of Circle:"<<endl;
cin>>r;
ptr[1]->Get_value(3);
ptr[1]->area();
ptr[1]->perim();
cout<<endl;
}
完成一个名为circle的圆类,其数据成员int*radius为指向其半径的指针...
include#includeusingnamespacestd;classCircle{doubler,Area,Circumference;public:Circle(doublea=0);Circle(Circle&);voidSetR(doubleR);doubleGetR(){returnr;}doubleGetAreaCircle(){returnArea;}doubleGetCircumference(){returnCircumference;}};Circle::Circle(doublea){r=a;Area=r*r*3.14159265;Ci...
分别用成员函数和友元函数来求圆的面积和周长。
using namespace std;class Circle { public:Circle(double r=0):radius(r){} double Area(){ return 3.14 * radius * radius; } friend double Perimeter(const Circle &c);private:double radius;};double Perimeter(const Circle &c){ return 2.* 3.14 * c.radius;} int main(){ Cir...