定义一个圆类(Circle)18
发布网友
发布时间:2023-10-23 13:03
我来回答
共2个回答
热心网友
时间:2024-11-20 20:26
#include "stdafx.h"
#include<iostream>
#include<cmath>
using namespace std;
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;
}
int 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;
return 0;
}
参考资料:已通过编译,main函数只做测试之用,所以比较灵活,可以自己写
热心网友
时间:2024-11-20 20:26
#include<iostream>
#define pi 3.14
using namespace std;
class circle{
public:
circle(double Radius=0){radius=Radius;area=pi*pi*radius;length=2*pi*radius;}
circle(circle& Circle){area=Circle.area;radius=Circle.radius;length=Circle.length;}
void GetRadius(){cout<<"Radius:"<<radius<<endl;}
void GetArea(){cout<<"Area:"<<area<<endl;}
void GetLength(){cout<<"Length:"<<length<<endl;}
private:
double area,radius,length;
};
int main(){
circle Circle1(4.0),Circle2(Circle1);
Circle1.GetRadius();
Circle1.GetArea();
Circle1.GetLength();
Circle2.GetRadius();
Circle2.GetArea();
Circle2.GetLength();
return 0;
}