成员函数重载
++
#include<iostream.h>
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; }
void main( )
{ Increase a , b , n ;
for ( int 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( ) ;
}
Increase operator ++ ( ) ;
Increase Increase :: operator ++ ( )
{ value ++ ; return * this ; }
++ n ;
预定义版本
6.3.1 重载++与--
11/13/2022 5.
评论0
最新资源