មេរៀនទី ៧: Operator Overloading និង Friend Functions
គោលបំណងមេរៀន
- យល់ដឹងពីគោលគំនិតនៃ Operator Overloading និងរបៀបប្រើប្រាស់វាដើម្បីបន្ថែមមុខងារដល់ arithmetic, relational, និង logical operators។
- ស្វែងយល់ពី Friend Functions និងការប្រើប្រាស់របស់វាក្នុងការចូលប្រើ private members នៃ class។
- ស្វែងយល់ពីសារៈសំខាន់នៃ Code Reusability និងរបៀបអនុវត្តវាតាមរយៈ operator overloading និង friend functions។
Operator Overloading គឺជាបច្ចេកទេសនៅក្នុង C++ ដែលអនុញ្ញាតឱ្យយើងកំណត់អត្ថន័យថ្មីសម្រាប់ operators (ដូចជា +
, -
, ==
, <
ជាដើម) នៅពេលប្រើជាមួយ objects នៃ class ដែលយើងបានបង្កើត។ វាធ្វើឱ្យកូដមានភាពងាយស្រួលអាន និងប្រើប្រាស់។
Operator អាចត្រូវបាន overloaded ជា member function ឬ non-member function។ Syntax ទូទៅមានដូចខាងក្រោម៖
// As a member function
return_type operator symbol (parameters) {
// Implementation
}
// As a non-member function
return_type operator symbol (parameters) {
// Implementation
}
ឧទាហរណ៍នេះបង្ហាញពីការបូក objects នៃ class Complex
ដែលតំណាងឱ្យលេខកុំផ្លិច។
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// Overload + operator as member function
Complex operator + (const Complex& other) {
return Complex(real + other.real, imag + other.imag);
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(3.5, 2.5);
Complex c2(1.5, 4.5);
Complex c3 = c1 + c2; // Calls operator +
cout << "c1 = ";
c1.display();
cout << "c2 = ";
c2.display();
cout << "c1 + c2 = ";
c3.display();
return 0;
}
- Class
Complex
: មាន private membersreal
និងimag
ដើម្បីតំណាងឱ្យផ្នែកពិត និងផ្នែកស្រមើលនៃលេខកុំផ្លិច។ - Operator Overloading:
operator +
ត្រូវបានកំណត់ជា member function ដែលបូកផ្នែកreal
និងimag
នៃ objects ពីរ ហើយបង្វិល objectComplex
ថ្មី។ - Output:
c1 = 3.5 + 2.5i c2 = 1.5 + 4.5i c1 + c2 = 5 + 7i
- អត្ថប្រយោជន៍: ការប្រើ
c1 + c2
ធ្វើឱ្យកូដមើលទៅសាមញ្ញ និងស្រដៀងនឹងការបូកលេខធម្មតា។
ឧទាហរណ៍នេះបង្ហាញពីការប្រៀបធៀប objects នៃ class Complex
ដើម្បីពិនិត្យថាតើពួកវាស្មើគ្នាឬអត់។
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// Overload == operator
bool operator == (const Complex& other) {
return (real == other.real && imag == other.imag);
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(2.0, 3.0);
Complex c2(2.0, 3.0);
Complex c3(1.0, 2.0);
cout << "c1 = ";
c1.display();
cout << "c2 = ";
c2.display();
cout << "c3 = ";
c3.display();
if (c1 == c2) {
cout << "c1 is equal to c2" << endl;
} else {
cout << "c1 is not equal to c2" << endl;
}
if (c1 == c3) {
cout << "c1 is equal to c3" << endl;
} else {
cout << "c1 is not equal to c3" << endl;
}
return 0;
}
- Operator
==
: ត្រូវបាន overload ដើម្បីប្រៀបធៀបreal
និងimag
នៃ objects ពីរ។ ប្រសិនបើស្មើគ្នា វាបង្វិលtrue
។ - Output:
c1 = 2 + 3i c2 = 2 + 3i c3 = 1 + 2i c1 is equal to c2 c1 is not equal to c3
- អត្ថប្រយោជន៍: ការប្រើ
==
ធ្វើឱ្យការប្រៀបធៀប objects មានភាពសាមញ្ញ និងអាចអានបាន។
Friend Function គឺជា function ដែលមិនមែនជា member នៃ class ប៉ុន្តែមានសិទ្ធិចូលប្រើ private និង protected members នៃ class នោះ។ វាត្រូវបានប្រើនៅពេលដែល function មួយត្រូវការធ្វើការជាមួយ objects ពីរ ឬនៅពេល operator overloading តម្រូវឱ្យប្រើ non-member function។
class ClassName {
private:
// Private members
public:
friend return_type function_name(parameters);
};
ឧទាហរណ៍នេះបង្ហាញពីការប្រើ friend function ដើម្បី overload operator *
សម្រាប់ការគុណលេខកុំផ្លិច។
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// Declare friend function for operator *
friend Complex operator * (const Complex& c1, const Complex& c2);
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
// Define friend function
Complex operator * (const Complex& c1, const Complex& c2) {
return Complex(
c1.real * c2.real - c1.imag * c2.imag,
c1.real * c2.imag + c1.imag * c2.real
);
}
int main() {
Complex c1(3, 2);
Complex c2(1, 4);
Complex c3 = c1 * c2;
cout << "c1 = ";
c1.display();
cout << "c2 = ";
c2.display();
cout << "c1 * c2 = ";
c3.display();
return 0;
}
- Friend Function:
operator *
ត្រូវបានកំណត់ជា friend function ដើម្បីអនុញ្ញាតឱ្យវាចូលប្រើreal
និងimag
នៃ objects ទាំងពីរ។ - ការគណនា: ការគុណលេខកុំផ្លិចត្រូវបានអនុវត្តតាមរូបមន្ត
(a + bi)(c + di) = (ac - bd) + (ad + bc)i
។ - Output:
c1 = 3 + 2i c2 = 1 + 4i c1 * c2 = -5 + 14i
- អត្ថប្រយោជន៍: Friend function អនុញ្ញាតឱ្យ operator
*
ត្រូវបានអនុវត្តជា non-member function ដែលសមស្របសម្រាប់ operators ដែលត្រូវការចូលប្រើ objects ពីរ។
Code Reusability គឺជាការប្រើប្រាស់កូដដែលមានស្រាប់ដើម្បីអនុវត្តមុខងារស្រដៀងគ្នានៅកន្លែងផ្សេងៗ ដោយមិនចាំបាច់សរសេរកូដថ្មី។ Operator overloading និង friend functions ជួយលើកកម្ពស់ code reusability ដោយ៖
- Operator Overloading: អនុញ្ញាតឱ្យ operators ដែលមានស្រាប់ (ដូចជា
+
,==
) ត្រូវបានប្រើជាមួយ user-defined types ដែលធ្វើឱ្យកូដអាចប្រើឡើងវិញបានសម្រាប់ប្រភេទទិន្នន័យផ្សេងៗ។ - Friend Functions: ផ្តល់វិធីដើម្បីចែករំលែក logic រវាង classes ផ្សេងៗ ឬ objects ដោយមិនចាំបាច់បង្កើត member functions សម្រាប់រាល់ករណី។
ឧទាហរណ៍នេះបង្ហាញពីការប្រើ operator overloading ដើម្បីធ្វើឱ្យ class Complex
អាចប្រើឡើងវិញបានសម្រាប់ប្រតិបត្តិការច្រើន។
#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
// Overload + operator
Complex operator + (const Complex& other) {
return Complex(real + other.real, imag + other.imag);
}
// Overload - operator
Complex operator - (const Complex& other) {
return Complex(real - other.real, imag - other.imag);
}
// Friend function for * operator
friend Complex operator * (const Complex& c1, const Complex& c2);
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
// Define friend function
Complex operator * (const Complex& c1, const Complex& c2) {
return Complex(
c1.real * c2.real - c1.imag * c2.imag,
c1.real * c2.imag + c1.imag * c2.real
);
}
int main() {
Complex c1(3, 2);
Complex c2(1, 4);
Complex sum = c1 + c2;
Complex diff = c1 - c2;
Complex prod = c1 * c2;
cout << "c1 = ";
c1.display();
cout << "c2 = ";
c2.display();
cout << "Sum (c1 + c2) = ";
sum.display();
cout << "Difference (c1 - c2) = ";
diff.display();
cout << "Product (c1 * c2) = ";
prod.display();
return 0;
}
- Code Reusability: Class
Complex
ត្រូវបានរចនាឡើងដើម្បីគាំទ្រ operators ច្រើន (+
,-
,*
) ដែលអនុញ្ញាតឱ្យ objects នៃ class នេះត្រូវបានប្រើនៅក្នុងប្រតិបត្តិការផ្សេងៗដោយមិនចាំបាច់សរសេរកូដថ្មី។ - Output:
c1 = 3 + 2i c2 = 1 + 4i Sum (c1 + c2) = 4 + 6i Difference (c1 - c2) = 2 - 2i Product (c1 * c2) = -5 + 14i
- អត្ថប្រយោជន៍: ការប្រើ operator overloading និង friend function ធ្វើឱ្យ class
Complex
អាចប្រើឡើងវិញបានសម្រាប់ប្រតិបត្តិការគណនាផ្សេងៗ ដែលកាត់បន្ថយការសរសេរកូដស្ទួន។
- Operator Overloading ជួយធ្វើឱ្យកូដមានភាពសាមញ្ញ និងអាចអានបាន ដោយអនុញ្ញាតឱ្យ operators ធ្វើការជាមួយ user-defined types។
- Friend Functions ផ្តល់ភាពបត់បែនក្នុងការចូលប្រើ private members និងជួយអនុវត្ត operators ដែលត្រូវការចូលប្រើ objects ច្រើន។
- Code Reusability ជួយសន្សំសំចៃពេលវេលា និងធ្វើឱ្យកម្មវិធីមានភាពងាយស្រួលក្នុងការថែទាំ។