操作符重载03--和/差/积/商赋值运算符(Sum Assignment Operator)

+=、-=、*=、/=操作符重载。

测试类.h

   1: #pragma once
   2:  
   3: /*和/差/积/商赋值操作符重载
   4: 1. 为2元操作符
   5: 2. 一般返回类的引用,其实作为成员函数不返回值也可以,就是this本身
   6: 3. 注意要满足运算符有意义,尤其是除法
   7: */
   8: class TRectangle
   9: {
  10:     friend TRectangle operator+(const TRectangle& x, const TRectangle& y);
  11:     friend TRectangle operator-(const TRectangle& x, const TRectangle& y);
  12:     friend TRectangle operator*(const TRectangle& x, const TRectangle& y);
  13:     friend TRectangle operator/(const TRectangle& x, const TRectangle& y);
  14: public:
  15:     TRectangle(double L = 0.00, double H = 0.00);
  16:     TRectangle(const TRectangle& R);
  17:     ~TRectangle();
  18:  
  19:     //method
  20:     void setLength(const double L) { Length = L; }
  21:     double getLength() const { return Length; }
  22:     void setHeight(const double H) { Height = H; }
  23:     double getHeight() const { return Height; }
  24:     double Perimeter() const { return 2 * (Length + Height); }
  25:     double Area() const { return Length * Height; }
  26:  
  27:     TRectangle& operator=(const TRectangle& Rect);
  28:  
  29:     TRectangle& operator+=(const TRectangle& Rect);
  30:     TRectangle& operator-=(const TRectangle& Rect);
  31:     TRectangle& operator*=(const TRectangle& Rect);
  32:     TRectangle& operator/=(const TRectangle& Rect);
  33: private:
  34:     double Length;
  35:     double Height;
  36: };
  37:  

测试类.cpp

   1: #include "StdAfx.h"
   2: #include "TRectangle.h"
   3:  
   4:  
   5: TRectangle::TRectangle(double L, double H) : 
   6: Length(L),
   7: Height(H)
   8: {
   9: }
  10:  
  11: TRectangle::TRectangle(const TRectangle& R) :
  12: Length(R.Length),
  13: Height(R.Height)
  14: {
  15: }
  16:  
  17: TRectangle::~TRectangle(void)
  18: {
  19: }
  20:  
  21: TRectangle& TRectangle::operator=(const TRectangle& Rect)
  22: {
  23:     Length = Rect.Length;
  24:     Height = Rect.Height;
  25:     return *this;
  26: }
  27:  
  28: TRectangle operator+(const TRectangle& x, const TRectangle& y)
  29: {
  30:     TRectangle R;
  31:     R.Length = x.Length + y.Length;
  32:     R.Height = x.Height + y.Height;
  33:     return R;
  34: }
  35:  
  36: TRectangle operator-(const TRectangle& x, const TRectangle& y)
  37: {
  38:     TRectangle R;
  39:     R.Length = x.Length - y.Length;
  40:     R.Height = x.Height - y.Height;
  41:  
  42:     return R;
  43: }
  44:  
  45: TRectangle operator*(const TRectangle& x, const TRectangle& y)
  46: {
  47:     TRectangle R;
  48:     R.Length = x.Length * y.Length;
  49:     R.Height = x.Height * y.Height;
  50:  
  51:     return R;
  52: }
  53:  
  54: TRectangle operator/(const TRectangle& x, const TRectangle& y)
  55: {
  56:     TRectangle R;
  57:     if( y.Length == 0 ) // 避免分母为0
  58:         R.Length = 0;
  59:     else
  60:         R.Length = x.Length / y.Length;
  61:  
  62:     if( y.Height == 0 )
  63:         R.Height = 0;
  64:     else
  65:         R.Height = x.Height / y.Height;
  66:  
  67:     return R;
  68: }
  69:  
  70: TRectangle& TRectangle::operator+=(const TRectangle& R)
  71: {
  72:     Length += R.Length;
  73:     Height += R.Height;
  74:  
  75:     return *this;
  76: }
  77:  
  78: TRectangle& TRectangle::operator-=(const TRectangle& R)
  79: {
  80:     Length -= R.Length;
  81:     Height -= R.Height;
  82:  
  83:     return *this;
  84: }
  85:  
  86: TRectangle& TRectangle::operator*=(const TRectangle& R)
  87: {
  88:     Length *= R.Length;
  89:     Height *= R.Height;
  90:  
  91:     return *this;
  92: }
  93:  
  94: TRectangle& TRectangle::operator/=(const TRectangle& R)
  95: {
  96:     if( R.Length == 0 )
  97:         Length = 0;
  98:     else
  99:         Length /= R.Length;
 100:  
 101:     if( R.Height == 0 )
 102:         Height = 0;
 103:     else
 104:         Height /= R.Height;
 105:  
 106:     return *this;
 107: }

main

   1: // SumAssignmentOperator.cpp : 定义控制台应用程序的入口点。
   2: //
   3:  
   4: #include "stdafx.h"
   5: #include <iostream>
   6: #include "TRectangle.h"
   7: using namespace std;
   8:  
   9: void ShowResult(TRectangle& R, char* str = "")
  10: {
  11:     cout << str << "  Length : " << R.getLength() << " Height : " << R.getHeight() << endl;
  12: }
  13:  
  14: int _tmain(int argc, _TCHAR* argv[])
  15: {
  16:     TRectangle Ra, Rb, Rc;
  17:     Ra.setLength(20);
  18:     Ra.setHeight(10);
  19:  
  20:     Rb.setLength(40);
  21:     Rb.setHeight(20);
  22:  
  23:     ShowResult(Ra, "Ra");
  24:     ShowResult(Rb, "Rb");
  25:  
  26:     Rc = Ra;
  27:     ShowResult(Rc, "Rc Initialize");
  28:  
  29:     Rc += Rb;
  30:     ShowResult(Rc, "Rc += Rb");
  31:  
  32:     Rc -= Rb;
  33:     ShowResult(Rc, "Rc -= Rb");
  34:  
  35:     Rc *= Rb;
  36:     ShowResult(Rc, "Rc *= Rb");
  37:  
  38:     Rc /= Rb;
  39:     ShowResult(Rc, "Rc /= Rb");
  40:  
  41:     return 0;
  42: }
  43:  

测试结果图

image

转载于:https://www.cnblogs.com/maogeblog/archive/2012/09/13/2683233.html

猜你喜欢

转载自blog.csdn.net/weixin_34233679/article/details/94679561