一,基础知识
1,重载运算符的意义
使用户自定义类型的数据能够像普通数据一样简便的使用运算符。
2,不可以重载的运算符
. :: .* ?: sizeof
3,重载运算符的方式
1》重载为成员函数
1-在该类内部声明成员运算符函数。
2-在类外定义成员运算符函数的具体内容。
2》重载为友元函数
ps:常用于运算符左右操作数类型不同的情况。
不能用友元函数重载的运算符有:= () [ ] -> 。
4,重载运算符 ++,--;
1》前置方式:
1-成员函数:
类名 :: 类名 operator++ () ;
2-友元函数:
friend 类名 operator++ (类名 &) ;
2》后置方式:
1-成员函数
类名 :: 类名 operator++ (int) ;
2-友元函数
friend 类名 operator++ (类名 &, int) ;
5,重载赋值运算符 =;
1》函数原型:
类名 & 类名 :: operator= ( 类名 ) ;
PS:operator必须重载为成员函数。
6,重载运算符 [ ],( );
1》[ ] 重载格式:
数据类型 类名 :: operator[] ( 数据类型 ) ;
2》( ) 重载格式:
数据类型 类名 :: operator() ( 参数表 ) ;
PS:[ ] 和()都是二元运算符。
[ ] 和()只能用成员函数重载。
7,重载输入输出运算符 << ,>>;
1》关于cin和cout :
首先,istream 和 ostream 是C++的预定义流类,而cin是istream 的对象,cout是ostream的对象。
其次,运算符<< 由ostream重载为插入操作,用于输出基本类型数据,运算符 >>由istream重载为提取操作,用于输入基本类型数据,用友元函数重载<<和>>,输出和输入用户自定义的数据类型 。
PS:只能被重载成友元函数,不能重载成成员函数。
二,简单应用
1,重载运算符的方式
1》重载为成员函数
class Complex//复数类
{
public:
Complex( ) {real=0,imag=0;}//默认构造函数
Complex(double r,double i) {real=r; imag=i;}//带参构造函数
Complex operator + (Complex &c2); //重载加号运算符
void display( );
private:
double real;
double imag;
};
Complex Complex:: operator + (Complex &c2) {
return Complex(real+c2.real, imag+c2.imag);}
2》重载为友元函数
class Complex
{
public:
Complex( double r =0, double i =0 ) { Real = r ; Image = i ; }
Complex(int a) { Real = a ; Image = 0 ; }
void print() const ;
friend Complex operator+ ( const Complex & c1, const Complex & c2 ) ;
friend Complex operator- ( const Complex & c1, const Complex & c2 ) ;
friend Complex operator- ( const Complex & c ) ;
private:
double Real, Image ;
};
Complex operator + ( const Complex & c1, const Complex & c2 )
{ double r = c1.Real + c2.Real ; double i = c1.Image+c2.Image ;
return Complex ( r, i ) ;
}
Complex operator - ( const Complex & c1, const Complex & c2 )
{ double r = c1.Real - c2.Real ; double i = c1.Image - c2.Image ;
return Complex ( r, i ) ;
}
Complex operator- ( const Complex & c )
{ return Complex ( -c.Real, - c.Image ) ; }
2,重载运算符 ++,--;
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<<value<<'\n'; } ;
Increase operator ++ ( ) ; // 前置
Increase operator ++ ( int ) ; // 后置
private: unsigned value ;
};
Increase Increase :: operator ++ ( )
{ value ++ ; return *this ; }
Increase Increase :: operator ++ ( int )
{ Increase temp; temp.value = value ++ ; return temp; }
3,重载赋值运算符 =;
class Name
{ public :
Name ( char *pN ) ;
Name( const Name & ) ; //复制构造函数
Name& operator=( const Name& ) ; // 重载赋值运算符
~ Name() ;
protected :
char *pName ;
int size ;
} ;
Name & Name::operator= ( const Name & Obj )
{ delete []pName ;
pName = new char[ strlen( Obj.pName ) + 1 ] ;
if ( pName != 0 ) strcpy( pName , Obj.pName ) ;
size = Obj.size ;
return *this ;
}
4,重载运算符 [ ],( );
1》重载运算符[ ]
class vector
{
int * v ; int size ;
public :
vector ( int n ) { v = new int [ n ] ; size = n ; }
~ vector ( ) { delete [ ] v ; size = 0 ; }
int & operator [ ] ( int i ) { return v [ i ] ; }
};
int main ( )
{ vector a ( 5 ) ;
a [ 2 ] = 12 ;
cout << a [ 2 ] << endl ;
}
2》重载运算符( )
class F
{ public :
double operator ( ) ( double x , double y ) ;
} ;
double F :: operator ( ) ( double x , double y )
{ return x * x + y * y ; }
5,重载输入输出运算符 << ,>>;
ostream& operator<<(ostream& out,class_name& obj)
{
out<<obj.item1;
out<<obj.item2;
return out;
};
三,学习感悟
感觉这章的学习主要靠记忆,最基础的预习做不好,再往下复习就变得晦涩。
其中的+,-,等运算符比较好理解,但是对于[ ],( ),则太好理解,还有重载输入输出操作符,一开始没记住只能用友元重载,错的很蒙,课堂听讲很重要,课前预习更重要,如果都没做好,就老老实实 当垫底的,闷头补吧。