用C++写代码,main.cpp。里面包含三个类,分别为点,圆,圆柱为.cpp和.h形式。圆里面要包含点,圆柱里面要包含圆,且圆里面的函数要求出圆的面积,圆柱里面的函数要求出圆柱的表面积。
时间: 2024-03-07 07:49:31 浏览: 18
好的,以下是代码实现:
Point.h:
```cpp
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point(double x = 0, double y = 0, double z = 0);
double getX() const;
double getY() const;
double getZ() const;
private:
double x, y, z;
};
#endif
```
Point.cpp:
```cpp
#include "Point.h"
Point::Point(double x, double y, double z) : x(x), y(y), z(z) {}
double Point::getX() const {
return x;
}
double Point::getY() const {
return y;
}
double Point::getZ() const {
return z;
}
```
Circle.h:
```cpp
#ifndef CIRCLE_H
#define CIRCLE_H
#include "Point.h"
class Circle {
public:
Circle(double x = 0, double y = 0, double z = 0, double r = 0);
double getArea() const;
private:
Point center;
double radius;
};
#endif
```
Circle.cpp:
```cpp
#include "Circle.h"
#include <cmath>
Circle::Circle(double x, double y, double z, double r) : center(x, y, z), radius(r) {}
double Circle::getArea() const {
return M_PI * radius * radius;
}
```
Cylinder.h:
```cpp
#ifndef CYLINDER_H
#define CYLINDER_H
#include "Circle.h"
class Cylinder {
public:
Cylinder(double x = 0, double y = 0, double z = 0, double r = 0, double h = 0);
double getSurfaceArea() const;
private:
Circle base;
double height;
};
#endif
```
Cylinder.cpp:
```cpp
#include "Cylinder.h"
#include <cmath>
Cylinder::Cylinder(double x, double y, double z, double r, double h) : base(x, y, z, r), height(h) {}
double Cylinder::getSurfaceArea() const {
return 2 * base.getArea() + 2 * M_PI * base.radius * height;
}
```
main.cpp:
```cpp
#include "Cylinder.h"
#include <iostream>
int main() {
Cylinder cyl(0, 0, 0, 2, 5);
std::cout << "Cylinder surface area is " << cyl.getSurfaceArea() << std::endl;
return 0;
}
```
以上代码实现了三个类,分别为Point、Circle和Cylinder。其中Circle类包含一个Point对象,Cylinder类包含一个Circle对象。在各自的类中实现了计算面积和表面积的函数。在main函数中创建了一个Cylinder对象并调用了getSurfaceArea函数输出表面积。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)