编写程序计算圆的周长和面积。要求构造函数能对半径进行初始化,方法getPerimeter和getArea分别计算
1个回答
展开全部
#include <iostream>
using namespace std;
const double PI=3.1415926;
class Circle{
double radius;
public:
Circle(double r=1){
radius=r;
}
double getPerimeter(){
return 2*PI*radius;
}
double getArea(){
return PI*radius*radius;
}
};
int main(){
Circle c1(10);
cout <<"Perimeter: " <<c1.getPerimeter() <<endl;
cout <<"Area: " <<c1.getArea() <<endl;
return 0;
}
using namespace std;
const double PI=3.1415926;
class Circle{
double radius;
public:
Circle(double r=1){
radius=r;
}
double getPerimeter(){
return 2*PI*radius;
}
double getArea(){
return PI*radius*radius;
}
};
int main(){
Circle c1(10);
cout <<"Perimeter: " <<c1.getPerimeter() <<endl;
cout <<"Area: " <<c1.getArea() <<endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询