12345678910111213141516171819202122232425262728293031323334 |
- const float PI = 3.14;
- class Circle {
- private:
- float radius;
- public:
- // 构造函数
- Circle(float r) {
- radius = r;
- }
- // 计算圆的面积
- float getArea() {
- return PI * radius * radius;
- }
- // 计算圆的周长
- float getGirth() {
- return 2 * PI * radius;
- }
- };
- #include<iostream>
- using namespace std;
- int main()
- {
- float r;
- cout << "请输入圆的半径:";
- cin >> r; //从键盘接受半径的值
- Circle c1(r); //将值赋值给半径并调用定义好的函数
- cout<<"圆的面积为:"<<c1.getArea()<<endl;
- Circle c2(r);
- cout<<"圆的周长为:"<<c2.getGirth()<<endl;
- }
|