输入圆的半径,求园的面积,要求定义和调用函数circle(r)计算园的面积
发布网友
发布时间:2023-05-08 23:19
我来回答
共1个回答
热心网友
时间:2024-12-14 08:25
答:圆的面积: f(r)=π·r²,(r>0); 圆的周长:f(r)=2π·r; (r>0);
有2种方法,自己在VC6.0上面试试
#include<iostream>
using namespace std;
#define pi 3.14
class circle
{
public:
circle(double r=0.0){}
circle(circle &c){}
double circumference();
double area();
void cinradius();
double coutradius(){return radius;}
private:
double radius;
};
double circle::circumference()
{
return 2*pi*radius;
}
double circle::area()
{
return pi*radius*radius;
}
void circle::cinradius()
{
cout<<endl
<<"请输入半径:";
cin>>radius;
}
int main()
{
circle CIR;
CIR.cinradius();
cout<<"半径:"
<<CIR.coutradius();
cout<<endl
<<"圆的周长为"
<<CIR.circumference()
<<endl
<<"圆的面积为"
<<CIR.area();
return 0;
}
或者
#include<iostream.h>
class Circle{
private:
float R;//半径
const float PI;//pi
public:
void Circle::registCircle(float);
float outr(void);
float outc(void);
float outs(void);
};
void Circle::registCircle(float r)
{
R=r;
PI=3.1415926;
}
float Circle::outr(void)
{
return(R);
}
float Circle::outc(void)
{
float c;
c=2*PI*R;
return(c);
}
float Circle::outs(void)
{
float s;
s=PI*R*R;
return(s);
}
void main()
{
Circle yuan;
float rr;
cout<<"请输入圆半径:"<<endl;
yuan.registCircle(rr);
cout<<"圆的半径为:"<<yuan.outr();
cout<<"圆的周长为:"<<yuan.outc();
cout<<"圆的面积为:"<<yuan.outs();
}