关于重载运算符的学习总结

一,基础知识

1,重载运算符的意义

    使用户自定义类型的数据能够像普通数据一样简便的使用运算符。

2,不可以重载的运算符

       .    ::    .*    ?:    sizeof

3,重载运算符的方式

    1》重载为成员函数   

        1-在该类内部声明成员运算符函数。

        2-在类外定义成员运算符函数的具体内容。

    2》重载为友元函数

        ps:常用于运算符左右操作数类型不同的情况。

               不能用友元函数重载的运算符有:= () [ ]  ->  。

4,重载运算符 ++,--;

    1》前置方式:

        1-成员函数:

类名 :: 类名 operator++ () ; 

        2-友元函数:

friend 类名 operator++ (类名 &) ; 

    2》后置方式:

        1-成员函数

类名 :: 类名  operator++ (int) ; 

        2-友元函数

friend 类名 operator++ (类名 &, int) ; 

5,重载赋值运算符 =;

    1》函数原型:

类名  &  类名  :: operator= ( 类名 ) ;

    PS:operator必须重载为成员函数。

6,重载运算符 [ ],( );

      1》[ ] 重载格式:

数据类型 类名 :: operator[]  ( 数据类型 ) ;

      2》( ) 重载格式:

数据类型 类名 :: operator()  ( 参数表  ) ;

        PS:[ ] 和()都是二元运算符。

               [ ] 和()只能用成员函数重载。

7,重载输入输出运算符 << ,>>;  

    1》关于cin和cout :   

        首先,istream 和 ostream 是C++的预定义流类,而cin是istream 的对象,cout是ostream的对象。

        其次,运算符<< 由ostream重载为插入操作,用于输出基本类型数据,运算符 >>istream重载为提取操作,用于输入基本类型数据,用友元函数重载<<>>,输出和输入用户自定义的数据类型 。

         PS:只能被重载成友元函数,不能重载成成员函数。

二,简单应用

1,重载运算符的方式 

    1》重载为成员函数   

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);}

    2》重载为友元函数

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, const Complex & c2 )
  { double r = c1.Real + c2.Real ;  double i = c1.Image+c2.Image ;
     return Complex ( r,  i ) ;
  }
Complex operator - ( const Complex & c1, const 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 ) ; }

2,重载运算符 ++,--;

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

3,重载赋值运算符 =;

class  Name
{ public :
     Name ( char  *pN ) ;
     Name( const Name & ) ;		    //复制构造函数
     Name& operator=( const Name& ) ;     // 重载赋值运算符
     ~ Name() ;
  protected : 
     char  *pName ;
     int 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 ;
}

4,重载运算符 [ ],( );

    1》重载运算符[ ]

class  vector
{
       int * v ;       int size ; 
public :
       vector ( int  n )  {  v = new  int [ n ] ; size = n ; }
       ~ vector ( )  { delete [ ] v ; size = 0 ; }
       int & operator [ ] ( int  i )  {  return  v [ i ] ; }      
};
int main ( )
{  vector  a ( 5 ) ;
    a [ 2 ] = 12 ;	  
    cout << a [ 2 ] << endl ;
}

    2》重载运算符( )

class  F
  { public :  
        double  operator ( )  ( double x ,  double  y ) ;
  } ;
double  F :: operator ( )  ( double  x ,  double  y )
   { return   x * x + y * y ; }

5,重载输入输出运算符 << ,>>;

    ostream& operator<<(ostream& out,class_name& obj)
    {
          out<<obj.item1;
          out<<obj.item2;
          return out;
    };

三,学习感悟

    感觉这章的学习主要靠记忆,最基础的预习做不好,再往下复习就变得晦涩。

    其中的+,-,等运算符比较好理解,但是对于[ ],( ),则太好理解,还有重载输入输出操作符,一开始没记住只能用友元重载,错的很蒙,课堂听讲很重要,课前预习更重要,如果都没做好,就老老实实 当垫底的,闷头补吧。


