操作符重载02--运算操作符(Arithmetic Operators)
测试类.h
1: #pragma once
2:
3: /*运算操作符重载
4: 1. 运算操作符为2元操作符
5: 2. 一般使用友元friend的形式,友元可以访问任何权限的成员变量,如果使用成员函数的形式,
6: 则只带一个参数,还有一个为本身,友元带2个参数,运算结果直接返回,使用方便
7: 3. 注意要满足运算符有意义,尤其是除法
8: */
9: class TRectangle
10: {
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: friend TRectangle operator/(const TRectangle& x, const TRectangle& y);
15: public:
16: TRectangle(double L = 0.00, double H = 0.00);
17: TRectangle(const TRectangle& R);
18: ~TRectangle();
19:
20: //method
21: void setLength(const double L) { Length = L; }
22: double getLength() const { return Length; }
23: void setHeight(const double H) { Height = H; }
24: double getHeight() const { return Height; }
25: double Perimeter() const { return 2 * (Length + Height); }
26: double Area() const { return Length * Height; }
27:
28: TRectangle& operator=(const TRectangle& Rect);
29: private:
30: double Length;
31: double Height;
32: };
33:
测试类.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: }
main()
1: // ArithmeticOperators.cpp : 定义控制台应用程序的入口点。
2: //
3:
4: #include "stdafx.h"
5: #include "TRectangle.h"
6: #include <iostream>
7: using namespace std;
8:
9: void ShowResult(TRectangle& R)
10: {
11: cout << "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);
24: ShowResult(Rb);
25:
26: Rc = Ra + Rb;
27: ShowResult(Rc);
28:
29: Rc = Ra - Rb;
30: ShowResult(Rc);
31:
32: Rc = Ra * Rb;
33: ShowResult(Rc);
34:
35: Rc = Ra / Rb;
36: ShowResult(Rc);
37: return 0;
38: }
39:
测试结果图: