C++ experiment --- the area of a circle

Area of ​​the circle

Description

Define the Circle class, there is a private, double type attribute radius, which represents the radius of the circle. There is a public, often static data member of the double type, PI=3.14, which represents the pi. Define the getArea() and getRadius() methods to return the area and radius of the circle. Note: When defining the above two methods, ensure that the program can be executed correctly.

Input

A double type of data.

Output

See example.

Sample Input

3.15

Sample Output

PI=3.14
radius=2,area=12.56
radius=3.15,area=31.1566

Title given code

int main()
{
    
    
    double radius;
    cout<<"PI="<<Circle::PI<<endl;
    const Circle c1(2);
    cout<<"radius="<<c1.getRadius();
    cout<<",area="<<c1.getArea()<<endl;
    cin>>radius;
    Circle c2(radius);
    cout<<"radius="<<c2.getRadius();
    cout<<",area="<<c2.getArea()<<endl;
    return 0;
}

Note: The const object is given in the main function, so the getArea() and getRadius() functions add the const keyword between the parameter list and the body!
code:

#include<iostream>

using namespace std;

class Circle{
    
    
private:
	double radius;
public:
  	static const double PI=3.14;
	
	Circle(double r){
    
    
		radius=r;
	}
	
	double getArea()const{
    
    
		return PI*radius*radius;
	}
	
	double getRadius()const{
    
    
		return radius;
	}
};

int main()
{
    
    
    double radius;
    cout<<"PI="<<Circle::PI<<endl;
    const Circle c1(2);
    cout<<"radius="<<c1.getRadius();
    cout<<",area="<<c1.getArea()<<endl;
    cin>>radius;
    Circle c2(radius);
    cout<<"radius="<<c2.getRadius();
    cout<<",area="<<c2.getArea()<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/timelessx_x/article/details/115262083