<< Chapter < Page Chapter >> Page >

Ở dòng 12 của chương trình ở ví dụ 4.2:

Complex(double R);

là một constructor chuyển đổi (conversion constructor). Constructor này lấy một tham số double và khởi tạo đối tượng Complex mà phần thực bằng giá trị tham số truyền vào và phần ảo bằng 0.0 (từ dòng 48 đến 52). Bất kỳ một constructor nào có tham số đơn có thể được nghĩ như một constructor chuyển đổi. Constructor chuyển đổi sẽ đổi một số thực thành một đối tượng Complex rồi gán cho đối tượng đích Complex. Chẳng hạn:

T = 3.5; // Ngầm định: T = Complex(3.5)

Trình biên dịch tự động dùng constructor chuyển đổi để tạo một đối tượng tạm thời Complex, rồi dùng toán tử gán để gán đối tượng tạm thời này cho đối tượng khác của Complex. Chẳng hạn câu lệnh sau vẫn đúng:

X = Y + 3.5; // Ngầm định: X = Y + Complex(3.5);

Như vậy một constructor chuyển đổi được sử dụng để thực hiện một sự chuyển đổi ngầm định.

Ví dụ 4.3: Lấy lại ví dụ 4.2 nhưng các hàm toán tử +, - và các hàm toán tử so sánh là hàm không thành viên.

#include<iostream.h>

#include<math.h>

class Complex

{

private:

double Real,Imaginary;

public:

Complex();//Constructor mac dinh

Complex(double R,double I);

Complex (const Complex&Z);//Constructor sao chep

Complex (double R);//Constructor chuyen doi

void Print();//Hien thi so phuc

//Cac toan tu tinh toan

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

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

Complex operator += (Complex Z);

Complex operator -= (Complex Z);

//Cac toan tu so sanh

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

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

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

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

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

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

private:

double Abs();//Gia tri tuyet doi cua so phuc

};

Complex::Complex()

{

Real = 0.0;

Imaginary = 0.0;

}

Complex::Complex(double R,double I)

{

Real = R;

Imaginary = I;

}

Complex::Complex(const Complex&Z)

{

Real = Z.Real;

Imaginary = Z.Imaginary;

}

Complex::Complex(double R)

{

Real = R;

Imaginary = 0.0;

}

void Complex::Print()

{

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

}

Complex operator + (Complex Z1,Complex Z2)

{

Complex Tmp;

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

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

return Tmp;

}

Complex operator - (Complex Z1,Complex Z2)

{

Complex Tmp;

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

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

return Tmp;

}

Complex Complex::operator += (Complex Z)

{

Real += Z.Real;

Imaginary += Z.Imaginary;

return *this;

}

Complex Complex::operator -= (Complex Z)

{

Real -= Z.Real;

Imaginary -= Z.Imaginary;

return *this;

}

int operator == (Complex Z1,Complex Z2)

{

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

}

int operator != (Complex Z1,Complex Z2)

{

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

}

int operator>(Complex Z1,Complex Z2)

{

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

}

int operator>= (Complex Z1,Complex Z2)

{

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

}

int operator<(Complex Z1,Complex Z2)

{

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

}

int operator<= (Complex Z1,Complex Z2)

{

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

}

double Complex::Abs()

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