Làm cách nào để cộng hai số phức bằng cách sử dụng nạp chồng toán tử trong C++?

Bước 24  Số phức. Quá tải đối tượng và toán tử
Các mục tiêu của hệ thống số phức.
  • Đơn vị ảo i = sqrt(-1) trong đó i2 = -1
  • Dạng chuẩn của số phức. a + bi trong đó a và b là các số thực, a gọi là phần thực và bi gọi là phần ảo. Nếu b = 0 thì kết quả là một số thực và nếu a = 0 thì nó được gọi là một số thuần ảo
  • Hai số phức a + bi và c + di bằng nhau nếu a = c và b = d
  • phép cộng. (a + bi) + (c + di) = (a + c) + (b + d)i
  • phép trừ. (a + bi) - (c + di) = (a - c) + (b - d)i
  • phép nhân. (a + bi) x (c + di) = (ac - bd) + (bc + ad)i
  • liên hợp phức tạp. a + bi và a - bi. Lưu ý rằng (a + bi)(a - bi) = a2 + b2 là một số thực
  • phân công. (a + bi) / (c + di) = [(a + bi) x (c - di)] / [(c + di) x (c - di)] =. .
  • Các quy tắc cho số học phức hợp mở rộng số học số thực. Số thực a có thể viết a + 0i nên số thực là tập con của số phức và phép toán số phức dùng với số thực, cho kết quả số thực thông thường
Tuyệt, bây giờ chúng ta đã xem lại tất cả những thứ thú vị đó, hãy tạo một số phức. Chúng ta không thể chỉ làm điều này.
complex num1;
vì vậy chúng ta cần thực hiện một số công việc nền, vì vậy hãy tạo một lớp phức tạp đơn giản.
#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}

Nếu bạn muốn xem liệu bạn có hiểu điều này hay không, hãy làm cho chương trình hoạt động và sau đó mở rộng nó để bao gồm phép trừ và phép nhân. Trước khi thử phép chia, bạn có thể tạo một chương trình mới gọi là phân số. cpp được mô hình hóa sau cái này và thực hiện tất cả các thay đổi cần thiết để triển khai một lớp phân số (lần này với tử số và mẫu số thay vì phần thực và phần ảo). Các phép tính số học với phân số liên quan nhiều hơn một chút vì bạn có thể phải rút gọn phân số kết quả. Một cách tiếp cận tốt có thể là tạo một hàm gcd (ước số chung lớn nhất) và sử dụng nó để rút gọn các phân số của bạn nếu cần (i. e. gcd không phải là 1). Tất nhiên, bạn muốn đảm bảo rằng mẫu số không bao giờ được đặt thành 0

Chuyển sang Bước Hai mươi lăm

Quay trở lại bảng nội dung

Ở đây, các giá trị của số thực và số ảo được truyền trong khi gọi hàm tạo được tham số hóa và với sự trợ giúp của hàm tạo mặc định (trống), hàm addComp được gọi để lấy phép cộng các số phức

Trong C++, chúng ta có thể làm cho các toán tử làm việc cho các lớp do người dùng định nghĩa. Điều này có nghĩa là C++ có khả năng cung cấp cho các toán tử một ý nghĩa đặc biệt cho một kiểu dữ liệu, khả năng này được gọi là nạp chồng toán tử. Ví dụ: chúng ta có thể quá tải một toán tử '+' trong một lớp như Chuỗi để chúng ta có thể nối hai chuỗi chỉ bằng cách sử dụng +. Các lớp ví dụ khác mà toán tử số học có thể bị quá tải là Số phức, Số phân số, Số nguyên lớn, v.v.

Nạp chồng toán tử là một đa hình thời gian biên dịch. Đó là một ý tưởng mang lại ý nghĩa đặc biệt cho một toán tử hiện có trong C++ mà không làm thay đổi ý nghĩa ban đầu của nó.
Ví dụ.

       int a;
      float b,sum;
      sum=a+b;

