C++编程求助编写一个基于对象的程序:输入半径,计算圆的周长和面积并输出

2024-12-01 09:18:45
推荐回答(4个)
回答1:

#include"iostream"
#define PI 3.1415962
using namespace std;
class circle
{
private:
int r; //半径
public:
circle() //无参数构造函数以10为半径
{
r=10;
}
circle(double x) //构造函数
{
r=x;
}
double length()
{

return 2*PI*r;

}
double area()
{
return PI*r*r;
}
}
main()
{
double x;
circle a;
cout<<"默认无参数构造函数输出"< cout<<"周长"< cout<<"面积"< cout<<"请输入半径";

cin>>x;
circle b(x);

cout<<"参数构造函数输出"< cout<<"周长"< cout<<"面积"<}

回答2:

#include
#define Pi 3.14;
class yuan
{
public:
float length(float);
float area(float);
private:
float r;
};

float yuan::length(float r)
{float len;
len=2*Pi*r;}
float yuan::area(floay r)
{float ar;
ar=Pi*r*r;}
void main()
{
yuan m;
cout<
}

回答3:

#include
using namespace std;
class circle
{
public:circle(double r):a(r){}
void length(){cout<<"length="<<2*3.14*a<void area(){cout<<"area="<<3.14*a*a<private:double a;};
int main()
{
circle x(10.0);
x.length();
x.area();
cin.get();
}

回答4:

周长:(保留2位小数)
var
r,l:real;
begin
readln(r);
l:=2*3.14*r;
writeln(l:0:2);

end

面积:(保留2位小数)
var
r,s:real;
begin
readln(r);
s:=3.14*r*r;
writeln(s:0:2);

end.