Operator overloading in C #
Overloading Operator is Operator Overload. You can redefine or overload most existing operators in C #. Therefore, a programmer can use operators with self-defined types (user-defined).
Operators overloaded in C # are functions with special names: the symbol for the operator being defined will have the operator keyword immediately behind. Similar to any other function, an overloaded operator has a return type and a parameter list.
For example, consider the following function:
public static Hop operator + (Hop b, Hop c)
{
Hop hopA = new Hop ();
hopA.chieu_dai = b.chieu_dai + c.chieu_dai;
hopA.chieu_rong = b.chieu_rong + c.chieu_rong;
hopA.chieu_cao = b.chieu_cao + c.chieu_cao;
return hopA;
}
The above function implements the plus (+) operator for a class Hop self-defined (user-defined). It adds the properties of two Hop objects and returns the Hop result object.
Deploy operator overloading in C #
The following example illustrates how to implement operator overloading in C #: create two classes named Hop and Thuinh respectively :
using System ; namespace VdNapChongToanTu {
// Lớp Hop chứa các thuộc tính và phương thức. class Hop { private double chieu_dai ; // Chiều dài của hộp private double chieu_rong ; // Chiều rộng của hộp private double chieu_cao ; // Chiều cao của hộp public double tinhTheTich () { return chieu_dai * chieu_rong * chieu_cao ; } public void ganChieuDai ( double dai ) { chieu_dai = dai ; } public void ganChieuRong ( double rong ) { chieu_rong = rong ; } public void ganChieuCao ( double cao ) { chieu_cao = cao ; } // Nạp chồng toán tử + để cộng hai object Hop public static Hop operator + ( Hop b , Hop c ) { Hop hopA = new Hop (); hopA.chieu_dai = b.chieu_dai + c.chieu_dai; hopA.chieu_rong = b.chieu_rong + c.chieu_rong; hopA.chieu_cao = b.chieu_cao + c.chieu_cao; return hopA ; } }
// Lớp ThuTinh chứa phương thức main() để thao tác trên đối tượng Hop. class ThuTinh { static void Main ( string [] args ) { Hop Hop 1 = new Hop (); // Khai báo Hop 1 thuộc loại Hop Hop Hop 2 = new Hop (); // Khai báo Hop 2 thuộc loại Hop Hop Hop 3 = new Hop (); // Khai báo Hop 3 thuộc loại Hop double TheTich = 0.0 ; // Lưu thể tích Hop ở đây // Thông số của Hop1 Hop1 . ganChieuDai ( 6.0 ); Hop 1 . ganChieuRong ( 7.0 ); Hop 1 . ganChieuCao ( 5.0 ); // Thông số của Hop2 Hop 2 . ganChieuDai ( 12.0 ); Hop 2 . ganChieuRong ( 13.0 ); Hop 2 . ganChieuCao ( 10.0 ); // Thể tích của Hop1 TheTich = Hop 1 . tinhTheTich (); Console . WriteLine ( "Thể tích của Hop 1: {0}" , TheTich ); // Thể tích của Hop2 TheTich = Hop 2 . tinhTheTich (); Console . WriteLine ( "Thể tích của Hop 2: {0}" , TheTich ); // Cộng 2 thể tích như bên dưới: Hop 3 = Hop 1 + Hop 2 ; // Thể tích của Hop3 TheTich = Hop 3 . tinhTheTich (); Console . WriteLine ( "Thể tích của Hop3: {0}" , TheTich ); Console . ReadKey (); } } }
Use the Console.ReadKey () command; to see the results more clearly.
Compiling and running the above C # program will produce the following results:
Volume of Hop1: 210
Volume of Hop2: 1560
Volume of Hop3: 5400
Operator can be overloaded and cannot be overloaded in C #
The following table describes overloaded operators in C #:
Operator Description +, -,!, ~, ++, - These unary operators receive an operand and can be overloaded. +, -, *, /,% These binary operators receive an operand and can be overloaded. =,! =, <,>, <=,> = The comparison operators can be overloaded. &&, || Conditional logical operators cannot be overloaded directly. + =, - =, * =, / =,% = The assignment operators cannot be overloaded. =,.,?:, ->, new, is, sizeof, typeof These operators can not be overloaded.For example
From the contents discussed above, we inherited the above example and overloaded some operators in C #: above we created two classes Hop, ThuTinh , now we modify the code of each class as follows:
using System ; namespace VdNapChongToanTu {
// Lớp Hop chứa các thuộc tính và phương thức. class Hop { private double chieu_dai ; // Chiều dài của hộp private double chieu_rong ; // Chiều rộng của hộp private double chieu_cao ; // Chiều cao của hộp public double tinhTheTich () { return chieu_dai * chieu_rong * chieu_cao ; } public void ganChieuDai ( double dai ) { chieu_dai = dai ; } public void ganChieuRong ( double rong ) { chieu_rong = rong ; } public void ganChieuCao ( double cao ) { chieu_cao = cao ; } // Nạp chồng toán tử + để cộng hai object Hop public static Hop operator + ( Hop b , Hop c ) { Hop hopA = new Hop (); hopA.chieu_dai = b.chieu_dai + c.chieu_dai; hopA.chieu_rong = b.chieu_rong + c.chieu_rong; hopA.chieu_cao = b.chieu_cao + c.chieu_cao; return hopA ; }
public static bool operator == ( Hop lhs , Hop rhs ) { bool status = false ; if ( lhs .chieu_ dai == rhs .chieu_ dai && lhs .chieu_ cao == rhs .chieu_ cao && lhs .chieu_rong == rhs .chieu_rong ) { status = true ; } return status ; } public static bool operator !=( Hop lhs , Hop rhs ) { bool status = false ; if ( lhs .chieu_ dai != rhs .chieu_ dai || lhs .chieu_ cao != rhs .chieu_ cao || lhs .chieu_rong != rhs .chieu_rong ) { status = true ; } return status ; } public static bool operator <( Hop lhs , Hop rhs ) { bool status = false ; if ( lhs .chieu_ dai < rhs .chieu_ dai && lhs .chieu_ cao < rhs .chieu_ cao && lhs .chieu_rong < rhs .chieu_rong ) { status = true ; } return status ; } public static bool operator >( Hop lhs , Hop rhs ) { bool status = false ; if ( lhs .chieu_ dai > rhs .chieu_ dai && lhs .chieu_ cao > rhs .chieu_ cao && lhs .chieu_rong > rhs .chieu_rong ) { status = true ; } return status ; } public static bool operator <=( Hop lhs , Hop rhs ) { bool status = false ; if ( lhs .chieu_ dai <= rhs .chieu_ dai && lhs .chieu_ cao <= rhs .chieu_ cao && lhs .chieu_rong <= rhs .chieu_rong ) { status = true ; } return status ; } public static bool operator >=( Hop lhs , Hop rhs ) { bool status = false ; if ( lhs .chieu_ dai >= rhs .chieu_ dai && lhs .chieu_ cao >= rhs .chieu_ cao && lhs .chieu_rong >= rhs .chieu_rong ) { status = true ; } return status ; } public override string ToString () { return String . Format ( "({0}, {1}, {2})" , chieu_dai , chieu_rong , chieu_cao ); } } // Lớp ThuTinh chứa phương thức main() để thao tác trên đối tượng Hop. class ThuTinh { static void Main ( string [] args ) { Hop Hop 1 = new Hop (); // Khai báo Hop 1 thuộc loại Hop Hop Hop 2 = new Hop (); // Khai báo Hop 2 thuộc loại Hop Hop Hop 3 = new Hop (); // Khai báo Hop 3 thuộc loại Hop
Hop Hop 4 = new Hop (); // Khai báo Hop 4 thuộc loại Hop double TheTich = 0.0 ; // Lưu thể tích Hop ở đây // Thông số của Hop1 Hop1 . ganChieuDai ( 6.0 ); Hop 1 . ganChieuRong ( 7.0 ); Hop 1 . ganChieuCao ( 5.0 ); // Thông số của Hop2 Hop 2 . ganChieuDai ( 12.0 ); Hop 2 . ganChieuRong ( 13.0 ); Hop 2 . ganChieuCao ( 10.0 ); // Hiển thị các hộp sử dụng nạp chồng ToString(): Console . WriteLine ( " Hop 1: {0}" , Hop1 . ToString ()); Console . WriteLine ( " Hop 2: {0}" , Hop2 . ToString ()); // Thể tích Hop1 TheTich = Hop1 . tinhTheTich (); Console . WriteLine ( "Thể tích Hop 1 : {0}" , TheTich ); // Thể tích Hop2 TheTich = Hop2 . tinhTheTich (); Console . WriteLine ( "Thể tích Hop 2 : {0}" , TheTich ); // Cộng 2 đối tượng như bên dưới: Hop 3 = Hop 1 + Hop 2 ; Console . WriteLine ( "Hop3: {0}" , Hop 3 . ToString ()); // Thể tích Hop3 TheTich = Hop 3 . tinhTheTich (); Console . WriteLine ( "Thể tích Hop3 : {0}" , TheTich ); //So sánh các hộp if ( Hop 1 > Hop 2 ) Console . WriteLine ( " Hop 1 lớn hơn Hop 2" ); else Console . WriteLine ( " Hop 1 không lớn hơn Hop 2" ); if ( Hop 1 < Hop 2 ) Console . WriteLine ( " Hop 1 nhỏ hơn Hop 2" ); else Console . WriteLine ( " Hop 1 không nhỏ hơn Hop 2" ); if ( Hop 1 >= Hop 2 ) Console . WriteLine ( " Hop 1 lớn hơn hoặc bằng Hop 2" ); else Console . WriteLine ( " Hop 1 không lớn hơn hoặc bằng Hop 2" ); if ( Hop 1 <= Hop 2 ) Console . WriteLine ( " Hop 1 nhỏ hơn hoặc bằng Hop 2" ); else Console . WriteLine ( " Hop 1 không nhỏ hơn hoặc bằng Hop 2" ); if ( Hop 1 != Hop 2 ) Console . WriteLine ( " Hop 1 không bằng Hop 2" ); else Console . WriteLine ( " Hop 1 không lớn hơn hoặc bằng Hop 2" ); Hop 4 = Hop 3 ; if ( Hop 3 == Hop 4 ) Console . WriteLine ( " Hop 3 bằng Hop 4" ); else Console . WriteLine ( " Hop 3 không bằng Hop 4" ); Console . ReadKey (); } } }
Compiling and running the above C # program will produce the following results:
Hop1: (6, 7, 5)
Hop2: (12, 13, 10)
Volume of Hop1: 210
Volume Hop2: 1560
Hop3: (18, 20, 15)
Volume Hop3: 5400
Hop1 is no bigger than Hop2
Hop1 is smaller than Hop2
Hop1 is no bigger than or equal to Hop2
Hop1 is less than or equal to Hop2
Hop1 is not equal to Hop2
Hop3 with Hop4
Follow tutorialspoint
Previous article: Polymorphism in C #
Next lesson: Interface in C #
You should read it
May be interested
- How to Get Compensation from a BUI Accidentif you were injured in a boating accident, then you can sue the boat operator who caused the injury. for example, the operator may have hit your own boat, or you may have been in the boat with the operator who was intoxicated. either way,...
- How does Dynamic NAT (dynamic NAT) and Overloading NAT work? (Part 3)in dynamic nat, a private ip address will be mapped with a public ip address in the public ip address group. to learn more about the dynamic mechanism of dynamic nat, please refer to the article below.
- OpenAI Announces Operator, an AI Agent That Automatically Performs Tasks on User's Behalfthe company behind chatgpt is unveiling a new way to use ai. openai has just announced operator, an ai agent that can automatically perform tasks for you.
- EXCEPT operator in SQL Serverthe except operator in sql server is used to return the rows in the first select statement that are not returned in the second select statement.
- Wildcard representation operator in Accesswildcard, also known as a wildcard, can be used to determine the location of a specific item when you don't remember exactly how it was written.
- Operator in JavaScriptwe see the following simple expression: 4 + 5 is equal to 9. here 4 and 5 are operands and '+' called operator - operator.
- UNION operator in SQL Serverthis tutorial explains how to use the union operator in sql server with specific syntax and examples.
- Speedtest announces VNPT as Vietnam's No. 1 Internet network operatorduring the test, vnpt achieved download speed of 64mbps and upload speed of 65.07mbps.
- How to Drive an Excavatoran excavator is a large piece of machinery used to dig dirt and debris out of construction sites. using one on a construction site requires operator training and a state operator license.https://study.com/articles/become_an_excavator_edu...
- How to chat with Viettel operatorviettel customers can access viettel.vn website to chat directly with viettel operator in the fastest way and completely free instead of having to call the switchboard to get assistance. this new way of not only helps you save costs (no call charges) but also helps your phone to easily understand what you are asking.