Ở đây, biến “a” và “b” thuộc kiểu “int” và “float”, là kiểu dữ liệu có sẵn. Do đó, toán tử cộng '+' có thể dễ dàng cộng nội dung của “a” và “b”. Điều này là do toán tử cộng “+” được xác định trước để chỉ thêm các biến có kiểu dữ liệu dựng sẵn.
 

Bây giờ, hãy xem xét một ví dụ khác

hạng A
{

};

int main()
{
      A   a1,a2,a3;

a3= a1 + a2;

      trả về 0;
}

Trong ví dụ này, chúng ta có 3 biến “a1”, “a2” và “a3” thuộc loại “class A”. Ở đây chúng tôi đang cố gắng thêm hai đối tượng “a1” và “a2”, thuộc loại i do người dùng xác định. e. thuộc loại “hạng A” sử dụng toán tử “+”. Điều này không được phép vì toán tử cộng “+” được xác định trước để chỉ hoạt động trên các kiểu dữ liệu tích hợp. Nhưng ở đây, “class A” là kiểu do người dùng định nghĩa nên trình biên dịch sẽ báo lỗi. Đây là lúc khái niệm “Toán tử quá tải” xuất hiện.
Bây giờ, nếu người dùng muốn tạo toán tử “+” để thêm hai đối tượng lớp, người dùng phải xác định lại ý nghĩa của toán tử “+” sao cho nó thêm hai đối tượng lớp. Điều này được thực hiện bằng cách sử dụng khái niệm “Toán tử quá tải”. Vì vậy, ý tưởng chính đằng sau “Nạp chồng toán tử” là sử dụng các toán tử C++ với các biến lớp hoặc đối tượng lớp. Việc xác định lại ý nghĩa của các toán tử thực sự không làm thay đổi ý nghĩa ban đầu của chúng; .
Một ví dụ đơn giản và đầy đủ 
 

CPP




#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
10

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
11
#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
12
12 + i9
0

 

12 + i9
1
12 + i9
2

12 + i9
3____24

12 + i9
5
12 + i9
6
12 + i9
7

12 + i9
8
12 + i9
4

12 + i9
5
12 + i9
1
12 + i9
6
12 + i9
3
12 + i9
6
12 + i9
5

12 + i9
5

12 + i9
5
12 + i9
8

12 + i9
5
12 + i9
20

12 + i9
5
12 + i9
22
12 + i9
23
12 + i9
24

________ 225 ________ 226

12 + i9
25
12 + i9
28

12 + i9
25
12 + i9
20

12 + i9
25
12 + i9
22
12 + i9
23

12 + i9
5
12 + i9
25

12 + i9
5
12 + i9
27
12 + i9
28
12 + i9
29
12 + i9
20
12 + i9
21
12 + i9
22

12 + i9
23

 

12 + i9
6
12 + i9
25

12 + i9
26

12 + i9
5
12 + i9
28

12 + i9
5
12 + i9
20

12 + i9
5____522

12 + i9
25

Đầu ra

12 + i9

đầu ra.  

12 + i9

 

 

Sự khác biệt giữa hàm toán tử và hàm thông thường là gì? . Điểm khác biệt duy nhất là tên của hàm toán tử luôn là từ khóa toán tử theo sau là ký hiệu của toán tử và các hàm toán tử được gọi khi toán tử tương ứng được sử dụng.
Operator functions are the same as normal functions. The only differences are, that the name of an operator function is always the operator keyword followed by the symbol of the operator and operator functions are called when the corresponding operator is used. 
Sau đây là một ví dụ về hàm toán tử toàn cục.

Đầu ra

12 + i9

Chúng ta có thể quá tải tất cả các toán tử không? . Sau đây là danh sách các toán tử không thể nạp chồng.
Almost all operators can be overloaded except a few. Following is the list of operators that cannot be overloaded. 

12 + i9
2

Các toán tử có thể bị quá tải

Chúng ta có thể quá tải

  • toán tử đơn nguyên
  • toán tử nhị phân
  • Các toán tử đặc biệt ( [ ], () v.v)
     

Nhưng, trong số đó, có một số toán tử không thể quá tải. họ đang

  • Toán tử phân giải phạm vi.
  • Toán tử lựa chọn thành viên
  • Lựa chọn thành viên thông qua                               *                             
     

