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.
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 Show 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ó. int a; Ở đâ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() 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. 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 + i90
12 + i91 12 + i92 12 + i93____24 12 + i95 12 + i96 12 + i97 12 + i98 12 + i94 12 + i95 12 + i91 12 + i96 12 + i93 12 + i96 12 + i95 12 + i95 12 + i95 12 + i98 12 + i95 12 + i920 12 + i95 12 + i922 12 + i923 12 + i924 ________ 225 ________ 226 12 + i925 12 + i928 12 + i925 12 + i920 12 + i925 12 + i922 12 + i923 12 + i95 12 + i925 12 + i95 12 + i927 12 + i928 12 + i929 12 + i920 12 + i921 12 + i922 12 + i923
12 + i96 12 + i925 12 + i926 12 + i95 12 + i928 12 + i95 12 + i920 12 + i95____522 12 + i925 Đầ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. Đầ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. 12 + i92 Các toán tử có thể bị quá tảiChúng ta có thể quá tải
Nhưng, trong số đó, có một số toán tử không thể quá tải. họ đang
Con trỏ tới biến thành viên
Các toán tử có thể bị quá tải
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 + i924 #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 + i90
12 + i91 12 + i929 #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 + i93 12 + i94 #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 + i96 #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 + i96 #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 + i98 12 + i94 #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 + i96 #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 + i96 #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 + i95 #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 + i95 #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 + i925 #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 + i927 #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 + i95 12 + i900____329 12 + i902 #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 + i925 #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 + i906 12 + i95 12 + i908 12 + i95 12 + i910____1119 12 + i912 12 + i95 12 + i914____1119 12 + i916 12 + i95 12 + i922 12 + i919 #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 + i925 12 + i923 12 + i96 12 + i924 12 + i95 12 + i926 12 + i95 12 + i928 12 + i95 12 + i930 12 + i95____522 12 + i95 12 + i922 12 + i935 12 + i925 Đầu ra 12 + i92 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 . |