<< Chapter < Page Chapter >> Page >

89: Imaginary -= Z.Imaginary;

90: return *this;

91: }

92:

93: Complex Complex::operator - ()

94: {

95: Complex Tmp;

96:

97: Tmp.Real = -Real;

98: Tmp.Imaginary = -Imaginary;

99: return Tmp;

100: }

101

102: int operator == (Complex Z1,Complex Z2)

103: {

104: return (Z1.Real == Z2.Real)&&(Z1.Imaginary == Z2.Imaginary);

105: }

106:

107: int operator != (Complex Z1,Complex Z2)

108: {

109: return (Z1.Real != Z2.Real) || (Z1.Imaginary != Z2.Imaginary);

110: }

111:

112: int operator>(Complex Z1,Complex Z2)

113: {

114: return Z1.Abs()>Z2.Abs();

115: }

116:

117: int operator>= (Complex Z1,Complex Z2)

118: {

119: return Z1.Abs()>= Z2.Abs();

120: }

121:

122: int operator<(Complex Z1,Complex Z2)

123: {

124: return Z1.Abs()<Z2.Abs();

125: }

126:

127: int operator<= (Complex Z1,Complex Z2)

128: {

129: return Z1.Abs()<= Z2.Abs();

130: }

131:

132: double Complex::Abs()

133: {

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

135: }

136:

137: int main()

138: {

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

140:

141: cout<<"X: ";

142: X.Print();

143: cout<<endl<<"Y: ";

144: Y.Print();

145: cout<<endl<<"Z: ";

146: Z.Print();

147: X = -Y + 3.6;

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

149: X.Print();

150: cout<<" = ";

151: (-Y).Print();

152: cout<<" + 3.6 ";

153: X = -Y + -Z;

154: cout<<endl<<"X = -Y + -Z: ";

155: X.Print();

156: cout<<" = ";

157: (-Y).Print();

158: cout<<" + ";

159: (-Z).Print();

160: return 0;

161: }

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

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

Đa năng hóa một số toán tử đặc biệt

Trong phần này chúng ta sẽ tìm hiểu cách cài đặt một vài toán tử đặc biệt như () [] ++ -- , = ->

Toán tử []

Khi cài đặt các lớp vector hoặc chuỗi ký tự, chúng ta cần phải truy cập đến từng phần tử của chúng, trong ngôn ngữ C/C++ đã có toán tử [] để truy cập đến một phần tử của mảng. Đây là toán tử hai ngôi, có dạng a[b]và khi đa năng toán tử này thì hàm toán tử tương ứng phải là thành viên của một lớp.

Ví dụ 4.5:   Đa năng hóa toán tử [] để truy cập đến một phần tử của vector.

1: #include<iostream.h>

2:

3: class Vector

4: {

5: private:

6: int Size;

7: int *Data;

8: public:

9: Vector(int S=2,int V=0);

10: ~Vector();

11: void Print() const;

12: int&operator [] (int I);

13: };

14:

15: Vector::Vector(int S,int V)

16: {

17: Size = S;

18: Data=new int[Size];

19: for(int I=0;I<Size;++I)

20: Data[I]=V;

21: }

22:

23: Vector::~Vector()

24: {

25: delete []Data;

26: }

27: void Vector::Print() const

28: {

29: cout<<"Vector:(";

30: for(int I=0;I<Size-1;++I)

31: cout<<Data[I]<<",";

32: cout<<Data[Size-1]<<")"<<endl;

33: }

34:

35: int&Vector::operator [](int I)

36: {

37: return Data[I];

38: }

39:

40: int main()

41: {

42: Vector V(5,1);

43: V.Print();

44: for(int I=0;I<5;++I)

45: V[I]*=(I+1);

46: V.Print();

47: V[0]=10;

48: V.Print();

49: return 0;

50: }

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

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

Trong chương trình ở ví dụ 4.5, hàm toán tử của toán tử [] ở lớp Vector trả về một tham chiếu vì toán tử này có thể dùng ở vế trái của phép gán.

Toán tử ()

Toán tử () được dùng để gọi hàm, toán tử này gồm hai toán hạng: toán hạng đầu tiên là tên hàm, toán hạng thứ hai là danh sách các tham số của hàm. Toán tử này có dạng giống như toán tử []và khi đa năng toán tử này thì hàm toán tử tương ứng phải là thành viên của một lớp.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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