定义Point(点)类,由Point类派生出Circle(圆)类,再由Circle类派生出Cylinder(圆柱体)类。将类的声明部分分别作为3个头文件,对它们的成员函数的定义部分分别作为3个源文件(.cpp文件),在主函数中用#include命令把它们包含进来,形成一个完整的程序,并上机运行。c++编程,并重载输入输出运算符
时间: 2024-02-17 20:04:38 浏览: 74
定义三个类Point,Circle和Cylinder,Point类为基类,为上述3个类添加计算面积的成员函数Area(),要求函数Area()采用虚函数的形式,并通过基类指针调用虚函数Area()。
5星 · 资源好评率100%
好的,以下是Point类、Circle类和Cylinder类的声明部分:
Point.h
```cpp
#ifndef POINT_H
#define POINT_H
#include <iostream>
class Point {
public:
Point(double x = 0, double y = 0); // 构造函数
double getX() const; // 获取x坐标
double getY() const; // 获取y坐标
void setX(double x); // 设置x坐标
void setY(double y); // 设置y坐标
friend std::ostream& operator<<(std::ostream& out, const Point& p); // 重载输出运算符
friend std::istream& operator>>(std::istream& in, Point& p); // 重载输入运算符
private:
double x, y; // 坐标
};
#endif
```
Circle.h
```cpp
#ifndef CIRCLE_H
#define CIRCLE_H
#include "Point.h"
#include <iostream>
class Circle : public Point {
public:
Circle(double x = 0, double y = 0, double r = 0); // 构造函数
double getR() const; // 获取半径
void setR(double r); // 设置半径
double getArea() const; // 获取面积
friend std::ostream& operator<<(std::ostream& out, const Circle& c); // 重载输出运算符
friend std::istream& operator>>(std::istream& in, Circle& c); // 重载输入运算符
private:
double r; // 半径
};
#endif
```
Cylinder.h
```cpp
#ifndef CYLINDER_H
#define CYLINDER_H
#include "Circle.h"
#include <iostream>
class Cylinder : public Circle {
public:
Cylinder(double x = 0, double y = 0, double r = 0, double h = 0); // 构造函数
double getH() const; // 获取高度
void setH(double h); // 设置高度
double getVolume() const; // 获取体积
friend std::ostream& operator<<(std::ostream& out, const Cylinder& cy); // 重载输出运算符
friend std::istream& operator>>(std::istream& in, Cylinder& cy); // 重载输入运算符
private:
double h; // 高度
};
#endif
```
接下来是Point类、Circle类和Cylinder类的定义部分:
Point.cpp
```cpp
#include "Point.h"
Point::Point(double x, double y) : x(x), y(y) {}
double Point::getX() const {
return x;
}
double Point::getY() const {
return y;
}
void Point::setX(double x) {
this->x = x;
}
void Point::setY(double y) {
this->y = y;
}
std::ostream& operator<<(std::ostream& out, const Point& p) {
out << "(" << p.x << ", " << p.y << ")";
return out;
}
std::istream& operator>>(std::istream& in, Point& p) {
in >> p.x >> p.y;
return in;
}
```
Circle.cpp
```cpp
#include "Circle.h"
#include <cmath>
Circle::Circle(double x, double y, double r) : Point(x, y), r(r) {}
double Circle::getR() const {
return r;
}
void Circle::setR(double r) {
this->r = r;
}
double Circle::getArea() const {
return M_PI * r * r;
}
std::ostream& operator<<(std::ostream& out, const Circle& c) {
out << "圆心:" << static_cast<const Point&>(c) << ",半径:" << c.r;
return out;
}
std::istream& operator>>(std::istream& in, Circle& c) {
in >> static_cast<Point&>(c) >> c.r;
return in;
}
```
Cylinder.cpp
```cpp
#include "Cylinder.h"
#include <cmath>
Cylinder::Cylinder(double x, double y, double r, double h) : Circle(x, y, r), h(h) {}
double Cylinder::getH() const {
return h;
}
void Cylinder::setH(double h) {
this->h = h;
}
double Cylinder::getVolume() const {
return getArea() * h;
}
std::ostream& operator<<(std::ostream& out, const Cylinder& cy) {
out << "底面圆:" << static_cast<const Circle&>(cy) << ",高:" << cy.h << ",体积:" << cy.getVolume();
return out;
}
std::istream& operator>>(std::istream& in, Cylinder& cy) {
in >> static_cast<Circle&>(cy) >> cy.h;
return in;
}
```
最后是主函数:
main.cpp
```cpp
#include "Cylinder.h"
#include <iostream>
using namespace std;
int main() {
Cylinder cyl;
cout << "请输入圆柱体的底面圆的圆心坐标和半径:";
cin >> cyl;
cout << "请输入圆柱体的高:";
cin >> cyl.setH;
cout << "圆柱体的信息:" << cyl << endl;
return 0;
}
```
编译链接后运行,输入圆心坐标和半径,再输入高度,输出圆柱体的信息,例如:
```
请输入圆柱体的底面圆的圆心坐标和半径:1 2 3
请输入圆柱体的高:4
圆柱体的信息:底面圆:圆心:(1, 2),半径:3,高:4,体积:113.097
```
阅读全文