<think>好的,我现在需要帮助用户了解在ACM编程竞赛中运算符重载的实现与用法。首先,我需要整理用户提供的引用内容,看看有哪些相关的信息可以参考。 用户提到的引用1和2都提到了复数类的运算符重载,特别是重载+、*和<<运算符。引用2还提到了两种重载方式:类成员函数和全局函数,并且需要友元声明。引用3提到了实验题目,可能涉及具体的实现细节。引用4是关于时间日期类的运算符重载,比如加减运算,这可能与ACM中的实际问题相关。 首先,我应该解释运算符重载的基本概念,说明它在ACM中的作用,比如简化代码,提高可读性。然后,结合用户提供的例子,复数类和时间类,分别展示如何实现常见的运算符重载,比如加减、比较、输入输出等。 接下来,需要区分类成员运算符重载和全局函数重载的不同之处,比如参数数量,是否需要友元等。例如,成员函数的运算符重载,比如operator+,只需要一个参数,因为左操作数是this指针;而全局函数需要两个参数。 然后,在ACM中,常见的应用场景可能包括复数运算、大数运算、矩阵运算、几何计算等,这些都需要重载运算符来简化操作。需要举几个例子,比如矩阵加法或向量的叉乘,用运算符重载如何实现。 另外,注意事项也很重要,比如不能重载运算符(如::、.、.*等),以及运算符的优先级和结合性保持不变。同时,友元函数的使用场景,比如当左操作数不是当前类的对象时,必须使用全局函数重载,比如<<运算符。 最后,总结在ACM中使用运算符重载的好处,比如代码简洁、减少错误,并提醒用户注意合理使用,避免过度复杂化。 现在需要确保回答符合用户的要求,结构清晰,分点说明,并包含相关示例代码,比如复数类的+和<<重载,时间类的加减运算。同时,生成相关问题,帮助用户进一步学习。</think>### ACM竞赛中运算符重载的实现与用法 在ACM编程竞赛中,运算符重载(Operator Overloading)是C++中一项重要特性,能够通过自定义类对象的行为,使代码更简洁高效。以下从实现方法和典型应用场景展开说明。 --- #### 一、运算符重载的实现方法 运算符重载分为**类成员函数重载**和**全局函数重载**两种形式,具体选择取决于操作符的语义需求。 1. **类成员函数重载** 运算符作为类的成员函数时,左操作数为当前类的对象(`this`指针),右操作数为参数。 **示例(复数加法)**: ```cpp class Complex { public: Complex(double r = 0, double i = 0) : real(r), imag(i) {} // 成员函数重载 "+" Complex operator+(const Complex& other) { return Complex(real + other.real, imag + other.imag); } private: double real, imag; }; ``` 2. **全局函数重载(需友元声明)** 当左操作数不是当前类对象时(如输入输出流`<<`),需声明为全局函数并配合`friend`关键字。 **示例(复数输出)**: ```cpp class Complex { friend ostream& operator<<(ostream& os, const Complex& c); // 声明友元 }; // 全局重载 "<<" ostream& operator<<(ostream& os, const Complex& c) { os << c.real << "+" << c.imag << "i"; return os; } ``` --- #### 二、ACM中的典型应用场景 1. **复数运算** 直接通过`+`、`*`等运算符实现复数加减乘除,避免手动拆解实虚部。 **乘法重载示例**: ```cpp Complex operator*(const Complex& a, const Complex& b) { return Complex(a.real*b.real - a.imag*b.imag, a.real*b.imag + a.imag*b.real); } ``` 2. **时间/日期计算** 实现时间的加减或比较操作,例如计算时间差[^4]。 **时间加法示例**: ```cpp class Time { public: Time operator+(int seconds) { Time result = *this; result.sec += seconds; // 处理进位逻辑 return result; } }; ``` 3. **几何计算** 向量点乘、叉乘等操作可通过运算符简化: ```cpp Vector3D operator*(const Vector3D& v1, const Vector3D& v2) { return Vector3D(v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x); // 叉乘 } ``` --- #### 三、注意事项 1. **不可重载运算符** `::`、`.`、`.*`、`sizeof`等运算符不可重载。 2. **保持语义一致性** 例如重载`+`不应修改操作数本身,而是返回新对象。 3. **优先选择成员函数** 对称运算符(如`+`、`==`)可灵活选择,但输入输出流必须全局重载。 --- #### 四、优势总结 - **代码简洁性**:直接通过`a + b`代替`a.add(b)`。 - **可读性提升**:运算符语义直观,如`vector1 * vector2`表示叉乘。 - **减少错误**:封装底层操作,避免重复代码。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值