print barcode with vb.net 12: Operator Overloading in Visual C#

Making USS Code 128 in Visual C# 12: Operator Overloading

12: Operator Overloading
Printing Code 128C In Visual C#.NET
Using Barcode generation for .NET framework Control to generate, create Code 128A image in VS .NET applications.
www.OnBarcode.com
Code 128A Decoder In C#.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Note the use of the explicit keyword, indicating that this requires an explicit cast from a Fraction to an int. The method itself simply divides the numerator by the denominator. Since you re returning an int, this is integer division, which means that any remainder will be discarded. You see the cast in the Run( ) method:
Drawing DataMatrix In Visual C#
Using Barcode drawer for .NET Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
Code 3 Of 9 Printer In C#.NET
Using Barcode creator for VS .NET Control to generate, create Code 39 Full ASCII image in .NET framework applications.
www.OnBarcode.com
int truncated = (int) f4;
Encoding Barcode In C#.NET
Using Barcode creation for .NET Control to generate, create Barcode image in VS .NET applications.
www.OnBarcode.com
QR Code Creator In C#.NET
Using Barcode creation for .NET Control to generate, create Quick Response Code image in Visual Studio .NET applications.
www.OnBarcode.com
The cast from an int to a Fraction, on the other hand, is perfectly safe, so it can be implicit. This is what it looks like in the Run( ) method:
UCC - 12 Creation In Visual C#.NET
Using Barcode generation for VS .NET Control to generate, create USS-128 image in Visual Studio .NET applications.
www.OnBarcode.com
Generate ISSN - 10 In C#
Using Barcode generation for .NET framework Control to generate, create ISSN - 13 image in .NET framework applications.
www.OnBarcode.com
Fraction f4 = f3 + 5;
Code 128C Scanner In .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Create USS Code 128 In None
Using Barcode creator for Online Control to generate, create ANSI/AIM Code 128 image in Online applications.
www.OnBarcode.com
Notice that there is no explicit cast in this statement. When you add the int to the Fraction, the int is implicitly cast to a Fraction. The implementation of this is to create a new Fraction object and to return it:
Code128 Printer In Java
Using Barcode maker for Java Control to generate, create Code 128A image in Java applications.
www.OnBarcode.com
Matrix 2D Barcode Creator In VS .NET
Using Barcode creation for .NET framework Control to generate, create Matrix image in .NET framework applications.
www.OnBarcode.com
public static implicit operator Fraction(int theInt) { Console.WriteLine("Implicitly converting int to Fraction"); return new Fraction(theInt); }
Code-128 Reader In Visual Basic .NET
Using Barcode decoder for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Barcode Creator In Visual Basic .NET
Using Barcode creator for VS .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Using the implicit cast operator causes the constructor to be invoked:
EAN-13 Supplement 5 Drawer In None
Using Barcode creator for Office Word Control to generate, create GTIN - 13 image in Word applications.
www.OnBarcode.com
UPC - 13 Encoder In Objective-C
Using Barcode drawer for iPhone Control to generate, create European Article Number 13 image in iPhone applications.
www.OnBarcode.com
public Fraction(int wholeNumber) { Console.WriteLine("In constructor taking a whole number"); numerator = wholeNumber; denominator = 1; }
Printing Code 128 Code Set B In VS .NET
Using Barcode maker for Visual Studio .NET Control to generate, create Code-128 image in .NET applications.
www.OnBarcode.com
Universal Product Code Version A Generator In VS .NET
Using Barcode creation for .NET framework Control to generate, create UPC-A image in Visual Studio .NET applications.
www.OnBarcode.com
You see this sequence of events represented in the output:
GTIN - 12 Creator In None
Using Barcode drawer for Microsoft Excel Control to generate, create UPCA image in Microsoft Excel applications.
www.OnBarcode.com
Code 128A Encoder In Objective-C
Using Barcode creation for iPhone Control to generate, create Code128 image in iPhone applications.
www.OnBarcode.com
Implicitly converting int to Fraction In constructor taking a whole number
Summary
You can overload operators in much the same way that you would overload methods. To overload an operator, use the static keyword with the operator keyword, and the name of the operator you re overloading. It is good programming practice to use operator overloading sparingly and to be sure that the meaning of the overload is obvious and intuitive. When you overload the equals (==) operator, you should also override the Equals( ) method for compatibility with other .NET languages. If you overload the == operator, you must also overload the != operator. Similarly, the < and > operators are paired, as are the <= and >= operators.
Summary |
You can also overload conversion operators to allow one type to be implicitly or explicitly cast to another type. When doing so, you must use the keyword implicit when the conversion is guaranteed to succeed without loss of information, and explicit when there is a risk that information might be lost. Now you know how to overload constructors, regular class methods, and even operators. That s a lot of overloading going on, but it s all with good reason to make your user-defined classes flexible and extensible. In the next chapter, we ll look at yet another method of extension: interfaces. Interfaces let you dictate the methods that a class will have, without using inheritance. It s complicated, but powerful, as you ll see.
Test Your Knowledge: Quiz
Question 12-1. What is operator overloading Question 12-2. Are operators implemented as properties, static methods, or instance methods Question 12-3. What keyword do you use to overload an operator Question 12-4. How does the compiler translate:
Fraction f3 = f2 + f1;
assuming that f2 and f1 are Fraction objects and you have overloaded the + operator for the Fraction class Question 12-5. Which of the following overloads are reasonable 1. Overloading the == operator for a Dog class such that two Dog objects with the same name are equal 2. Overloading the - operator of a Box class such that subtracting one Box object from another produces a new Box object with a volume equal to the difference between the other two Box objects 3. Overloading the -- operator of an Employee class so that the employee s hours are reduced 4. Overloading the + operator of a BankAccount class to merge the balance of two accounts Question 12-6. Which of the comparison operators are paired Question 12-7. What should you also do if you overload the == operator
|
Copyright © OnBarcode.com . All rights reserved.