定义一个圆类(Circle),要求用两种方法定义: 方法一: 要求属性为半径,操作为计算圆的周长和面积。 方
3个回答
展开全部
#include <iostream.h>
#include <math.h>
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;
}
void 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;
}
#include <math.h>
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;
}
void 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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package other;
public class Circle {
private int radius;
private double PI=3.14;
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public double getArea(int r){
return PI*r*r;
}
public double getGirth(int r){
return 2*PI*r;
}
}
public class Circle {
private int radius;
private double PI=3.14;
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public double getArea(int r){
return PI*r*r;
}
public double getGirth(int r){
return 2*PI*r;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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();
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |