3 回答
TA贡献171条经验 获得超74个赞
#include <iostream> #include <cstdio> using namespace std; class circle{ private: double radius; public: void setRadius(double radius){ this->radius = radius; } double area(){ return 3.14*radius*radius; } double zhouchang(){ return 2*3.14*radius; } double getRadius(){ return radius; } circle(){} circle(double radius){ this->radius = radius; } circle(circle &c){ this->radius = c.getRadius(); } }; int main(){ circle c1; double radius; cin >> radius; c1.setRadius(radius); cout << c1.area() << endl; cout << c1.zhouchang() << endl; circle c2(2*radius); cout << c2.area() << endl; cout << c2.zhouchang() << endl; circle c3(c1); cout << c3.area() << endl; cout << c3.zhouchang() << endl; return 0; }
TA贡献143条经验 获得超187个赞
#include <iostream> using namespace std; #define PI 3.141592635 class Circle{ public: Circle() : _r(0.f){} Circle(double r) : _r(r){} Circle(const Circle& circle) { this->_r = circle._r; } void setRadius(double r) { this->_r = r; } double Area() { return PI * this->_r * this->_r; } double Circumference() { return 2 * PI * this->_r; } private: double _r; }; int main(){ Circle c1; double x; cin >> x; c1.setRadius(x); cout << "c1面积:" << c1.Area() << endl; cout << "c1周长:" << c1.Circumference() << endl; Circle c2(2 * x); cout << "c2面积:" << c2.Area() << endl; cout << "c2周长:" << c2.Circumference() << endl; Circle c3(c1); cout << "c3面积:" << c3.Area() << endl; cout << "c3周长:" << c3.Circumference() << endl; return 0; }
TA贡献1条经验 获得超0个赞
#include<iostream>
using namespace std;
/*编写一个关于圆形的C++程序。要求用定义一个圆形类Circle,其中包含如下成员:
1) 1个私有数据成员(半径)。
2) 3个公有函数成员(设置半径、计算面积、计算周长)。
3) 3个构造函数(不带参数的构造函数、带参数的构造函数和拷贝构造函数)。
主函数main使用圆形类Circle创建圆形对象,要求:
1) 定义一个圆对象c1,从键盘输入一个值x并将其设定为c1的半径,计算并显示c1的面积和周长
2) 再定义一个圆对象c2,并将半径初始化为2x,计算并显示c2的面积和周长
3) 再定义一个圆对象c3,并用c1初始化c3,计算并显示c3的面积和周长*/
#define PI 3.14
class Circle
{
private:
double radius;
public:
Circle();
Circle(double radius);
Circle(Circle& c);
void setR(double radius);
double area();
double circumference();
};
Circle::Circle()
{
cout<<"Circle()"<<endl;
}
Circle::Circle(double radius):radius (radius )
{
cout<<"Circle(float radius)"<<endl;
}
Circle::Circle(Circle& c)
{
radius =c.radius;
cout<<"Circle(Circle& c)"<<endl;
}
void Circle ::setR (double radius)
{
this->radius =radius ;
}
double Circle::area()
{
return PI*radius *radius ;
}
double Circle::circumference()
{
return PI*2*radius ;
}
//定义一个圆对象c1,从键盘输入一个值x并将其设定为c1的半径,计算并显示c1的面积和周长
//2) 再定义一个圆对象c2,并将半径初始化为2x,计算并显示c2的面积和周长
//3) 再定义一个圆对象c3,并用c1初始化c3,计算并显示c3的面积和周长*/
int main()
{
Circle c1;
double r;
cin>>r;
c1.setR (r);
cout<<"area="<<c1.area() <<"circumference="<<c1.circumference ()<<endl;
Circle c2(2);
cout<<"area="<<c2.area() <<"circumference="<<c2.circumference ()<<endl;
Circle c3(c1);
cout<<"area="<<c3.area() <<"circumference="<<c3.circumference ()<<endl;
return 0;
}
- 3 回答
- 1 关注
- 4672 浏览
添加回答
举报