基本知识点:
|不可重载的算符:
.::.*?:sizeof
|可重载的运算符:+、 -、 *、 /、 %、 ^、 &、 |、 ~、 !、 =、 <、 >、 +=、 -=、 *=、 /=、 <<、 >>、 ==、 !=、 <=、 >=、 &&、 ||、 ++、 --、等
|重载运算符的限制:
不改变运算符的优先级;
不改变运算符的结合性;
不改变运算符所需要的操作数;
不能改变新的运算符;
可对运算符作出新的解释,原有的基本语义不变;
|运算符函数可重载为成员函数和友元函数;
一元运算符:
重载成员函数:
Object.operator op()
重载友元函数:
Operator op(Object)
二元运算符:
ObjectL op ObjectR
重载成员函数:
ObjectL.operator op(ObjectR)
重载友元函数:
operator op(ObjectL,ObjectR)
运算符重载为成员函数基本模板:
class X
{
返回值类型 operator运算符(形参表);
};
返回类型X::operator运算符(形参表)
{
函数体
}
双目运算符重载为成员函数:
形参表中仅有一个参数,作为运算符的右操作数,
当前对象作为运算符的左操作数;
重载复数加号样例:
#include <iostream>
using namespace std;
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);}
void Complex::display( )
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main( )
{
Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1=";c1.display( );
cout<<"c2=";c2.display( );
cout<<"c1+c2 =";
c3.display( );
return 0;
}
单目运算符重载为成员函数:
|成员运算符函数的参数表中无参数,当前对象作为运算符的一个操作数;
样例:
class Time
{
public:
Time(){minute=sec=0;}
Time(int m,int s):minute(m),sec(s){}
Time operator++();
Time operator++(int);
private:
int minute,sec;
};
Time Time::operator()
{if(++sec>=60)
{
sec-=60;
++minute;
}
return *this;
}
Time Time::operator++(int)
{
Time temp(*this);
sec++;
if(sec>=60)
{
sec-=60;
++minute;
}
return temp;
}
用友元函数重载:
|在第一个参数需要隐式转换的情形下,使用友元函数重载运算符是正确的选择;
|友元函数没有this指针,所需操作数都必须在参数表显示;
|C++中不能用友元函数重载的运算符有=、()、[]、->;
样例:
#include <iostream>
using namespace std;
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,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);
}
void Complex::print()const
{cout<<real<<","<<image<<endl;}
成员运算符与友元运算符的比较:
|成员运算符比友元运算符少带一个参数(后置的++、--需要增加一个形参);
|当操作数类型不同时必须使用友元函数;
成员函数重载样例:
#include<iostream>
using namespace std;
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; }
int main( )
{ Increase a , b , n ;
int i ;
for ( i = 0 ; i < 10 ; i ++ )
a = n ++ ;
cout <<"n= " ;
n.display( ) ;
cout <<"a= " ;
a.display( ) ;
for ( i = 0 ; i < 10 ; i ++ )
b = ++ n ;
cout << "n= " ;
n.display( ) ;
cout << "b= " ;
b.display( ) ;
}
友元函数重载样例:
#include<iostream>
using namespace std;
class Increase
{ public :
Increase ( )
{ value=0; }
void display( )
const { cout<<value<<'\n'; } ;
friend Increase operator ++ ( Increase & ) ; // 前置
friend Increase operator ++ ( Increase &, int ) ; // 后置
private: unsigned value ;
};
Increase operator ++ ( Increase & a )
{ a.value ++ ;
return a ; }
Increase operator ++( Increase & a, int )
{ Increase temp(a);
a.value ++ ;
return temp; }
int main( )
{ Increase a ,b , n ;
int i ;
for ( i = 0 ; i < 10 ; i ++ )
a = n ++ ;
cout <<"n= " ;
n.display( ) ;
cout <<"a= " ;
a.display( ) ;
for ( i = 0 ; i < 10 ; i ++ )
b = ++ n ;
cout << "n= " ;
n.display( ) ;
cout << "b= " ;
b.display( ) ;
}
重载运算符输入与输出流运算符:
|istream 和 ostream 是 C++ 的预定义流类;
|cin 是 istream 的对象,cout 是 ostream 的对象;
|运算符 << 由ostream 重载为插入操作,用于输出基本类型数据;
|运算符 >> 由 istream 重载为提取操作,用于输入基本类型数据;
|用友元函数重载 << 和 >> ,输出和输入用户自定义的数据类型;
|只可重载为友元函数不可重载为成员函数;
重载输入流模板:
istream& operator>>(istream& in,class_name& obj)
{
in>>obj.item1;
in>>obj.item2;
.. .
in>>obj.itemn;
return in;
}
重载输出流模板:
ostream& operator<<(ostream& out,class_name& obj)
{
out<<obj.item1;
out<<obj.item2;
.. .
out<<obj.itemn;
return out;
}
重载[]以及重载输入输出运算符样例:
#include<iostream>
#include<cstdlib>
using namespace std;
class vector
{ public :
vector( int size =1 ) ;
~vector() ;
int&operator[](int i);
friend ostream & operator << ( ostream & output , vector & ) ;
friend istream & operator >> ( istream & input, vector & ) ;
private :
int * v ;
int len ;
};
int main(){
int k ;
cout << "Input the length of vector A :\n" ; cin >> k ;
vector A( k ) ;
cout << "Input the elements of vector A :\n" ;
cin >> A ;
cout << "Output the elements of vector A :\n" ;
cout << A ;
}
vector::vector( int size )
{ if (size <= 0 || size > 100 )
{ cout << "The size of " << size << " is null !\n" ;
exit( 0 ) ; }
v = new int[ size ] ;
len = size ;
}
vector :: ~vector()
{ delete[] v ; len = 0 ; }
ostream & operator << ( ostream & output, vector & ary )
{ for(int i = 0 ; i < ary.len ; i ++ ) output << ary[ i ] << " " ;
output << endl ;
return output ;
}
istream & operator >> ( istream & input, vector & ary )
{ for( int i = 0 ;
i < ary.len ; i ++ )
input >> ary[ i ] ;
return input ;
}
int & vector :: operator [] ( int i )
{ if( i >=0 && i < len )
return v[ i ] ;
cout << "The subscript " << i << " is outside !\n" ;
exit( 0 ) ;
}
学习心得:
重载各类运算符的目的是为了根据人的需求更加方便的处理新型数据以及成组的数据,使得主函数中语句更加简便,不需要重复书写冗杂的语句,赋予各种运算符新的功能及含义,通过函数扩展了运算符语义。
开拓了学习编程的思路与眼界,使编程更有趣味性,简洁性。