<< Chapter < Page Chapter >> Page >

{

return sqrt(Real*Real+Imaginary*Imaginary);

}

int main()

{

Complex X,Y(4.3,8.2),Z(3.3,1.1);

cout<<"X: "; X.Print();

cout<<endl<<"Y: "; Y.Print();

cout<<endl<<"Z: "; Z.Print();

X = Y + 3.6;

cout<<endl<<endl<<"X = Y + 3.6: ";

X.Print(); cout<<" = ";

Y.Print(); cout<<" + 3.6 ";

X = 3.6 + Y;cout<<endl<<"X = 3.6 + Y: ";

X.Print(); cout<<" = 3.6 + ";

Y.Print(); X = 3.8 - Z;

cout<<endl<<"X = 3.8 - Z: ";

X.Print(); cout<<" = 3.8 - ";

Z.Print(); X = Z - 3.8;

cout<<endl<<"X = Z - 3.8: ";

X.Print(); cout<<" = ";

Z.Print(); cout<<" - 3.8 ";

return 0;

}

Chúng ta chạy ví dụ 4.3 , kết quả ở hình 4.7

Hình 4.7: Kết quả của ví dụ 4.3

Đa năng hóa các toán tử một ngôi

Các toán tử một ngôi được đa năng hóa trong hình 4.8 sau:

Toán tử Ví dụ Toán tử Ví dụ
+ +c ~ ~c
- -c ! !a
* *c ++ ++c, c++
& &c -- --c, c--
-> c->    

Hình 4.8: Các toán tử một ngôi được đa năng hóa

Một toán tử một ngôi của lớp được đa năng hóa như một hàm thành viên không tĩnh với không có tham số hoặc như một hàm không thành viên với một tham số; Tham số đó phải hoặc là một đối tượng lớp hoặc là một tham chiếu đến đối tượng lớp.

Ví dụ 4.4:   Lấy lại ví dụ 4.3 và thêm toán tử dấu trừ một ngôi.

1: #include<iostream.h>

2: #include<math.h>

3:

4: class Complex

5: {

6: private:

7: double Real,Imaginary;

8: public:

9: Complex(); // Constructor mặc định

10: Complex(double R,double I);

11: Complex (const Complex&Z); // Constructor sao chép

12: Complex (double R); // Constructor chuyển đổi

13: void Print(); // Hiển thị số phức

14: // Các toán tử tính toán

15: friend Complex operator + (Complex Z1,Complex Z2);

16: friend Complex operator - (Complex Z1,Complex Z2);

17: Complex operator += (Complex Z);

18: Complex operator -= (Complex Z);

19: // Toán tử trừ một ngôi

20: Complex operator – ();

21: // Các toán tử so sánh

22: friend int operator == (Complex Z1,Complex Z2);

23: friend int operator != (Complex Z1,Complex Z2);

24: friend int operator>(Complex Z1,Complex Z2);

25: friend int operator>= (Complex Z1,Complex Z2);

26: friend int operator<(Complex Z1,Complex Z2);

27: friend int operator<= (Complex Z1,Complex Z2);

28: private:

29: double Abs(); // Giá trị tuyệt đối của số phức

30: };

31:

32: Complex::Complex()

33: {

34: Real = 0.0;

35: Imaginary = 0.0;

36: }

37:

38: Complex::Complex(double R,double I)

39: {

40: Real = R;

41: Imaginary = I;

42: }

43:

44: Complex::Complex(const Complex&Z)

45: {

46: Real = Z.Real;

47: Imaginary = Z.Imaginary;

48: }

49:

50: Complex::Complex(double R)

51: {

52: Real = R;

53: Imaginary = 0.0;

54: }

55:

56: void Complex::Print()

57: {

58: cout<<'('<<Real<<','<<Imaginary<<')';

59: }

60:

61: Complex operator + (Complex Z1,Complex Z2)

62: {

63: Complex Tmp;

64:

65: Tmp.Real = Z1.Real + Z2.Real;

66: Tmp.Imaginary = Z1.Imaginary + Z2.Imaginary;

67: return Tmp;

68: }

69:

70: Complex operator - (Complex Z1,Complex Z2)

71: {

72: Complex Tmp;

73:

74: Tmp.Real = Z1.Real - Z2.Real;

75: Tmp.Imaginary = Z1.Imaginary - Z2.Imaginary;

76: return Tmp;

77: }

78:

79: Complex Complex::operator += (Complex Z)

80: {

81: Real += Z.Real;

82: Imaginary += Z.Imaginary;

83: return *this;

84: }

85:

86: Complex Complex::operator -= (Complex Z)

87: {

88: Real -= Z.Real;

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Lập trình hướng đối tượng. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10794/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Lập trình hướng đối tượng' conversation and receive update notifications?

Ask