C++ find the perimeter and area of a circle

 First of all, I recommend a github for learning C language/C++, sharing C language/C introduction, learning route, e-book recommendation and download, video recommendation, small projects, dry articles, written test interviews, C language/C++ specifications, open source projects, etc. .

Address: C language/C++ learning platform

Data description:    

                   Radius, perimeter, and area are all represented by real numbers

data processing:

                   input radius r ;

                   compute perimeter = 2*π* r ;

                   Calculate area = π* r 2 ;

                   Output radius, perimeter, area;

Method 1 : Use structured programming to find the perimeter and area of ​​a circle

// count the girth and area of circle
#include<iostream.h>
using name std;
void 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 ;
}

Method 2 : Use object-oriented programming to find the circumference and area of ​​a circle

#include<iostream.h> 
using name 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 ; }
} ;
void main()
{ 
Circle A, B ; //用类定义对象
   A.Set_Radius( 6.23 ) ; //类的调用
   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.5 ) ;
   cout << "B.radius = " << B.Get_Radius() << endl ;
   cout << "B.Girth=" << B.Get_Girth() << endl ;
   cout << "B.Area = " << B.Get_Area() << endl ; 
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324132533&siteId=291194637