C++ 面向对象编程:设计圆类并重载运算符
C++ 面向对象编程:设计圆类并重载运算符
本教程将详细讲解如何使用 C++ 面向对象编程,基于 Point 类和 Plane 类设计圆类 Circle。此外,教程还涵盖了对 Point 类和 Circle 类进行重载运算符 '<' 和 '>' 操作,并实现 Point 对象和 Circle 对象的文件读写操作。
Point 类结构说明
Point 类的数据成员包括:
- 私有数据成员:X 坐标 'x' (double 型),Y 坐标 'y' (double 型)。
Point 类成员函数包括:
-
有参构造函数 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)' 等。
Plane 类
Plane 类的成员函数包括:
- 纯虚函数 virtual double length() const 用于计算平面图形的周长。
- 纯虚函数 virtual double area() const 用于计算平面图形的面积。
Circle 类结构说明
公有派生圆类 Circle 以点类 Point、平面图形类 Plane 为基类,Circle 类的结构说明如下:
Circle 类的数据成员包括:
- 圆心坐标继承自 Point 类。
- 保护静态数据常量 PI (double 型),其值为 3.14159。
- 私有数据成员:半径 radius (double 型)。
Circle 类成员函数包括:
-
有参构造函数 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 坐标>), <半径>)
例如:'((1.0, 2.0), 3.0)' 等。
代码示例
// Point.h
#ifndef POINT_H
#define POINT_H
class Point {
private:
double x;
double y;
public:
Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}
void setX(double x) { this->x = x; }
double getX() const { return x; }
void setY(double y) { this->y = y; }
double getY() const { return y; }
friend std::ostream& operator<<(std::ostream& out, const Point& p); // 重载 << 运算符
friend std::istream& operator>>(std::istream& in, Point& p); // 重载 >> 运算符
};
std::ostream& operator<<(std::ostream& out, const Point& p) {
out << '(' << p.x << ',' << p.y << ')';
return out;
}
std::istream& operator>>(std::istream& in, Point& p) {
char c; // 用于读取括号和逗号
in >> c; // 读取左括号
in >> p.x; // 读取 x 坐标
in >> c; // 读取逗号
in >> p.y; // 读取 y 坐标
in >> c; // 读取右括号
return in;
}
#endif // POINT_H
// Plane.h
#ifndef PLANE_H
#define PLANE_H
class Plane {
public:
virtual double length() const = 0;
virtual double area() const = 0;
};
#endif // PLANE_H
// Circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
#include "Point.h"
#include "Plane.h"
class Circle : public Point, public Plane {
protected:
static const double PI = 3.14159;
private:
double radius;
public:
Circle(double x = 0.0, double y = 0.0, double radius = 0.0) : Point(x, y), radius(radius) {}
void setR(double radius) { this->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); // 重载 << 运算符
friend std::istream& operator>>(std::istream& in, Circle& c); // 重载 >> 运算符
};
std::ostream& operator<<(std::ostream& out, const Circle& c) {
out << '(' << c.getX() << ',' << c.getY() << ')' << ',' << c.radius; // 输出圆心坐标和半径
return out;
}
std::istream& operator>>(std::istream& in, Circle& c) {
char c1, c2, c3; // 用于读取括号和逗号
in >> c1; // 读取左括号
in >> c.getX(); // 读取 x 坐标
in >> c2; // 读取逗号
in >> c.getY(); // 读取 y 坐标
in >> c3; // 读取右括号
in >> c.radius; // 读取半径
return in;
}
#endif // CIRCLE_H
// main.cpp
#include <iostream>
#include <fstream>
#include "Circle.h"
int main() {
// 创建 Circle 对象
Circle circle1(1.0, 2.0, 3.0);
std::cout << "Circle 1: " << circle1 << std::endl;
// 从文件中读取 Circle 对象
std::ifstream inputFile("circle.txt");
Circle circle2;
inputFile >> circle2;
std::cout << "Circle 2: " << circle2 << std::endl;
inputFile.close();
// 将 Circle 对象写入文件
std::ofstream outputFile("circle.txt");
outputFile << circle1 << std::endl;
outputFile.close();
return 0;
}
总结
本教程演示了如何使用 C++ 面向对象编程设计圆类并重载运算符。通过重载运算符,我们可以方便地进行对象输入输出,并实现文件读写操作。
原文地址: https://www.cveoy.top/t/topic/ok3b 著作权归作者所有。请勿转载和采集!