C++ 重载(运算符和函数)
C++ 允许在同一作用域内为一个函数名或一个运算符指定多个定义,这分别称为函数重载和运算符重载。
重载声明是在同一作用域中与先前声明的声明具有相同名称的声明,只不过这两个声明具有不同的参数和明显不同的定义(实现)。
当您调用重载函数或运算符时,编译器通过将用于调用函数或运算符的参数类型与定义中指定的参数类型进行比较,确定要使用的最合适的定义。选择最合适的重载函数或运算符的过程称为重载决策。
C++ 中的函数重载
您可以在同一范围内对同一函数名称有多个定义。函数的定义必须在参数列表中的参数类型和/或数量上有所不同。您不能重载仅返回类型不同的函数声明。
以下是使用相同函数print()打印不同数据类型的示例-
#include <iostream>
using namespace std;
class printData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
};
int main(void) {
printData pd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");
return 0;
}
当上面的代码被编译并执行时,它会产生以下结果 -
Printing int: 5 Printing float: 500.263 Printing character: Hello C++
C++ 中的运算符重载
您可以重新定义或重载 C++ 中可用的大多数内置运算符。因此,程序员也可以将运算符与用户定义的类型一起使用。
重载运算符是具有特殊名称的函数:关键字“operator”后跟所定义的运算符的符号。与任何其他函数一样,重载运算符具有返回类型和参数列表。
Box operator+(const Box&);
声明可用于添加两个 Box 对象的加法运算符并返回最终的 Box 对象。大多数重载运算符可以定义为普通非成员函数或类成员函数。如果我们将上述函数定义为类的非成员函数,那么我们必须为每个操作数传递两个参数,如下所示 -
Box operator+(const Box&, const Box&);
以下示例展示了使用成员函数进行运算符重载的概念。这里一个对象作为参数传递,其属性将使用该对象进行访问,调用该运算符的对象可以使用该运算符进行访问,如下所述 -
#include <iostream>
using namespace std;
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
void setLength( double len ) {
length = len;
}
void setBreadth( double bre ) {
breadth = bre;
}
void setHeight( double hei ) {
height = hei;
}
// Overload + operator to add two Box objects.
Box operator+(const Box& b) {
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// Main function for the program
int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Box Box3; // Declare Box3 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// Add two object as follows:
Box3 = Box1 + Box2;
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
当上面的代码被编译并执行时,它会产生以下结果 -
Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400
可重载/不可重载运算符
以下是可以重载的运算符列表 -
| + | - | * | / | % | ^ |
| & | | | 〜 | ! | , | = |
| < | > | <= | >= | ++ | -- |
| << | >> | == | != | && | || |
| += | -= | /= | %= | ^= | &= |
| |= | *= | <<= | >>= | [] | () |
| -> | ->* | 新的 | 新的 [] | 删除 | 删除 [] |
以下是不能重载的运算符列表 -
| :: | .* | 。 | ?: |
运算符重载示例
以下是各种运算符重载示例,可帮助您理解该概念。
| 先生编号 | 运算符和示例 |
|---|---|
| 1 | 一元运算符重载 |
| 2 | 二元运算符重载 |
| 3 | 关系运算符重载 |
| 4 | 输入/输出运算符重载 |
| 5 | ++ 和 -- 运算符重载 |
| 6 | 赋值运算符重载 |
| 7 | 函数调用 () 运算符重载 |
| 8 | 下标 [] 运算符重载 |
| 9 | 类成员访问运算符 -> 重载 |