操作符重载04--布尔(逻辑)操作符(Boolean Operators)

测试类.h

   1: #pragma once
   2:  
   3: /*布尔(逻辑)操作符重载
   4: 1. 布尔操作符为2元操作符
   5: 2. 一般使用友元friend的形式,友元可以访问任何权限的成员变量,如果使用成员函数的形式,
   6:    则只带一个参数,还有一个为本身,友元带2个参数
   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:  
  15:     //boolean operators
  16:     friend bool operator==(const TRectangle& x, const TRectangle& y);
  17:     friend bool operator!=(const TRectangle& x, const TRectangle& y);
  18:     friend bool operator<(const TRectangle& x, const TRectangle& y);
  19:     friend bool operator<=(const TRectangle& x, const TRectangle& y);
  20:     friend bool operator>(const TRectangle& x, const TRectangle& y);
  21:     friend bool operator>=(const TRectangle& x, const TRectangle& y);
  22: public:
  23:     TRectangle(double L = 0.00, double H = 0.00);
  24:     TRectangle(const TRectangle& R);
  25:     ~TRectangle();
  26:  
  27:     //method
  28:     void setLength(const double L) { Length = L; }
  29:     double getLength() const { return Length; }
  30:     void setHeight(const double H) { Height = H; }
  31:     double getHeight() const { return Height; }
  32:     double Perimeter() const { return 2 * (Length + Height); }
  33:     double Area() const { return Length * Height; }
  34:  
  35:     TRectangle& operator=(const TRectangle& Rect);
  36: private:
  37:     double Length;
  38:     double Height;
  39: };
  40:  

测试类.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: bool operator==(const TRectangle& x, const TRectangle& y)
  71: {
  72:     if( x.Area() == y.Area() )
  73:         return true;
  74:     return false;
  75: }
  76:  
  77: bool operator!=(const TRectangle& x, const TRectangle& y)
  78: {
  79:     if( x.Area() != y.Area() )
  80:         return true;
  81:     return false;
  82: }
  83:  
  84: bool operator<(const TRectangle& x, const TRectangle& y)
  85: {
  86:     if( x.Area() < y.Area() )
  87:         return true;
  88:     return false;
  89: }
  90:  
  91: bool operator<=(const TRectangle& x, const TRectangle& y)
  92: {
  93:     if( x.Area() <= y.Area() )
  94:         return true;
  95:     return false;
  96: }
  97:  
  98: bool operator>(const TRectangle& x, const TRectangle& y)
  99: {
 100:     if( x.Area() > y.Area() )
 101:         return true;
 102:     return false;
 103: }
 104:  
 105: bool operator>=(const TRectangle& x, const TRectangle& y)
 106: {
 107:     if( x.Area() >= y.Area() )
 108:         return true;
 109:     return false;
 110: }

main

   1: // BooleanOperators.cpp : 定义控制台应用程序的入口点。
   2: //
   3:  
   4: #include "stdafx.h"
   5: #include "TRectangle.h"
   6: #include <iostream>
   7: using namespace std;
   8:  
   9: int _tmain(int argc, _TCHAR* argv[])
  10: {
  11:     TRectangle Ra(10, 20), Rb(11,21);
  12:     if(Ra > Rb)
  13:         cout << "Ra 大于 Rb" << endl; 
  14:     else if(Ra == Rb)
  15:         cout << "Ra 等于 Rb" << endl;
  16:     else
  17:         cout << "Ra 小于 Rb" << endl;
  18:     return 0;
  19: }
  20:  

测试结果图

m3hucyhf

转载于:https://www.cnblogs.com/maogeblog/archive/2012/09/14/2685848.html

猜你喜欢

转载自blog.csdn.net/weixin_34245749/article/details/94679563
今日推荐