重载运算符学习心得

匿名 (未验证) 提交于 2019-12-03 00:21:02

1.不改变运算符的优先级

2.不改变运算符的结合性

3.不改变运算符所需要的操作数

4.不能创建新的运算符

5.运算符函数可以重载为成员函数或者友元函数

}

类型

类名

默认版本

一元运算符

Object.operator op()

二元运算符

Objectl op objectr

Objectl.operator op (objectr)

用成员函数重载运算符

Class x

};

此时当前对象作为运算符的左操作数,它时通过this指针隐含的传递给函数的。

{

public:

Complex( ){real=0,imag=0;}

Complex(double r,double i){real=r; imag=i;}

void display( );

private:

double real;

double imag;

};

ComplexComplex:: 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=0;sec=0;}

private:

intminute;

intsec;

};

{

if(++sec>=60){

++minute;

}

}

{

Timetemp(*this);

sec++;

if(sec>=60) {

sec-=60;

++minute;

}

}

classComplex

{intReal ; intImag ;

public :

Complex ( int a ) { Real = a ;Imag = 0 ; }

Complex ( inta,intb ) { Real = a ;Imag = b ; }

Complexoperator + ( Complex ) ;

};

intf ( )

成员函数与友元运算符函数的比较

前置方式

后置方式

#include<iostream>

using namespace std;

classIncrease

{ public :

Increase () { value=0; }

voiddisplay( )const{ cout<<value<<'\n'; } ;

private:unsignedvalue ;

};

IncreaseIncrease :: operator ++ ( )

{ value ++;return *this ; }

IncreaseIncrease :: operator ++ ( int )

{ Increasetemp;temp.value = value ++ ;returntemp; }

int main( )

int main()

{ Name Obj1( "ZhangSan" ) ;

Name Obj3("NoName" ) ;

}

Name::Name ( char*pN )

{ cout<<" Constructing " << pN << endl ;

pName = newchar[ strlen( pN ) + 1 ] ;

if( pName !=0 ) strcpy( pName,pN ) ;

size =strlen( pN ) ;

}

{ 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;

}

{ delete[]pName ;

if ( pName !=0 ) strcpy( pName , Obj.pName ) ;

size =Obj.size ;

return *this;

}

Name::~ Name()

{ cout << " Destructing " <<pName << endl ;

delete[]pName ;

size = 0;

}

重载运算符【】和()

运算符【】和()时二元运算符

【】和()只能用成员函数重载,不能使用友元函数重载

1.

x [ y ]

x . operator [ ]( y )

#include<iostream>

using namespacestd;

classvector

{ public :

vector ( intn ){v = newint [ n ] ; size = n ; }

~ vector ( ){ delete [ ] v ; size = 0 ; }

int & operator [ ] ( inti ){returnv [ i ] ; }

private :

int * v ;int size ;

};

int main ( )

{vectora ( 5 ) ;

a [ 2 ] = 12 ;

cout << a [ 2 ] << endl ;

}

2.

#include <iostream>

using namespace std ;

classF

{ public:

doubleoperator ( )( double x ,doubley ) ;

} ;

doubleF ::operator ( )( doublex ,doubley )

{ returnx * x + y * y ; }

int main ( )

{ Ff;

cout <<f ( 5.2 , 2.5 ) << endl ;

}

{

out<<obj.item1;

out<<obj.item2;

.. .

out<<obj.itemn;

return out;

}

{

in>>obj.item1;

in>>obj.item2;

. . .

in>>obj.itemn;

return in;

}

#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 )

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 << " isoutside !\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 ] ;

returninput ;

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!