运算符重载
运算符重载的概念
运算符重载是指允许用户自定义类型的对象使用内置的运算符,或者允许用户自定义的运算符作用于内置类型的对象。运算符重载是通过函数重载来实现的。
不能重载的运算符:
.
和.*
运算符
::
运算符
?:
运算符
sizeof
运算符
typeid
运算符
运算符重载的基本语法
运算符重载的基本语法如下:
返回类型 operator 运算符(参数列表)
{
//函数体
}
加法运算符重载
//运算符重载-1
#include<iostream>
using namespace std;
class example
{
public:
int a;
int b;
example(int x,int y):a(x),b(y)
{
cout<<"初始化赋值操作"<<endl;
}
example operator+ (example x)
{
cout<<"重载操作"<<endl;
example temp(a+x.a,b+x.b);
return temp;
}
};
int main()
{
example e1(1,2),e2(3,4);
e1=e1+e1+e2+e2;
example e3 = e1+e2;
cout<<"e3的值";
cout<<e3.a<<","<<e3.b<<endl;
cout<<"e1的值";
cout<<e1.a<<","<<e1.b<<endl;
return 0;
}
运行结果:
初始化赋值操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
e3的值11,16
e1的值8,12
不但可以成员函数实现 + 号运算符重载,也可以通过全局函数实现 + 号运算符重载。
//运算符重载-2
#include<iostream>
using namespace std;
class example
{
public:
int a;
int b;
example(int x,int y):a(x),b(y)
{
cout<<"初始化赋值操作"<<endl;
}
};
example operator+ (example x,example y)
{
cout<<"重载操作"<<endl;
example temp(x.a+y.a,x.b+y.b);
return temp;
}
int main()
{
example e1(1,2),e2(3,4);
e1=e1+e1+e2+e2;
example e3 = e1+e2;
cout<<"e3的值";
cout<<e3.a<<","<<e3.b<<endl;
cout<<"e1的值";
cout<<e1.a<<","<<e1.b<<endl;
return 0;
}
初始化赋值操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
e3的值11,16
e1的值8,12
左移(<<)运算符重载
//运算符重载-3
#include<iostream>
using namespace std;
class example
{
public:
int a;
int b;
example(int x,int y):a(x),b(y)
{
cout<<"初始化赋值操作"<<endl;
}
//如果需要访问私有成员,需要将重载运算符函数声明为友元函数
friend ostream& operator<< (ostream &out,example x);
};
ostream& operator<< (ostream &out,example x)
{
cout<<"重载<<运算符"<<endl;
out<<x.a<<","<<x.b<<endl;
return out;
}
int main()
{
example e1(1,2),e2(3,4);
cout<<e1<<e2;
return 0;
}
输出结果:
初始化赋值操作
初始化赋值操作
重载<<运算符
1,2
重载<<运算符
3,4
自增运算符重载
//运算符重载-4(前置++)
#include<iostream>
using namespace std;
class example
{
public:
int a;
int b;
example(int x,int y):a(x),b(y)
{
cout<<"初始化赋值操作"<<endl;
}
example operator++ ()
{
cout<<"重载++运算符"<<endl;
a++;
b++;
return *this;
}
};
ostream& operator<<(ostream& out, example ex1)
{
out << ex1.a<<ex1.b;
return out;
}
int main()
{
example e1(1,2),e2(3,4);
++e1;
++e2;
cout<<e1<<","<<e2;
return 0;
}
输出结果:
初始化赋值操作
初始化赋值操作
重载++运算符
重载++运算符
23,45
//运算符重载-5(后置++)
#include<iostream>
using namespace std;
class example
{
public:
int a;
int b;
example(int x,int y):a(x),b(y)
{
cout<<"初始化赋值操作"<<endl;
}
example operator++ (int)
{
cout<<"重载++运算符"<<endl;
example temp(a,b);
a++;
b++;
return temp;
}
};
ostream& operator<<(ostream& out, example ex1)
{
out << ex1.a<<ex1.b;
return out;
}
int main()
{
example e1(1,2),e2(3,4);
e1++;
e2++;
cout<<e1<<e2<<endl;
cout<<e1++<<endl;
cout<<e1;
return 0;
}
输出结果:
初始化赋值操作
初始化赋值操作
重载++运算符
初始化赋值操作
重载++运算符
初始化赋值操作
2345
重载++运算符
初始化赋值操作
23
34
所以说:前置递增返回引用,后置递增返回值。
赋值运算符重载
//运算符重载-6
#include<iostream>
using namespace std;
class example
{
public:
int a;
int b;
example(int x,int y):a(x),b(y)
{
cout<<"初始化赋值操作"<<endl;
}
example operator= (example x)
{
cout<<"重载赋值运算符"<<endl;
a=x.a;
b=x.b;
return *this;
}
};
ostream& operator<<(ostream& out, example ex1)
{
out << ex1.a<<ex1.b;
return out;
}
int main()
{
example e1(1,2),e2(3,4);
e1=e2;
cout<<e1<<e2<<endl;
return 0;
}
输出结果:
初始化赋值操作
初始化赋值操作
重载赋值运算符
3434
关系运算符重载
//运算符重载-7
#include<iostream>
using namespace std;
class example
{
public:
int a;
int b;
example(int x,int y):a(x),b(y)
{
cout<<"初始化赋值操作"<<endl;
}
bool operator== (example x)
{
cout<<"重载==运算符"<<endl;
if(a==x.a&&b==x.b)
return true;
else
return false;
}
bool operator!= (example x)
{
cout<<"重载!=运算符"<<endl;
if(a!=x.a||b!=x.b)
return true;
else
return false;
}
bool operator> (example x)
{
cout<<"重载>运算符"<<endl;
if(a>x.a&&b>x.b)
return true;
else
return false;
}
bool operator< (example x)
{
cout<<"重载<运算符"<<endl;
if(a<x.a&&b<x.b)
return true;
else
return false;
}
};
ostream& operator<<(ostream& out, example ex1)
{
out << ex1.a<<ex1.b;
return out;
}
int main()
{
example e1(1,2),e2(3,4);
if(e1==e2)
cout<<"e1==e2"<<endl;
else
cout<<"e1!=e2"<<endl;
if(e1!=e2)
cout<<"e1!=e2"<<endl;
else
cout<<"e1==e2"<<endl;
if(e1>e2)
cout<<"e1>e2"<<endl;
else
cout<<"e1<=e2"<<endl;
if(e1<e2)
cout<<"e1<e2"<<endl;
else
cout<<"e1>=e2"<<endl;
return 0;
}
输出结果:
初始化赋值操作
初始化赋值操作
重载==运算符
e1!=e2
重载!=运算符
e1!=e2
重载>运算符
e1<=e2
重载<运算符
e1<e2
函数调用运算符重载
//运算符重载-8
#include<iostream>
using namespace std;
class example
{
public:
int a;
int b;
example(int x,int y):a(x),b(y)
{
cout<<"初始化赋值操作"<<endl;
}
example operator() (example x)
{
cout<<"重载()运算符"<<endl;
a=x.a;
b=x.b;
return *this;
}
};
ostream& operator<<(ostream& out, example ex1)
{
out << ex1.a<<ex1.b;
return out;
}
int main()
{
example e1(1,2),e2(3,4);
e1(e2);
cout<<e1<<","<<e2<<endl;
return 0;
}
输出结果:
初始化赋值操作
初始化赋值操作
重载()运算符
34,34
其他运算符略
重载运算符的注意事项
- 重载运算符不能改变运算符的优先级和结合性
- 重载运算符不能创建新的运算符
- 重载运算符不能改变运算符的操作数个数
- 重载运算符不能改变运算符的操作数类型
- 重载运算符不能改变运算符的操作数的含义
重载运算符的应用
- 重载运算符可以使得类的对象像内置类型一样使用,如:cin、cout、+、-、、/、==、!=、>、<、++、--、=、()、[]、&、|、~、!、<<、>>、+=、-=、=、/=、<<=、>>=、&=、^=、|=、,、->、->*、new、delete、new[]、delete[]等;