# 面面俱到----重载 (转)

----重载

int intadd( int v1 , int v2 )

{

return  v1+v2;

}

float floatadd( float v1 , float v2 )

{

return  v1+v2;

}

int ia = 1 , ib = 2 , iab = 0;

float fa = 1.0 , fb = 2.0 , fab = 0.0;

iab = intadd( ia ,ib );

fab = floatadd( fa , fb );

int add( int v1 , int v2 )

{

return  v1+v2;

}

float add( float v1 , float v2 )

{

return  v1+v2;

}

int ia = 1 , ib = 2 , iab = 0;

float fa = 1.0 , fb = 2.0 , fab = 0.0;

iab = add( ia ,ib );

fab = add( fa , fb );

int operator + ( int v1 , int v2 )

{

return  v1+v2;

}

float operator + ( float v1 , float v2 )

{

return  v1+v2;

}

int ia = 1 , ib = 2 , iab = 0;

float fa = 1.0 , fb = 2.0 , fab = 0.0;

iab = ia + ib ;

fab = fa + fb;

#include

unsing namespace std;

class complex{

private:

int real;

int image;

public:

complex( int _real = 0 , int _image = 0 ) : real(_real),image(_image) {}

int& Real(){ return real; }

int& Image(){ return image; }

complex operator + ( complex const& v )

{

complex w( real + v.real , image + v.image );

return w;

}

};

ostream& operator << ( ostream& out , complex& v )

{

if( v.Real() )

out << v.Real() ;

if( v.Image() > 0 )

cout << " + " ;

else if( v.Image() == 0 )

return cout;

else

cout << " - " ;

cout << v.Image() <<"i";

return out;

}

void main()

{

complex w( 1 , 2 ) , v( 3 , 4);

cout << (w + v) << endl;

}

2001/8/22

• 博文量
6241
• 访问量
2410788