C++操作符重载实现合集
C++操作符重载实现合集
本文包括+=,-=,前++,后++,前–,后–,左移<<,右移>>等操作符的代码实现
① +=操作符重载,实现代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Complex {
public:
Complex(int a, int b) {
this->a = a;
this->b = b;
}
void print() {
cout << "a=" << this->a << " b=" << this->b << endl;
}
friend Complex& operator+=(Complex& c1, Complex& c2);
局部重载的函数实现
Complex& operator+=(Complex& c2) { //局部
this->a += c2.a;
this->b += c2.b;
return *this;//返回自己!!!
}
private:
int a;
int b;
};
#if 0
//全局函数
Complex& operator+=(Complex& c1, Complex& c2) { //全局
c1.a += c2.a;
c1.b += c2.b;
return c1;
}
#endif
int main() {
Complex c1(10, 20);
Complex c2(30, 40);
//c1 += c2;//全局重载函数的调用方法
c1.operator+=(c2);//局部操作符函数重载的调用方法
c1.print();
system("pause");
return 0;
}
② -=操作符重载,实现代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Complex {
public:
Complex(int a, int b) {
this->a = a;
this->b = b;
}
void print() {
cout << "a=" << this->a << " b=" << this->b << endl;
}
friend Complex& operator-=(Complex& c1, Complex& c2);
//局部重载的函数实现
Complex& operator-=(Complex& c2) { //局部
this->a -= c2.a;
this->b -= c2.b;
return *this;//返回自己!!!
}
private:
int a;
int b;
};
#if 0
//全局重载的函数实现
Complex& operator-=(Complex& c1, Complex& c2) { //全局
c1.a -= c2.a;
c1.b -= c2.b;
return c1;
}
#endif
int main() {
Complex c1(10, 20);
Complex c2(30, 40);
//c1 += c2; //全局重载函数的调用方法
c1.operator-=(c2);//局部操作符函数重载的调用方法
c1.print();
system("pause");
return 0;
}
③ 前++操作符的重载,实现代码如下:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Complex {
public:
Complex(int a, int b) {
this->a = a;
this->b = b;
}
void print() {
cout << "a=" << this->a << " b=" << this->b << endl;
}
//friend Complex& operator++(Complex &c); //友元函数声明
//局部前++重载函数的实现
Complex& operator++() {
this->a++;
this->b++;
return *this;
}
private:
int a;
int b;
};
//全局前++重载函数的实现
//注意返回值为Complex&(对象的引用),这样才能实现连续的++
#if 0
Complex& operator++(Complex &c) {
c.a++;
c.b++;
return c;
}
#endif
int main() {
Complex c1(10, 20);
++c1;
++++++c1;//连续++的实现
c1.print();
system("pause");
return 0;
}
④ 后++操作符的重载,实现代码如下:
注意:(1)后++与前++有明显区别,后++不能实现连++,因为a++等式的值仍然是a,没有变化,变化的是a的值,故我们在函数前面加了const来确保前++只能用一次,不能连加
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Complex {
public:
Complex(int a, int b) {
this->a = a;
this->b = b;
}
void print() {
cout << "a=" << this->a << " b=" << this->b << endl;
}
friend const Complex& operator++(Complex& c, int);//友元函数声明
//局部后++重载函数的实现
#if 0
const Complex operator++(int) {
Complex temp(this->a, this->b);//用temp来保存a++前的值
this->a++;
this->b++;
return temp; //因为a++式子的值没有发生变化,所以返回值为temp(a在++前的值)
}
#endif
private:
int a;
int b;
};
//全局后++重载函数的实现
//注意前面加了const,因为不能连续前++
//注意此处传进函数的是c的引用,因为要在函数内读c的a,b进行++操作
const Complex& operator++(Complex &c,int) {
Complex temp(c.a, c.b);//用temp来保存a++前的值
c.a++;
c.b++;
return temp; //因为a++式子的值没有发生变化,所以返回值为temp(a在++前的值)
}
int main() {
Complex c1(10, 20);
c1++;
c1.print();
system("pause");
return 0;
}
⑤前–操作符的重载,实现代码如下:
前- -与前++只有符号不同
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Complex {
public:
Complex(int a, int b) {
this->a = a;
this->b = b;
}
void print() {
cout << "a=" << this->a << " b=" << this->b << endl;
}
//friend Complex& operator--(Complex &c); //友元函数声明
//局部前--重载函数的实现
Complex& operator--() {
this->a--;
this->b--;
return *this;
}
private:
int a;
int b;
};
//全局前--重载函数的实现
//注意返回值为Complex&(对象的引用),这样才能实现连续的--
#if 0
Complex& operator--(Complex &c) {
c.a--;
c.b--;
return c;
}
#endif
int main() {
Complex c1(10, 20);
--c1;
------c1;//连续--的实现
c1.print();
system("pause");
return 0;
}
⑥后–操作符的重载,实现代码如下:
后–与后++只有符号不同
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Complex {
public:
Complex(int a, int b) {
this->a = a;
this->b = b;
}
void print() {
cout << "a=" << this->a << " b=" << this->b << endl;
}
friend const Complex& operator--(Complex& c, int);//友元函数声明
//局部后++重载函数的实现
#if 0
const Complex operator--(int) {
Complex temp(this->a, this->b);//用temp来保存a--前的值
this->a--;
this->b--;
return temp; //因为a--式子的值没有发生变化,所以返回值为temp(a在--前的值)
}
#endif
private:
int a;
int b;
};
//全局后++重载函数的实现
//注意前面加了const,因为不能连续前--
//注意此处传进函数的是c的引用,因为要在函数内读c的a,b进行--操作
const Complex& operator--(Complex &c, int) {
Complex temp(c.a, c.b);//用temp来保存a--前的值
c.a--;
c.b--;
return temp; //因为a--式子的值没有发生变化,所以返回值为temp(a在++前的值)
}
int main() {
Complex c1(10, 20);
c1--;
c1.print();
system("pause");
return 0;
}
⑦左移<<操作符的重载,实现代码如下:
ps: ostream是cout的数据类型
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Complex {
public:
Complex(int a, int b) {
this->a = a;
this->b = b;
}
void print() {
cout << "a=" << this->a << " b=" << this->b << endl;
}
friend ostream& operator<<(ostream& os, Complex &c);
//局部重载<<函数写法,通常不写进来!!!
ostream& operator<<(ostream& os) { //相当于operator(cout,c1)
os << "( " << this->a << " " << this->b << "i)";
return os; //注意返回值是os的引用,因为要实现连续<<
}
private:
int a;
int b;
};
ostream& operator<<(ostream& os, Complex &c) {
os << "( " << c.a << " " << c.b << "i)";
return os; //注意返回值是os的引用,因为要实现连续<<
}
int main() {
Complex c1(10, 20);
//下面调用了连续<<
cout << c1 << " " << c1 << endl;//相当于调用operator(cout,c1)
//如果写在类里面的话,调用函数应写:c1<<cout,极易导致歧义,故一般不会将<<左移写到类里当局部函数
system("pause");
return 0;
}
小总结:为什么要在重载函数中返回os的引用可以实现连续左移?
右移前:os << "( " << c.a << " " << c.b << “i)”;
右移一次:os<< c.a << " " << c.b << “i)”;
右移一次:os<< " " << c.b << “i)”;
右移一次:os<< c.b << “i)”;
右移一次:os<<c.b << “i)”;
右移一次:os<< “i)”;
最终实现将全部需要打印的东西移到ostream中,在在main函数中利用cout打印出来
⑧右移>>操作符的重载,实现代码如下:
ps: istream是cin的数据类型
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Complex {
public:
Complex(int a, int b) {
this->a = a;
this->b = b;
}
void print() {
cout << "a=" << this->a << " b=" << this->b << endl;
}
friend istream& operator>>(istream& is, Complex &c);
//局部重载>>函数写法,通常不写进来!!!
istream& operator<<(istream& is) { //相当于operator(cout,c1)
cout << "a:";
is >> this->a;
cout << "b:";
is >> this->b;
}
private:
int a;
int b;
};
istream& operator>>(istream& is, Complex &c) {
cout << "a:";
is>>c.a;
cout << "b:";
is >> c.b;
return is; //注意返回值是is的引用,因为要实现连续<<
}
int main() {
Complex c1(10, 20);
cin >> c1;//相当于调用了operator>>(cin,c1);
//如果写在类里面的话,调用函数应写:cin>>c,极易导致歧义,故一般不会将>>右移写到类里当局部函数
c1.print();
system("pause");
return 0;
}
最终总结:为什么C++中需要操作符重载?因为C++引进了类这个概念,如果对类中私有对象进行操作符运算时,类中如果私有成员较多,就比较繁琐,这时可以通过操作符重载来实现,用一个简单的操作符让可以类与类私有成员的复杂操作得到执行。