编程定义一个圆类

时间:2022-12-15 07:30:41
定义一个圆类(Circle),属性为半径(radius)、圆周长和面积,操作为输入半径并计算周长、面积,输出半径、周长和面积。要求定义构造函数(以半径为参数,缺省值为0,周长和面积在构造函数中生成)和拷贝构造函数。编写主函数进行测试。

3 个解决方案

#1


作业贴!!讨厌!!

#2


定义一个圆类(Circle),属性为半径(radius)、圆周长和面积,操作为输入半径并计算周长、面积,输出半径、周长和面积。要求定义构造函数(以半径为参数,缺省值为0,周长和面积在构造函数中生成)和拷贝构造函数。编写主函数进行测试

Class CCircle
{
  double radius;
  double area;
  double circumference;
public:
  CCircle(double r=0);
  CCircle(CCircle &c);
};

...

#3


#include<iostream.h>
#include<math.h>
class Circle{        
        double r,Area,Circumference;
public:
        Circle(double a=0);
        Circle(Circle &);
        void SetR(double R);
        double GetR(){return r;}
        double GetAreaCircle(){return Area;}
        double GetCircumference(){return Circumference;}
};
Circle::Circle(double a){
        r=a;
        Area=r*r*3.14159265;
        Circumference=2*r*3.14159265;
}
Circle::Circle(Circle & cl){
        r=cl.r;
        Area=cl.Area;
        Circumference=cl.Circumference;
}
void Circle::SetR(double R){
        r=R;
        Area=r*r*3.14159265;
        Circumference=2*r*3.14159265;
}
void main(){
        Circle cl1(2),cl2,cl3=cl1;
        cout<<"圆半径:"<<cl3.GetR()<<'\t'<<"圆周长:"<<cl3.GetCircumference()
                <<'\t'<<"圆面积:"<<cl3.GetAreaCircle()<<endl;
        cl2.SetR(4);
        cout<<"圆半径:"<<cl2.GetR()<<'\t'<<"圆周长:"<<cl2.GetCircumference()
                <<'\t'<<"圆面积:"<<cl2.GetAreaCircle()<<endl;
}

#1


作业贴!!讨厌!!

#2


定义一个圆类(Circle),属性为半径(radius)、圆周长和面积,操作为输入半径并计算周长、面积,输出半径、周长和面积。要求定义构造函数(以半径为参数,缺省值为0,周长和面积在构造函数中生成)和拷贝构造函数。编写主函数进行测试

Class CCircle
{
  double radius;
  double area;
  double circumference;
public:
  CCircle(double r=0);
  CCircle(CCircle &c);
};

...

#3


#include<iostream.h>
#include<math.h>
class Circle{        
        double r,Area,Circumference;
public:
        Circle(double a=0);
        Circle(Circle &);
        void SetR(double R);
        double GetR(){return r;}
        double GetAreaCircle(){return Area;}
        double GetCircumference(){return Circumference;}
};
Circle::Circle(double a){
        r=a;
        Area=r*r*3.14159265;
        Circumference=2*r*3.14159265;
}
Circle::Circle(Circle & cl){
        r=cl.r;
        Area=cl.Area;
        Circumference=cl.Circumference;
}
void Circle::SetR(double R){
        r=R;
        Area=r*r*3.14159265;
        Circumference=2*r*3.14159265;
}
void main(){
        Circle cl1(2),cl2,cl3=cl1;
        cout<<"圆半径:"<<cl3.GetR()<<'\t'<<"圆周长:"<<cl3.GetCircumference()
                <<'\t'<<"圆面积:"<<cl3.GetAreaCircle()<<endl;
        cl2.SetR(4);
        cout<<"圆半径:"<<cl2.GetR()<<'\t'<<"圆周长:"<<cl2.GetCircumference()
                <<'\t'<<"圆面积:"<<cl2.GetAreaCircle()<<endl;
}