7-2 定义基类Point和派生类Circle,求圆的周长. 分数 10 作者 沙金 单位 石家庄铁道大学 定义基类Point(点)和派生类Circle(圆),求圆的周长。Point类有两个私有的数据成员float x,y;Circle类新增一个私有的数据成员半径float r和一个公有的求周长的函数getCircumference();主函数已经给出,请编写Point和Circle类。 #include <iostream> #include<iomanip> using namespace std; //请编写你的代码 int main() { float x,y,r; cin>>x>>y>>r; Circle c(x,y,r); cout<<fixed<<setprecision(2)<<c.getCircumference()<<endl; return 0; } 输入格式: 输入圆心和半径,x y r中间用空格分隔。 输出格式: 输出圆的周长,小数点后保留2位有效数字。 输入样例: 1 2 3 输出样例: 在这里给出相应的输出。例如: Point constructor called Circle constructor called 18.84 Circle destructor called Point destructor called
时间: 2024-02-25 20:57:34 浏览: 111
下面是Point和Circle类的实现代码:
```
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class Point{
private:
float x, y;
public:
Point(float x, float y): x(x), y(y){} // 构造函数
~Point(){} // 析构函数
float getX(){return x;}
float getY(){return y;}
};
class Circle: public Point{
private:
float r;
public:
Circle(float x, float y, float r): Point(x, y), r(r){} // 构造函数
~Circle(){} // 析构函数
float getCircumference(){return 2 * M_PI * r;} // 求周长
};
int main(){
float x, y, r;
cin >> x >> y >> r;
Circle c(x, y, r);
cout << fixed << setprecision(2) << c.getCircumference() << endl;
return 0;
}
```
在上述代码中,Point类表示一个点,Circle类继承自Point类,表示一个圆。Circle类新增了一个私有数据成员r表示圆的半径,以及一个公有函数getCircumference()用于求圆的周长。在主函数中,我们输入圆心和半径,构造一个Circle对象c,并调用它的getCircumference()函数求出圆的周长,然后输出圆的周长。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![.pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)