C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载。
重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义(实现)不相同。
当您调用一个重载函数或重载运算符时,编译器通过把您所使用的参数类型与定义中的参数类型进行比较,决定选用最合适的定义。选择最合适的重载函数或重载运算符的过程,称为重载决策。
c++中函数的重载
实例
同名函数 print() 被用于输出不同的数据类型:
#include <iostream>
using namespace std;
class printData
{
public:
void print(int i) {
cout << "整数为: " << i << endl;
}
void print(double f) {
cout << "浮点数为: " << f << endl;
}
void print(char c[]) {
cout << "字符串为: " << c << endl;
}
};
int main(void)
{
printData pd;
// 输出整数
pd.print(5);
// 输出浮点数
pd.print(500.263);
// 输出字符串
char c[] = "Hello C++";
pd.print(c);
return 0;
}
可以发现这里会自动识别我们输入的类型 之后自动输出
c++中运算重载符
您可以重定义或者重载大部分c++内部的运算符,这样就可以使用自定义的运算符
重载的运算符是带有特殊名称的函数,函数名称由关键字operator和其后要重载的运算符符号构成。和其他函数一样,重载运算符有一个返回类型和参数列表
然后我就依次给出实例供大家理解
单目运算符
下面我先给出一个进行取反运算的操作
#include <iostream>
using namespace std;
class TestClass {
public :
int Testa;
int Testb;
TestClass() {
//构造函数
Testa = 0;
Testa = 0;
}
TestClass(int a, int b) {
Testa = a;
Testb = b;
}
void DisplayDate() {
cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
}
TestClass operator-() {
Testa = -Testa;
Testb = -Testb;
return TestClass(Testa, Testb);
}
};
int main() {
TestClass tc1(1, 1), tc2(2, 2);
-tc1;
tc1.DisplayDate();
tc2.DisplayDate();
return 0;
}
从上边的例子我相信大家可以慢慢看懂的
双目运算符
我这个代码是在上个的基础上添加的
#include <iostream>
using namespace std;
class TestClass {
public :
int Testa;
int Testb;
TestClass() {
//构造函数
Testa = 0;
Testa = 0;
}
TestClass(int a, int b) {
Testa = a;
Testb = b;
}
void DisplayDate() {
cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
}
//取反操作重载
TestClass operator-() {
Testa = -Testa;
Testb = -Testb;
return TestClass(Testa, Testb);
}
//加法操作重载
TestClass operator+(const TestClass& b) {
TestClass t;
t.Testa = Testa + b.Testa;
t.Testb = Testb + b.Testb;
return t;
}
};
int main() {
TestClass tc1(1, 1), tc2(2, 2);
-tc1;
tc1.DisplayDate();
tc2.DisplayDate();
TestClass tc3 = tc1 + tc2;
tc3.DisplayDate();
return 0;
}
关系运算符
#include <iostream>
using namespace std;
class TestClass {
public :
int Testa;
int Testb;
TestClass() {
//构造函数
Testa = 0;
Testa = 0;
}
TestClass(int a, int b) {
Testa = a;
Testb = b;
}
void DisplayDate() {
cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
}
//取反操作重载
TestClass operator-() {
Testa = -Testa;
Testb = -Testb;
return TestClass(Testa, Testb);
}
//加法操作重载
TestClass operator+(const TestClass& b) {
TestClass t;
t.Testa = Testa + b.Testa;
t.Testb = Testb + b.Testb;
return t;
}
//重载小于(<)运算符
bool operator<(const TestClass& d) {
if (Testa < d.Testa && Testb < d.Testb) {
return true;
}
else {
return false;
}
}
};
int main() {
TestClass tc1(1, 1), tc2(2, 2);
-tc1;
tc1.DisplayDate();
tc2.DisplayDate();
TestClass tc3 = tc1 + tc2;
tc3.DisplayDate();
if (tc1 < tc2) {
cout << "tc1小于tc2" << endl;
}
else {
cout << "tc1不小于tc2" << endl;
}
return 0;
}
通过这种运算符 我们可以轻易的判断两个对象之间的一些关系等
++和–运算符重载
#include <iostream>
using namespace std;
class TestClass {
public :
int Testa;
int Testb;
TestClass() {
//构造函数
Testa = 0;
Testa = 0;
}
TestClass(int a, int b) {
Testa = a;
Testb = b;
}
void DisplayDate() {
cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
}
//取反操作重载
TestClass operator-() {
Testa = -Testa;
Testb = -Testb;
return TestClass(Testa, Testb);
}
//加法操作重载
TestClass operator+(const TestClass& b) {
TestClass t;
t.Testa = Testa + b.Testa;
t.Testb = Testb + b.Testb;
return t;
}
//重载小于(<)运算符
bool operator<(const TestClass& d) {
if (Testa < d.Testa && Testb < d.Testb) {
return true;
}
else {
return false;
}
}
//重载前缀自增运算符
TestClass operator++() {
++Testa;
++Testb;
return TestClass(Testa, Testb);
}
//重载后缀自增运算符
TestClass operator++(int) {
TestClass tc(Testa, Testb);
Testa++;
Testb++;
return tc;
}
};
int main() {
TestClass tc1(1, 1), tc2(2, 2);
-tc1;
tc1.DisplayDate();
tc2.DisplayDate();
cout << "------------------------" << endl;
TestClass tc3 = tc1 + tc2;
tc3.DisplayDate();
cout << "------------------------" << endl;
if (tc1 < tc2) {
cout << "tc1小于tc2" << endl;
}
else {
cout << "tc1不小于tc2" << endl;
}
cout << "------------------------" << endl;
tc1++;
tc1.DisplayDate();
tc1++;
tc1.DisplayDate();
++tc2;
tc2.DisplayDate();
++tc2;
tc2.DisplayDate();
cout << "------------------------" << endl;
return 0;
}
赋值运算符重载
#include <iostream>
using namespace std;
class TestClass {
public :
int Testa;
int Testb;
TestClass() {
//构造函数
Testa = 0;
Testa = 0;
}
TestClass(int a, int b) {
Testa = a;
Testb = b;
}
void DisplayDate() {
cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
}
//取反操作重载
TestClass operator-() {
Testa = -Testa;
Testb = -Testb;
return TestClass(Testa, Testb);
}
//加法操作重载
TestClass operator+(const TestClass& b) {
TestClass t;
t.Testa = Testa + b.Testa;
t.Testb = Testb + b.Testb;
return t;
}
//重载小于(<)运算符
bool operator<(const TestClass& d) {
if (Testa < d.Testa && Testb < d.Testb) {
return true;
}
else {
return false;
}
}
//重载前缀自增运算符
TestClass operator++() {
++Testa;
++Testb;
return TestClass(Testa, Testb);
}
//重载后缀自增运算符
TestClass operator++(int) {
TestClass tc(Testa, Testb);
Testa++;
Testb++;
return tc;
}
//赋值运算符重载
void operator=(const TestClass &d) {
Testa = d.Testa;
Testb = d.Testb;
}
};
int main() {
TestClass tc1(1, 1), tc2(2, 2);
-tc1;
tc1.DisplayDate();
tc2.DisplayDate();
cout << "------------------------" << endl;
TestClass tc3 = tc1 + tc2;
tc3.DisplayDate();
cout << "------------------------" << endl;
if (tc1 < tc2) {
cout << "tc1小于tc2" << endl;
}
else {
cout << "tc1不小于tc2" << endl;
}
cout << "------------------------" << endl;
tc1++;
tc1.DisplayDate();
tc1++;
tc1.DisplayDate();
++tc2;
tc2.DisplayDate();
++tc2;
tc2.DisplayDate();
cout << "------------------------" << endl;
TestClass tc4 = tc1;
tc4.DisplayDate();
cout << "------------------------" << endl;
return 0;
}
函数调用运算符()重载
#include <iostream>
using namespace std;
class TestClass {
public :
int Testa;
int Testb;
TestClass() {
//构造函数
Testa = 0;
Testa = 0;
}
TestClass(int a, int b) {
Testa = a;
Testb = b;
}
void DisplayDate() {
cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
}
//取反操作重载
TestClass operator-() {
Testa = -Testa;
Testb = -Testb;
return TestClass(Testa, Testb);
}
//加法操作重载
TestClass operator+(const TestClass& b) {
TestClass t;
t.Testa = Testa + b.Testa;
t.Testb = Testb + b.Testb;
return t;
}
//重载小于(<)运算符
bool operator<(const TestClass& d) {
if (Testa < d.Testa && Testb < d.Testb) {
return true;
}
else {
return false;
}
}
//重载前缀自增运算符
TestClass operator++() {
++Testa;
++Testb;
return TestClass(Testa, Testb);
}
//重载后缀自增运算符
TestClass operator++(int) {
TestClass tc(Testa, Testb);
Testa++;
Testb++;
return tc;
}
//赋值运算符重载
void operator=(const TestClass &d) {
Testa = d.Testa;
Testb = d.Testb;
}
//函数运算符()重载
TestClass operator()(int a,int b) {
Testa += a;
Testb += b;
return TestClass(Testa, Testb);
}
};
int main() {
TestClass tc1(1, 1), tc2(2, 2);
-tc1;
tc1.DisplayDate();
tc2.DisplayDate();
cout << "------------------------" << endl;
TestClass tc3 = tc1 + tc2;
tc3.DisplayDate();
cout << "------------------------" << endl;
if (tc1 < tc2) {
cout << "tc1小于tc2" << endl;
}
else {
cout << "tc1不小于tc2" << endl;
}
cout << "------------------------" << endl;
tc1++;
tc1.DisplayDate();
tc1++;
tc1.DisplayDate();
++tc2;
tc2.DisplayDate();
++tc2;
tc2.DisplayDate();
cout << "------------------------" << endl;
TestClass tc4 = tc1;
tc4.DisplayDate();
cout << "------------------------" << endl;
tc1(1, 1);
tc1.DisplayDate();
cout << "------------------------" << endl;
return 0;
}
输入输出运算符重载
#include <iostream>
using namespace std;
class TestClass {
public :
int Testa;
int Testb;
TestClass() {
//构造函数
Testa = 0;
Testa = 0;
}
TestClass(int a, int b) {
Testa = a;
Testb = b;
}
void DisplayDate() {
cout << "Testa:"<<Testa << " " <<"Testb:"<<Testb << endl;
}
//取反操作重载
TestClass operator-() {
Testa = -Testa;
Testb = -Testb;
return TestClass(Testa, Testb);
}
//加法操作重载
TestClass operator+(const TestClass& b) {
TestClass t;
t.Testa = Testa + b.Testa;
t.Testb = Testb + b.Testb;
return t;
}
//重载小于(<)运算符
bool operator<(const TestClass& d) {
if (Testa < d.Testa && Testb < d.Testb) {
return true;
}
else {
return false;
}
}
//重载前缀自增运算符
TestClass operator++() {
++Testa;
++Testb;
return TestClass(Testa, Testb);
}
//重载后缀自增运算符
TestClass operator++(int) {
TestClass tc(Testa, Testb);
Testa++;
Testb++;
return tc;
}
//赋值运算符重载
void operator=(const TestClass &d) {
Testa = d.Testa;
Testb = d.Testb;
}
//函数运算符()重载
TestClass operator()(int a,int b) {
Testa += a;
Testb += b;
return TestClass(Testa, Testb);
}
//输入输出运算符重载
friend ostream& operator<<(ostream& output, const TestClass& d) {
output << "Testa:" << d.Testa << " " << "Testb:" << d.Testb << endl;
return output;
}
friend istream& operator>>(istream& input, TestClass& D)
{
input >> D.Testa >> D.Testb;
return input;
}
};
int main() {
TestClass tc1(1, 1), tc2(2, 2);
-tc1;
tc1.DisplayDate();
tc2.DisplayDate();
cout << "------------------------" << endl;
TestClass tc3 = tc1 + tc2;
tc3.DisplayDate();
cout << "------------------------" << endl;
if (tc1 < tc2) {
cout << "tc1小于tc2" << endl;
}
else {
cout << "tc1不小于tc2" << endl;
}
cout << "------------------------" << endl;
tc1++;
tc1.DisplayDate();
tc1++;
tc1.DisplayDate();
++tc2;
tc2.DisplayDate();
++tc2;
tc2.DisplayDate();
cout << "------------------------" << endl;
TestClass tc4 = tc1;
tc4.DisplayDate();
cout << "------------------------" << endl;
tc1(1, 1);
tc1.DisplayDate();
cout << "------------------------" << endl;
TestClass tc5;
cin >> tc5;
cout << tc5;
return 0;
}
下标运算符[]重载
下标操作符[]可以用来a耐高温数组元素 可以增强我们自己数组的功能
int& operator[](int i)
{
if (i > elems.size())
{
cout << "索引超过最大值" << endl;
// 返回第一个元素
return elems[0];//elems为数组元素
}
return elems[i];
}
最常用就是我上边写的这些了 希望我写的对大家有所帮助
欢迎大家关注我的账号 有问题可以去主页联系我
我是头号理想 大家一起进步