C++ 运算符重载学习笔记
1. 运算符重载的基本概念
- 作用:运算符重载是指重新定义 C++ 中已有的运算符,使其能够用于用户自定义的数据类型。
- 实现方式:通过定义成员函数或全局函数来实现运算符的重载。
- 语法:运算符重载函数的名称由
operator
关键字和要重载的运算符组成。
示例代码:
#include <iostream>
using namespace std;
class Complex {
public:
int real;
int imaginary;
Complex operator+(const Complex& other) {
Complex result;
result.real = this->real + other.real;
result.imaginary = this->imaginary + other.imaginary;
return result;
}
void display() {
cout << "Real: " << real << ", Imaginary: " << imaginary << endl;
}
};
int main() {
Complex num1, num2, sum;
num1.real = 3;
num1.imaginary = 4;
num2.real = 1;
num2.imaginary = 2;
sum = num1 + num2;
sum.display();
return 0;
}
2. 成员函数重载运算符
- 语法:重载运算符的函数作为类的成员函数,用于两个对象之间的运算。
- this 指针:成员函数重载运算符可以访问类的私有成员,并且可以通过
this
指针访问当前对象。
示例代码:
算术运算符重载
#include <iostream>
using namespace std;
class Complex {
public:
int real;
int imaginary;
Complex(int r, int i) : real(r), imaginary(i) {}
// 加法运算符重载
Complex operator+(const Complex& other) {
Complex result(real + other.real, imaginary + other.imaginary);
return result;
}
// 减法运算符重载
Complex operator-(const Complex& other) {
Complex result(real - other.real, imaginary - other.imaginary);
return result;
}
// 乘法运算符重载
Complex operator*(const Complex& other) {
Complex result(real * other.real - imaginary * other.imaginary,
real * other.imaginary + imaginary * other.real);
return result;
}
// 除法运算符重载
Complex operator/(const Complex& other) {
int denominator = other.real * other.real + other.imaginary * other.imaginary;
Complex result((real * other.real + imaginary * other.imaginary) / denominator,
(imaginary * other.real - real * other.imaginary) / denominator);
return result;
}
void display() {
cout << "Real: " << real << ", Imaginary: " << imaginary << endl;
}
};
int main() {
Complex num1(3, 4), num2(1, 2), result;
result = num1 + num2; // 调用加法运算符重载
result.display();
result = num1 - num2; // 调用减法运算符重载
result.display();
result = num1 * num2; // 调用乘法运算符重载
result.display();
result = num1 / num2; // 调用除法运算符重载
result.display();
return 0;
}
关系运算符重载
#include <iostream>
using namespace std;
class Complex {
public:
int real;
int imaginary;
Complex(int r, int i) : real(r), imaginary(i) {}
// 相等运算符重载
bool operator==(const Complex& other) {
return (real == other.real) && (imaginary == other.imaginary);
}
// 不等运算符重载
bool operator!=(const Complex& other) {
return !(*this == other);
}
};
int main() {
Complex num1(3, 4), num2(3, 4), num3(1, 2);
if (num1 == num2) { // 调用相等运算符重载
cout << "num1 and num2 are equal" << endl;
}
if (num1 != num3) { // 调用不等运算符重载
cout << "num1 and num3 are not equal" << endl;
}
return 0;
}
赋值运算符重载
#include <iostream>
using namespace std;
class Complex {
public:
int real;
int imaginary;
Complex(int r, int i) : real(r), imaginary(i) {}
// 赋值运算符重载
Complex& operator=(const Complex& other) {
real = other.real;
imaginary = other.imaginary;
return *this;
}
};
int main() {
Complex num1(3, 4), num2(1, 2);
num2 = num1; // 调用赋值运算符重载
cout << "num2: Real - " << num2.real << ", Imaginary - " << num2.imaginary << endl;
return 0;
}
自增自减运算符重载
#include <iostream>
using namespace std;
class Counter {
private:
int count;
public:
Counter() : count(0) {}
// 前置自增运算符重载
Counter& operator++() {
count++;
return *this;
}
// 后置自增运算符重载
Counter operator++(int) {
Counter temp = *this;
count++;
return temp;
}
};
int main() {
Counter c;
++c; // 调用前置自增运算符重载
cout << "Count after pre-increment: " << endl;
c++; // 调用后置自增运算符重载
cout << "Count after post-increment: " << endl;
return 0;
}
成员访问运算符重载
#include <iostream>
using namespace std;
class Complex {
public:
int real;
int imaginary;
Complex(int r, int i) : real(r), imaginary(i) {}
// 成员访问运算符重载
int operator[](int index) {
if (index == 0) return real;
if (index == 1) return imaginary;
return -1; // 无效索引
}
};
int main() {
Complex num(3, 4);
cout << "Real part: " << num[0] << endl; // 调用成员访问运算符重载
cout << "Imaginary part: " << num[1] << endl; // 调用成员访问运算符重载
return 0;
}
函数调用运算符
重载
#include <iostream>
using namespace std;
class FunctionObject {
public:
int operator()(int x, int y) {
return x + y;
}
};
int main() {
FunctionObject add;
cout << "Result: " << add(3, 4) << endl; // 调用函数调用运算符重载
return 0;
}
类型转换运算符重载
#include <iostream>
using namespace std;
class Rational {
private:
int numerator;
int denominator;
public:
Rational(int n = 0, int d = 1) : numerator(n), denominator(d) {}
// 类型转换运算符重载(将 Rational 类型转换为 double 类型)
operator double() const {
return static_cast<double>(numerator) / denominator;
}
};
int main() {
Rational r(5, 2);
double result = static_cast<double>(r); // 调用类型转换运算符重载
cout << "Result: " << result << endl;
return 0;
}
3. 全局函数重载运算符
- 语法:重载运算符的函数作为全局函数,用于一个对象和另一个对象之间的运算,或对象和基本数据类型之间的运算。
- 友元函数:如果需要访问类的私有成员,可以将全局函数声明为友元函数。
示例代码:
#include <iostream>
using namespace std;
class Complex {
friend Complex operator+(const Complex& a, const Complex& b);
public:
int real;
int imaginary;
Complex(int r, int i) : real(r), imaginary(i) {}
void display() {
cout << "Real: " << real << ", Imaginary: " << imaginary << endl;
}
};
Complex operator+(const Complex& a, const Complex& b) {
Complex result(a.real + b.real, a.imaginary + b.imaginary);
return result;
}
int main() {
Complex num1(3, 4), num2(1, 2), sum;
sum = num1 + num2;
sum.display();
return 0;
}
通过运算符重载,我们可以为自定义类型定义自己的行为,使其具有更直观和灵活的操作方式,提高代码的可维护性和可读性。