第12课: 运算符重载
运算符重载基础
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// 重载+运算符
Complex operator+(const Complex &other) {
return Complex(real + other.real, imag + other.imag);
}
// 重载-运算符
Complex operator-(const Complex &other) {
return Complex(real - other.real, imag - other.imag);
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(3, 4);
Complex c2(1, 2);
Complex c3 = c1 + c2; // 使用重载的+运算符
Complex c4 = c1 - c2;
c3.display();
c4.display();
return 0;
}
重载输入输出运算符
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
Point(int x = 0, int y = 0) : x(x), y(y) {}
// 友元函数重载<<运算符
friend ostream& operator<<(ostream &out, const Point &p) {
out << "(" << p.x << ", " << p.y << ")";
return out;
}
// 友元函数重载>>运算符
friend istream& operator>>(istream &in, Point &p) {
cout << "输入x和y:";
in >> p.x >> p.y;
return in;
}
};
int main() {
Point p1(3, 4);
cout << "点p1:" << p1 << endl;
Point p2;
cin >> p2;
cout << "点p2:" << p2 << endl;
return 0;
}
重载比较运算符
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int score;
public:
Student(string n, int s) : name(n), score(s) {}
// 重载==运算符
bool operator==(const Student &other) const {
return score == other.score;
}
// 重载!=运算符
bool operator!=(const Student &other) const {
return score != other.score;
}
// 重载<运算符
bool operator<(const Student &other) const {
return score < other.score;
}
// 重载>运算符
bool operator>(const Student &other) const {
return score > other.score;
}
void display() const {
cout << name << ": " << score << "分" << endl;
}
};
int main() {
Student s1("张三", 85);
Student s2("李四", 90);
if (s1 < s2) {
cout << "张三分数低于李四" << endl;
}
if (s1 != s2) {
cout << "两人分数不同" << endl;
}
return 0;
}
重载下标运算符
#include <iostream>
using namespace std;
class Array {
private:
int *data;
int size;
public:
Array(int s) : size(s) {
data = new int[size];
for (int i = 0; i < size; i++) {
data[i] = 0;
}
}
~Array() {
delete[] data;
}
// 重载[]运算符
int& operator[](int index) {
if (index < 0 || index >= size) {
cout << "索引越界!" << endl;
exit(1);
}
return data[index];
}
int getSize() const {
return size;
}
};
int main() {
Array arr(5);
// 使用[]运算符赋值
for (int i = 0; i < arr.getSize(); i++) {
arr[i] = i * 10;
}
// 使用[]运算符访问
for (int i = 0; i < arr.getSize(); i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
重载赋值运算符
#include <iostream>
#include <cstring>
using namespace std;
class String {
private:
char *str;
public:
String(const char *s = "") {
str = new char[strlen(s) + 1];
strcpy(str, s);
}
~String() {
delete[] str;
}
// 重载=运算符(深拷贝)
String& operator=(const String &other) {
if (this != &other) { // 防止自我赋值
delete[] str;
str = new char[strlen(other.str) + 1];
strcpy(str, other.str);
}
return *this;
}
void display() const {
cout << str << endl;
}
};
int main() {
String s1("Hello");
String s2("World");
s2 = s1; // 使用重载的=运算符
s1.display();
s2.display();
return 0;
}
重载自增自减运算符
#include <iostream>
using namespace std;
class Counter {
private:
int count;
public:
Counter(int c = 0) : count(c) {}
// 前置++
Counter& operator++() {
++count;
return *this;
}
// 后置++(参数int用于区分)
Counter operator++(int) {
Counter temp = *this;
count++;
return temp;
}
// 前置--
Counter& operator--() {
--count;
return *this;
}
// 后置--
Counter operator--(int) {
Counter temp = *this;
count--;
return temp;
}
int getCount() const {
return count;
}
};
int main() {
Counter c(5);
cout << "初始值:" << c.getCount() << endl;
cout << "++c:" << (++c).getCount() << endl;
cout << "c++:" << (c++).getCount() << endl;
cout << "当前值:" << c.getCount() << endl;
return 0;
}
重载函数调用运算符
#include <iostream>
using namespace std;
class Adder {
public:
// 重载()运算符
int operator()(int a, int b) {
return a + b;
}
double operator()(double a, double b) {
return a + b;
}
};
int main() {
Adder add;
cout << "5 + 3 = " << add(5, 3) << endl;
cout << "5.5 + 3.2 = " << add(5.5, 3.2) << endl;
return 0;
}
练习题
- 实现一个分数类,重载四则运算符
- 创建一个矩阵类,重载+、-、*运算符
- 实现一个字符串类,重载+、==、[]运算符
- 设计一个日期类,重载比较运算符和++运算符