文章目录
- 三、几个典型运算符重载
- 1.++与--
- ①成员函数
- ②友元函数
- 2.重载赋值运算符
- ①条件
- ②代码
- 3.重载运算符[]和()
- ①前提
- ②重载下标运算符 []
- ③重载函数调用符 ()
- ④重载流插入和流提取运算符
- a.前提
三、几个典型运算符重载
1.++与–
①成员函数
#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<iosstream>
using namespace std;
class Increase
{public:Increase () {value=0;}void display() const {cout<<value<<'\n'};friend Increase operator ++();//前置friend Increase operator ++(Increase &,int);//后置private: unsigned value;
};
Increase Increase:: operator ++(Increase &)
{I.value++; return I;}
Increase Increase:: operator ++(Increase &I,int)
{Increase temp(a); I.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( ) ;}
2.重载赋值运算符
①条件
- 赋值运算符重载用于对象数据的复制
- operator= 必须重载为成员函数
- 重载函数原型为:类名 & 类名 :: operator= ( 类名 ) ;
②代码
#include<iostream>
#include<cstring>
using namespace std;
class Name
{ public :Name ( char *pN ) ;Name( const Name & ) ; //复制构造函数 Name& operator=( const Name& ) ; // 重载赋值运算符~ Name() ;protected : char *pName ;int size ;
} ;int main()
{ Name Obj1( "ZhangSan" ) ;Name Obj2 = Obj1 ; // 调用复制构造函数 Name Obj3( "NoName" ) ;Obj3 = Obj2 = Obj1 ; // 调用重载赋值运算符函数
}
Name::Name ( char *pN ){ cout <<" Constructing " << pN << endl ;pName = new char[ strlen( pN ) + 1 ] ;if( pName != 0 ) strcpy( pName,pN ) ;size = strlen( pN ) ;}
Name::Name( const Name & Obj ) //复制构造函数
{ cout << " Copying " << Obj.pName << " into its own block\n";pName = new char[strlen( Obj.pName ) + 1 ] ;if ( pName != 0 ) strcpy( pName, Obj.pName ) ;size = Obj.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 ;
}
Name::~ Name()
{ cout << " Destructing " << pName << endl ;delete []pName ;size = 0;
}
3.重载运算符[]和()
①前提
- 运算符 [] 和 () 是二元运算符
- [] 和 () 只能用成员函数重载,不能用友元函数重载
②重载下标运算符 []
[] 运算符用于访问数据对象的元素
重载格式 类型 类 :: operator[] ( 类型 ) ;
#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 ) { return v [ i ] ; }//返回为引用,以便修改private : int * v ; int size ;
};
int main ( )
{ vector a ( 5 ) ;a [ 2 ] = 12 ; cout << a [ 2 ] << endl ;
}
③重载函数调用符 ()
() 运算符用于函数调用
重载格式 类型 类 :: operator() ( 参数表 ) ;
#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 ;
}
④重载流插入和流提取运算符
a.前提
- istream 和 ostream 是 C++ 的预定义流类
- cin 是 istream 的对象,cout 是 ostream 的对象
- 运算符 << 由ostream 重载为插入操作,用于输出基本类型数据
- 运算符 >> 由 istream 重载为提取操作,用于输入基本类型数据
- 用友元函数重载 << 和 >> ,输出和输入用户自定义的数据类型
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 ; }
int & vector :: operator [] ( int i )
{ if( i >=0 && i < len ) return v[ i ] ;cout << "The subscript " << i << " is outside !\n" ; exit( 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 ;
}