Con trỏ tới biến thành viên

  • Điều hành có điều kiện ?
  • Toán tử sizeof                                       sizeof()
     

Các toán tử có thể bị quá tải

  1. Số học nhị phân     ->     +, -, *, /, %
  2. Số học đơn phương     ->     +, -, ++, —
  3. Bài tập     ->       =, +=,*=, /=,-=, %=
  4. Một chút khôn ngoan      ->     & ,. , << , >> , ~ , ^
  5. Bỏ tham chiếu     ->     (->)
  6. Cấp phát và hủy cấp phát bộ nhớ động     ->     Mới, xóa
  7. Đăng ký     ->     [ ]
  8. Lệnh gọi hàm     ->     ()
  9. Hợp lý      ->     &,. . ,
  10. Relational     ->     >, < , = =, <=, >=

Tại sao các toán tử đã nêu ở trên không thể bị quá tải?

1. sizeof – Điều này trả về kích thước của đối tượng hoặc kiểu dữ liệu được nhập làm toán hạng. Điều này được đánh giá bởi trình biên dịch và không thể được đánh giá trong thời gian chạy. Việc gia tăng thích hợp của một con trỏ trong một mảng các đối tượng hoàn toàn phụ thuộc vào toán tử sizeof. Thay đổi ý nghĩa của nó bằng cách sử dụng quá tải sẽ khiến một phần cơ bản của ngôn ngữ bị sụp đổ

2. đánh máy. Điều này cung cấp cho chương trình CPP khả năng khôi phục loại dẫn xuất thực tế của đối tượng được tham chiếu bởi một con trỏ hoặc tham chiếu. Đối với toán tử này, toàn bộ vấn đề là xác định duy nhất một loại. Nếu chúng ta muốn làm cho một loại do người dùng định nghĩa 'trông' giống như một loại khác, thì có thể sử dụng tính đa hình nhưng ý nghĩa của toán tử typeid phải không thay đổi, nếu không có thể phát sinh các vấn đề nghiêm trọng

3. Độ phân giải phạm vi (. ). Điều này giúp xác định và chỉ định ngữ cảnh mà mã định danh đề cập đến bằng cách chỉ định một không gian tên. Nó được đánh giá hoàn toàn trong thời gian chạy và hoạt động trên tên chứ không phải giá trị. Các toán hạng của độ phân giải phạm vi là các biểu thức ghi chú có kiểu dữ liệu và CPP không có cú pháp để nắm bắt chúng nếu nó bị quá tải. Vì vậy, về mặt cú pháp không thể quá tải toán tử này

4. Toán tử truy cập thành viên lớp (. (chấm),. * (con trỏ tới toán tử thành viên)). Tầm quan trọng và việc sử dụng ẩn của các toán tử truy cập thành viên lớp có thể được hiểu thông qua ví dụ sau

C++




12 + i9
24

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
11
#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
12
12 + i9
0

 

12 + i9
1
12 + i9
29

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
100
12 + i9
3
12 + i9
4

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
100
12 + i9
6
#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
105

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
100
12 + i9
6
#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
108

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
100
12 + i9
8
12 + i9
4

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
100
#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
113
12 + i9
6
#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
115
12 + i9
6
#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
117

12 + i9
5
#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
119____1120

12 + i9
5
#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
119____1123

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
100
12 + i9
25

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
100
12 + i9
27
#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
128

12 + i9
5
12 + i9
00____329
12 + i9
02

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
100
12 + i9
25

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
100
12 + i9
06

12 + i9
5
12 + i9
08

12 + i9
5
12 + i9
10____1119
12 + i9
12

12 + i9
5
12 + i9
14____1119
12 + i9
16

12 + i9
5
12 + i9
22
12 + i9
19

#include 
using namespace std;

class Complex {
  public:
    Complex(float realpart=0, float imaginarypart=0);
    Complex Add(const Complex &c) const;
    float GetReal() const;
    float GetImaginary() const;
    void SetReal(float r);
    void SetImaginary(float i);
  private:
    float real;
    float imaginary;
};

