重定义或重载大部分 C++ 内置的运算符就能使用自定义类型的运算符。
重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。
不能为了重载而重载 重载之后的运算符和原来的使用方式特性一模一样
双目算术运算符 + (加),-(减),*(乘),/(除),% (取模)
关系运算符 ==(等于),!= (不等于),< (小于),> (大于),<=(小于等于),>=(大于等于)
逻辑运算符 ||(逻辑或),&&(逻辑与),!(逻辑非)
单目运算符 + (正),-(负),*(指针),&(取地址)
自增自减运算符 ++(自增),--(自减)
位运算符 | (按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移)
赋值运算符 =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
空间申请与释放 new, delete, new[ ] , delete[]
其他运算符 ()(函数调用),->(成员访问),,(逗号),[](下标)
. :成员访问运算符
-> :成员指针访问运算符
:: :域运算符
sizeof :长度运算符
?: :条件运算符
# : 预处理符号
+ - 两个运算符的重载
#include
using namespace std;
class dog
{
private:
int weight ;
string name;
public:
//构造函数
dog(string name , int weight)
{
this->name = name ;
this->weight = weight ;
}
//拷贝构造
dog(const dog & obj)
{
this->name = obj.name;
this->weight = obj.weight;
}
//移动
dog(const dog && obj)
{
this->name = obj.name;
this->weight = obj.weight;
}
//析够函数
~dog()
{
}
//+两只狗体重之和
dog & operator +(const dog & obj )
{
static dog temp(" ",1);
temp.weight = this->weight + obj.weight;
temp.name = this->name + "和" + obj.name;
return temp;
}
//-两只狗体重之差
dog & operator -(const dog & obj )
{
static dog temp(" ",1);
temp.weight = this->weight - obj.weight;
temp.name = this->name + "和" + obj.name+"体重相差";
return temp;
}
friend ostream & operator <<(ostream & os , dog & obj );
};
//输出<<重载
ostream & operator <<(ostream & os , dog & obj )
{
os<<"名字:"<<obj.name<<endl<<"体重:"<<obj.weight<<endl;
return os;
}
int main()
{
dog g1("土狗",25);
dog g2("小狗",26);
cout<<g1<<g2;
cout<<g1+g2;
cout<<g2-g1;
return 0;
}
其他运算符实例代码
#include
#include
using namespace std;
class Arr
{
private:
char * arr;
int len ;
int size;
public:
//构造
Arr(char * str)
{
len = strlen(str);
size = len+1;
arr = new char[size];
strcpy(arr,str);
}
//拷贝构造
Arr(Arr & obj)
{
arr = new char[obj.size];
len = obj.len;
size = obj.size;
strcpy(arr,obj.arr);
}
//移动构造
Arr(Arr && obj )
{
len = obj.len;
size = obj.size;
arr = obj.arr;
obj.arr = nullptr;
}
//+=重载:两个字符串连接
Arr & operator +=(const Arr &obj)
{
if(obj.len + this->len > this->size )
{
char * p = new char[obj.len+len+1];
strcpy(p,arr);
delete []arr;
arr = p ;
p = nullptr;
size = obj.len+len+1;
}
strcat(arr,obj.arr);
len = len + obj.len ;
return *this;
}
//= 实现字符串复制功能
Arr & operator =(const Arr &obj)
{
if(obj.len > size )
{
char *p = new char[obj.size];
strcpy(p,obj.arr);
arr = p ;
p = nullptr;
size = obj.size;
}
else
{
strcpy(this->arr,obj.arr);
}
len = obj.len;
return *this;
}
//[]
char & operator [](const int i )
{
return arr[i];
}
//()
void operator ()()
{
cout<<"hello new ()"<<endl;
}
friend ostream & operator <<(ostream & os , const Arr & obj);
friend istream & operator >>(istream & is , const Arr & obj );
};
ostream & operator <<(ostream & os , const Arr & obj)
{
os<<"data:"<<obj.arr<<endl<<"len ="<<obj.len<<endl<<endl<<"size="<<obj.size<<endl;
return os;
}
istream & operator >>(istream & is , const Arr & obj )
{
is>>obj.arr;
return is;
}
int main()
{
char buf[] = {"n牛"};
char buf1[] = {"m马"};
Arr a = buf;
Arr b = buf1;
cout<<a<<endl;
a +=b;
cout<<a<<endl;
a=b;
cout<<a<<endl;
a[0]= 't';
cout<<a<<endl;
a();
cin>>a;
cout<<a<<endl;
}