方法一:用结构化方法编程,求圆的周长和面积。
#include <iostream>
using namespace std;
int main(){
double r,girth,area;
const double PI=3.1415;
cout<<"Please input radius:\n" ;
cin>>r;
girth=2*PI*r;
area=PI*r*r;
cout<<"radius="<<r<<endl;
cout<<"girth=" <<girth<<endl;
cout<<"area=" <<area<<endl;
}
方法二:用面向对象方法编程
v
#include <iostream>
using namespace std;
class Circle
{
double radius;
public:
void Set_Radius(double r) {radius=r;}
double Get_Radius() {return radius;}
double Get_Girth() {return 2*3.14f*radius;}
double Get_Area() {return 3.14f*radius*radius;}
};
int main()
{
Circle A,B;
A.Set_Radius(6.01);
cout<<"A.radius="<<A.Get_Radius()<<endl;
cout<<"A.Girth="<<A.Get_Girth()<<endl;
cout<<"A.Area="<<A.Get_Area()<<endl;
B.Set_Radius(10.4);
cout<<"B.radius="<<B.Get_Radius()<<endl;
cout<<"B.Girth="<<B.Get_Girth()<<endl;
cout<<"B.Area="<<B.Get_Area()<<endl;
}