定义一个圆类(Circle),要求用两种方法定义: 方法一: 要求属性为半径,操作为计算圆的周长和面积。 方 200
展开全部
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();
#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();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |