用C++写一个代码建立一个Point(点)类,包含数据成员m_dX,m_dY(坐标点),构造函数、相应成员函数; 以Point为基类,派生出一个Circle(圆)类,增加数据成员m_dR(半径),构造函数、相应成员函数,圆面积函数getArea(); 再以Circle类为直接基类,派生出一个Cylinder(圆柱体)类,增加数据成员m_dH(高),构造函数、相应成员函数、圆柱体积函数getVolumn()、圆柱表面积函数getArea(). 编写程序,要求: (1)采用分文件编程 (5分) (2)重载运算符<<,使之能用于属于以上类对象的输出。(10分)
时间: 2024-03-24 14:42:05 浏览: 103
10-ABC2-2.zip_Point 类
以下是代码实现:
Point.h文件:
```
#ifndef POINT_H
#define POINT_H
class Point {
public:
Point(double x = 0, double y = 0);
void setX(double x);
void setY(double y);
double getX() const;
double getY() const;
friend std::ostream& operator<<(std::ostream& os, const Point& p);
protected:
double m_dX;
double m_dY;
};
#endif
```
Point.cpp文件:
```
#include "Point.h"
#include <iostream>
Point::Point(double x, double y) {
m_dX = x;
m_dY = y;
}
void Point::setX(double x) {
m_dX = x;
}
void Point::setY(double y) {
m_dY = y;
}
double Point::getX() const {
return m_dX;
}
double Point::getY() const {
return m_dY;
}
std::ostream& operator<<(std::ostream& os, const Point& p) {
os << "(" << p.m_dX << "," << p.m_dY << ")";
return os;
}
```
Circle.h文件:
```
#ifndef CIRCLE_H
#define CIRCLE_H
#include "Point.h"
class Circle : public Point {
public:
Circle(double x = 0, double y = 0, double r = 0);
void setR(double r);
double getR() const;
double getArea() const;
friend std::ostream& operator<<(std::ostream& os, const Circle& c);
protected:
double m_dR;
};
#endif
```
Circle.cpp文件:
```
#include "Circle.h"
#include <cmath>
Circle::Circle(double x, double y, double r) : Point(x, y) {
m_dR = r;
}
void Circle::setR(double r) {
m_dR = r;
}
double Circle::getR() const {
return m_dR;
}
double Circle::getArea() const {
return M_PI * m_dR * m_dR;
}
std::ostream& operator<<(std::ostream& os, const Circle& c) {
os << "Center: " << static_cast<const Point&>(c) << ", Radius: " << c.m_dR;
return os;
}
```
Cylinder.h文件:
```
#ifndef CYLINDER_H
#define CYLINDER_H
#include "Circle.h"
class Cylinder : public Circle {
public:
Cylinder(double x = 0, double y = 0, double r = 0, double h = 0);
void setH(double h);
double getH() const;
double getVolumn() const;
double getArea() const;
friend std::ostream& operator<<(std::ostream& os, const Cylinder& cy);
protected:
double m_dH;
};
#endif
```
Cylinder.cpp文件:
```
#include "Cylinder.h"
#include <cmath>
Cylinder::Cylinder(double x, double y, double r, double h) : Circle(x, y, r) {
m_dH = h;
}
void Cylinder::setH(double h) {
m_dH = h;
}
double Cylinder::getH() const {
return m_dH;
}
double Cylinder::getVolumn() const {
return getArea() * m_dH;
}
double Cylinder::getArea() const {
return 2 * Circle::getArea() + 2 * M_PI * m_dR * m_dH;
}
std::ostream& operator<<(std::ostream& os, const Cylinder& cy) {
os << "Center: " << static_cast<const Point&>(cy) << ", Radius: " << cy.m_dR << ", Height: " << cy.m_dH;
return os;
}
```
main.cpp文件:
```
#include "Cylinder.h"
#include <iostream>
int main() {
Cylinder cy(1, 2, 3, 4);
std::cout << cy << std::endl;
std::cout << "Volumn: " << cy.getVolumn() << std::endl;
std::cout << "Area: " << cy.getArea() << std::endl;
return 0;
}
```
输出结果:
```
Center: (1,2), Radius: 3, Height: 4
Volumn: 113.097
Area: 150.796
```
阅读全文