定义基类Point(点)和派生类Circle(圆),求圆的周长。Point类有两个私有的数据成员float x,y;Circle类新增一个私有的数据成员半径float r和一个公有的求周长的函数getCircumference();主函数已经给出,请编写Point和Circle类。
输入格式:
输入圆心和半径,x y r中间用空格分隔。
输出格式:
输出圆的周长,小数点后保留2位有效数字。
输入样例:
1 2 3
输出样例:
在这里给出相应的输出。例如:
Point constructor called
Circle constructor called
18.84
Circle destructor called
Point destructor called
#include<iostream>
#include<iomanip>
using namespace std;
class Point {
public:
Point() {
cout << "Point constructor called" << endl;
}
~Point() {
cout << "Point destructor called" << endl;
}
private:
float x;
float y;
};
class Circle :public Point{
public:
Circle(float x,float y,float r){
this->x = x;
this->y = y;
this->r = r;
cout << "Circle constructor called" << endl;
}
~Circle() {
cout << "Circle destructor called" << endl;
}
double getCircumference()
{
S = 2 * pi * r;
return S;
}
double S;
private:
float x, y;
float r;
float pi = 3.14;
};
int main(){
float x, y, r;
cin >> x >> y >> r;
Circle c(x, y, r);
cout << fixed << setprecision(2) << c.getCircumference() << endl;
return 0;
}