计划这个总结很久了,但因为一直没有时间,所以拖到现在,真不知道自己每天都在忙些什么东西。。。
先总结几个常用的:
一、输入输出流重载“<<”“>>”(以输出运算符为例):
固定声明格式:
friend ostream & operator <<(ostream & output,Time & obj);
注意事项:
①:输入输出流的重载只能用友元函数的形式重载;
②:3个“引用&”,目前最好都不要少,ostream后面的“引用&”绝对不能省;
③:在类内声明并定义或者在类内声明类外定义这两种方法均可;
例如:
#include<bits/stdc++.h>
using namespace std;
class Time
{
private:
int year,month;
public:
Time(int y,int m):year(y),month(m){}
Time():year(0),month(0){}
friend ostream & operator <<(ostream & output,Time & obj)//类内声明并定义;
{
output<<obj.year<<"年";
output<<obj.month<<"月";
return output;
}
};
int main()
{
Time t(2018,5);
cout<<t<<endl;
return 0;
}
二、重载“>”/“<”/“>=”/“<=”/“==”等比较运算符:
例:<=重载的一般固定声明格式:
bool operator<=(const Time & obj)const;
注意事项:
①:一般重载为成员函数形式即可,友元形式(未测试);
②:一般形参及整个函数均为常量形式(两个const最好都加吧,目前的话);
③:成员函数形式只有一个“引用&”;
④:若在类外定义,注意表明该函数属于哪个类。
示例代码:
#include<bits/stdc++.h>
using namespace std;
class Time
{
private:
int year,month;
public:
Time(int y,int m):year(y),month(m){}
Time():year(0),month(0){}
bool operator<=(const Time & obj)const//内类声明并定义;
{
if(year<obj.year)return 1;
else if(year==obj.year)
{
if(month<=obj.month)return 1;
}
return 0;
}
friend ostream & operator<<(ostream &output,Time & obj);
};
ostream & operator <<(ostream & output,Time & obj)
{
output<<obj.year;
output<<" ";
output<<obj.month;
return output;
}
int main()
{
Time t(2018,5),tt(2018,6);
if(t<=tt)cout<<tt<<endl;
else cout<<t<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
class Time
{
private:
int year,month;
public:
Time(int y,int m):year(y),month(m){}
Time():year(0),month(0){}
bool operator<=(const Time & obj)const;
friend ostream & operator<<(ostream &output,Time & obj);
};
bool Time::operator<=(const Time & obj)const//类内定义,类外声明;
{
if(year<obj.year)return 1;
else if(year==obj.year)
{
if(month<=obj.month)return 1;
}
return 0;
}
ostream & operator <<(ostream & output,Time & obj)
{
output<<obj.year;
output<<" ";
output<<obj.month;
return output;
}
int main()
{
Time t(2018,5),tt(2018,6);
if(t<=tt)cout<<tt<<endl;
else cout<<t<<endl;
return 0;
}
三、“=”的重载;
Time operator =(const int &x)
{
year=x;
return *this;
}
注意事项:
①:“=”只能重载为成员函数;
②:一般形参前都要加“引用”;
③:最后一定要有一个返回值;
示例代码:
#include<bits/stdc++.h>
using namespace std;
class Time
{
private:
int year,month;
public:
Time(int y,int m):year(y),month(m){}
Time():year(0),month(0){}
friend ostream & operator<<(ostream &output,Time & obj);
Time operator =(int &x)
{
year=x;
return *this;
}
};
ostream & operator <<(ostream & output,Time & obj)//流输入输出重载时ostream后面的&一定不要省去;
{
output<<obj.year;
output<<" ";
output<<obj.month;
return output;
}
int main()
{
Time t(2018,5),tt;
tt=2019;//
cout<<tt<<endl;
return 0;
}
四、“+、-”的重载;
注意事项:
①:最好在重载函数内定义一个临时对象用于储存计算结果;
②:成员函数、友元均可以;
例如:
Complex operator +(const Complex & obj)
{
Complex temp;
temp.real=real+obj.real;
temp.image=image+obj.image;
return temp;
}
②:对于“+”,因为其为双目运算符,且满足交换律,所以最好还是重载为友元函数,否则的话,比如下面这种情况就会报错(c4=4+c),详见注释;
#include<bits/stdc++.h>
using namespace std;
class Complex
{
private:
int real,image;
public:
Complex(int r,int m):real(r),image(m){}
Complex():real(0),image(0){}
Complex(int r):real(r),image(0){}//用于应对下面加一个整数的情况;
Complex operator +(const Complex & obj)
{
Complex temp;
temp.real=real+obj.real;
temp.image=image+obj.image;
return temp;
}
friend ostream & operator <<(ostream & output,Complex & obj)
{
output<<obj.real;
output<<" ";
output<<obj.image;
return output;
}
};
int main()
{
Complex c(1,2),cc(2,3);
//wrong write way:cout<<c+cc<<endl;
Complex ccc,c4;
c4=c+4;//if there is "c4=4+c",system thinks it is wrong,另外这里+的4,可以理解为一个对象,它有一个专门对应的构造函数,见上面;
cout<<c4<<endl;
return 0;
}
“+”重载为友元函数的代码:
注意注释部分的const;
#include<bits/stdc++.h>
using namespace std;
class Complex
{
private:
int real,image;
public:
Complex(int r,int m):real(r),image(m){}
Complex():real(0),image(0){}
Complex(int r):real(r),image(0){}
friend Complex operator +(const Complex & ,const Complex &);//const if not add,will not to operate the const num,such as "4";
friend ostream & operator <<(ostream & output,Complex & obj)
{
output<<obj.real;
output<<" ";
output<<obj.image;
return output;
}
};
Complex operator +(const Complex & obj1,const Complex & obj2)
{
Complex obj;
obj.real=obj1.real+obj2.real;
obj.image=obj1.image+obj2.image;
return obj;
}
int main()
{
Complex c(1,2),cc(2,3);
//wrong write way:cout<<c+cc<<endl;
Complex ccc,c4;
c4=4+c;
cout<<c4<<endl;
return 0;
}
五、“++”“--”的重载(以“++”为例);
“++”为单目运算符:
1、成员函数重载:
#include<bits/stdc++.h>
using namespace std;
class Increase
{
private:
int t;
public:
Increase(int x):t(x){}
Increase():t(0){}
Increase operator ++()//前加运算模板;
{
t++;
return *this;
}
Increase operator ++(int x)//后加运算模板
{
Increase i(*this);
t++;
return i;
}
friend ostream & operator<<(ostream &out,Increase & obj)
{
out<<obj.t;
return out;
}
};
int main()
{
Increase i,ii,iii,iiii;
ii=++i;
cout<<ii<<endl;//i运行结果i=1;
iiii=iii++;
cout<<iiii<<endl;//运行结果iiii=0;
}
2、友元重载版:
#include<bits/stdc++.h>
using namespace std;
class Increase
{
private:
int t;
public:
Increase(int x):t(x){}
Increase():t(0){}
friend Increase operator ++(Increase & obj)//前加运算模板,多一个对象形参;
{
obj.t++;
return obj;
}
friend Increase operator ++(Increase & obj,int )//后加运算模板,可以只有一个int,即不要非得int x;
{
Increase i(obj);
obj.t++;
return i;
}
friend ostream & operator<<(ostream &out,Increase & obj)
{
out<<obj.t;
return out;
}
};
int main()
{
Increase i,ii,iii,iiii;
ii=++i;
cout<<ii<<endl;//i运行结果i=1;
iiii=iii++;
cout<<iiii<<endl;//运行结果iiii=0;
}
六、“()”“[ ]”重载;
注意:这两者只能用成员函数重载,不能用友元来重载;
例如动态数组模拟源码:
#include<iostream>
using namespace std;
class vector
{
public :
vector ( int n )
{
v = new int [ n ] ;
size = n ;
}
~ vector ( )
{
delete [ ] v ;
size = 0 ;
}
int & operator [ ] ( int i )//返回值为int型;
{
return v [ i ] ;
}
private :
int * v ;
int size ;
};
int main ( )
{
vector a ( 5 ) ;
a [ 2 ] = 12 ;
cout << a [ 2 ] << endl ;
}
再如()的重载:
#include <iostream>
using namespace std ;
class F
{
public :
double operator ( ) ( double x , double y ) ;
} ;
double F :: operator ( ) ( double x , double y )
{
return x * x + y * y ;
}
int main ( )
{
F f ;
cout << f ( 5.2 , 2.5 ) << endl ;
}
1、只能用友元重载的:<<、>>;只能用成员函数重载的:[ ]、( )、=;
2、ostream后面一定加“&”;
3、一般来说形参括号内均加const 和 &;
4、注意友元函数形式比成员函数形式多一个形参;
The end;