Complex::Complex(float realpart, float imaginarypart) {
  SetReal(realpart);
  SetImaginary(imaginarypart);
}

void Complex::SetReal(float r) {
  real = r;
}

void Complex::SetImaginary(float i) {
  imaginary = i;
}

float Complex::GetReal() const {
  return real;
}

float Complex::GetImaginary() const {
  return imaginary;
}

Complex Complex::Add(const Complex &c) const {
  return Complex(GetReal() + c.GetReal(), GetImaginary() + c.GetImaginary());
}

Complex operator+(const Complex &c1, const Complex &c2) {
  return c1.Add(c2);
}

int main(void) {
  Complex x(-1,3);
  Complex y(5,4);
  Complex z = x + y;
  cout << z.GetReal() << '+' << z.GetImaginary() << 'i' << endl;
  return 0;
}
100
12 + i9
25

12 + i9
23

12 + i9
6
12 + i9
24

12 + i9
5
12 + i9
26

12 + i9
5
12 + i9
28

12 + i9
5
12 + i9
30

12 + i9
5____522

12 + i9
5
12 + i9
22
12 + i9
35

12 + i9
25

Đầu ra

12 + i9
2

Câu lệnh ComplexNumber c3 = c1 + c2; . toán tử+ (c2); . Đối số c1 được chuyển hoàn toàn bằng cách sử dụng '. ' nhà điều hành. Câu lệnh tiếp theo cũng sử dụng toán tử dấu chấm để truy cập hàm thành viên print và chuyển c3 làm đối số. Do đó, để đảm bảo một hệ thống truy cập các thành viên lớp đáng tin cậy và không mơ hồ, cơ chế được xác định trước sử dụng các toán tử truy cập thành viên lớp là hoàn toàn cần thiết. Bên cạnh đó, các toán tử này cũng hoạt động trên tên chứ không phải giá trị và không có quy định (về mặt cú pháp) để quá tải chúng

5. ternary hoặc có điều kiện (?. ). Toán tử bậc ba hoặc điều kiện là biểu diễn viết tắt của câu lệnh if-else. Trong toán tử, các biểu thức đúng/sai chỉ được đánh giá trên cơ sở giá trị thực của biểu thức điều kiện.  

câu lệnh điều kiện? . biểu thức2 (khác)

Một hàm nạp chồng toán tử bậc ba cho một lớp nói ABC sử dụng định nghĩa

Toán tử ABC?. (điều kiện bool, ABC trueExpr, ABC falseExpr);

sẽ không thể đảm bảo rằng chỉ một trong các biểu thức được đánh giá. Do đó, toán tử bậc ba không thể bị quá tải

++ được sử dụng như thế nào trong nạp chồng toán tử?

Toán tử gia tăng hậu tố ++ có thể được nạp chồng cho một loại lớp bằng cách khai báo một hàm không phải thành viên toán tử operator++() với hai đối số, đối số thứ nhất có loại lớp và đối số thứ hai có loại . Ngoài ra, bạn có thể khai báo một hàm thành viên operator++() với một đối số có kiểu int. . Alternatively, you can declare a member function operator operator++() with one argument having type int .

Làm cách nào để cộng hai số phức trong C#?

Chúng ta đã xác định số phức thứ nhất và số phức thứ hai bằng các biến 'num1' và 'num2'. Tính tổng các giá trị của biến 'num1' và 'num2'. Nhóm phần thực và phần ảo của số phức rồi cộng giá trị . In phép cộng hai số phức.

Chúng ta có thể quá tải toán tử += không?

Điều này có nghĩa là C++ có khả năng cung cấp cho các toán tử một ý nghĩa đặc biệt cho một kiểu dữ liệu, khả năng này được gọi là nạp chồng toán tử. Ví dụ: chúng ta có thể quá tải một toán tử '+' trong một lớp như Chuỗi để chúng ta có thể nối hai chuỗi chỉ bằng cách sử dụng + .

Hai cách nạp chồng một toán tử là gì?

Các kiểu nạp chồng toán tử trong C++ . Nạp chồng toán tử một ngôi. Nạp chồng toán tử nhị phân .