C++ 面向对象编程:基于 Point 类和 Plane 类设计圆类 Circle 并实现运算符重载和文件读写
C++ 面向对象编程:基于 Point 类和 Plane 类设计圆类 Circle 并实现运算符重载和文件读写
1. Point 类
类结构说明:
- 数据成员:
- 私有数据成员:X 坐标
x(double 型),Y 坐标y(double 型)。
- 私有数据成员:X 坐标
- 成员函数:
- 有参构造函数
Point(double, double),其中参数分别为 X 坐标和 Y 坐标,默认值均为 0。 - 公有函数成员
void setX(double)和double getX() const分别返回和设置 X 坐标。 - 公有函数成员
void setY(double)和double getY() const分别返回和设置 Y 坐标。 - 重载运算符
<<以实现 Point 对象的格式输出,输出格式要求:
例如:(<X坐标>,<Y坐标>)(1.0,2.0)等。 - 重载运算符
>>以实现 Point 对象的格式输入,输入格式要求:
例如:(<X坐标>,<Y坐标>)(1.0,2.0)等。
- 有参构造函数
2. Plane 类
类结构说明:
- 成员函数:
- 纯虚函数
virtual double length()const用于计算平面图形的周长。 - 纯虚函数
virtual double area()const用于计算平面图形的面积。
- 纯虚函数
3. Circle 类
类结构说明:
- 继承: 公有派生圆类 Circle 以点类 Point、平面图形类 Plane 为基类。
- 数据成员:
- 圆心坐标继承自 Point 类。
- 保护静态数据常量
PI(double 型),其值为 3.14159。 - 私有数据成员:半径
radius(double 型)。
- 成员函数:
- 有参构造函数
Circle(double, double, double),其中函数参数包括圆心坐标和半径,圆心调用 Point 类构造函数进行构造,各参数默认值为 0。 - 公有函数成员
void setR(double)和double getR()const分别返回和设置radius。 - 重载
double area()const用于计算圆的面积。 - 重载
double length()const用于计算圆的周长。 - 重载运算符
<<以实现 Circle 对象的格式输出,输出格式要求:
例如:((<X坐标>,<Y坐标>),<半径>)((1.0,2.0),3.0)等。 - 重载运算符
>>以实现 Circle 对象的格式输入,输入格式要求:((<X坐标>,<Y坐标>),<半径>)
- 有参构造函数
4. main(void) 函数
操作顺序:
- 首先已输入的
d1、d2来构造 Point 对象p,并将p用<<写入文本文件s3.txt中。 - 再将文本文件
s3.txt打开,应用>>操作将里面的数据读入到 Point 对象p中。 - 利用 Point 对象
p的数据设置 Circle 对象c,设置要求:将c的圆心 X 坐标设置为p的 Y 坐标,将c的圆心 Y 坐标设置为p的 X 坐标,将c的半径设置为p的 Y 坐标与 X 坐标的和。并将设置好的c写入二进制文件b3.dat中。 - 再将二进制文件
b3.dat打开,将里面的数据读入到 Circle 对象c中。 - 最后以
c对象为参数调用f函数。
5. 代码示例
// Point 类定义
class Point {
private:
double x, y;
public:
Point(double _x = 0, double _y = 0) : x(_x), y(_y) {}
void setX(double _x) { x = _x; }
double getX() const { return x; }
void setY(double _y) { y = _y; }
double getY() const { return y; }
friend std::ostream& operator<<(std::ostream& out, const Point& p) {
out << '(' << p.x << ',' << p.y << ')';
return out;
}
friend std::istream& operator>>(std::istream& in, Point& p) {
char c;
in >> c >> p.x >> c >> p.y >> c;
return in;
}
};
// Plane 类定义
class Plane {
public:
virtual double length() const = 0;
virtual double area() const = 0;
};
// Circle 类定义
class Circle : public Point, public Plane {
protected:
static const double PI = 3.14159;
private:
double radius;
public:
Circle(double _x = 0, double _y = 0, double _radius = 0) : Point(_x, _y), radius(_radius) {}
void setR(double _radius) { radius = _radius; }
double getR() const { return radius; }
double area() const override { return PI * radius * radius; }
double length() const override { return 2 * PI * radius; }
friend std::ostream& operator<<(std::ostream& out, const Circle& c) {
out << '((' << c.getX() << ',' << c.getY() << '),' << c.radius << ')';
return out;
}
friend std::istream& operator>>(std::istream& in, Circle& c) {
char c1, c2;
in >> c1 >> c.getX() >> c1 >> c.getY() >> c1 >> c.radius >> c2;
return in;
}
};
// main 函数
int main() {
double d1, d2;
std::cout << '请输入 Point 对象的坐标 (x, y):';
std::cin >> d1 >> d2;
Point p(d1, d2);
// 将 Point 对象写入文本文件
std::ofstream outfile('s3.txt');
outfile << p;
outfile.close();
// 从文本文件读取 Point 对象
std::ifstream infile('s3.txt');
infile >> p;
infile.close();
// 设置 Circle 对象
Circle c(p.getY(), p.getX(), p.getY() + p.getX());
// 将 Circle 对象写入二进制文件
std::ofstream outfile2('b3.dat', std::ios::binary);
outfile2.write(reinterpret_cast<char*>(&c), sizeof(c));
outfile2.close();
// 从二进制文件读取 Circle 对象
std::ifstream infile2('b3.dat', std::ios::binary);
infile2.read(reinterpret_cast<char*>(&c), sizeof(c));
infile2.close();
// 调用 f 函数
// ...
return 0;
}
6. 总结
本示例展示了如何使用 C++ 面向对象编程设计一个 Circle 类,该类继承自 Point 类和 Plane 类,并重载了 << 和 >> 运算符以方便对象输入输出。此外,示例还展示了如何使用文件读写操作来存储和读取 Point 和 Circle 对象。
原文地址: https://www.cveoy.top/t/topic/ok20 著作权归作者所有。请勿转载和采集!