<< Chapter < Page Chapter >> Page >

89: Imaginary -= Z.Imaginary;

90: return *this;

91: }


93: Complex Complex::operator - ()

94: {

95: Complex Tmp;


97: Tmp.Real = -Real;

98: Tmp.Imaginary = -Imaginary;

99: return Tmp;

100: }


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

103: {

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

105: }


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

108: {

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

110: }


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

113: {

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

115: }


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

118: {

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

120: }


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

123: {

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

125: }


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

128: {

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

130: }


132: double Complex::Abs()

133: {

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

135: }


137: int main()

138: {

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


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>


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: };


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: }


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: }


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

36: {

37: return Data[I];

38: }


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

Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?