ssrs ean 128 The Rat ional Class with an Overloaded * = Operator in Software

Print Denso QR Bar Code in Software The Rat ional Class with an Overloaded * = Operator

EXAMPLE 9.6 The Rat ional Class with an Overloaded * = Operator
QR Code JIS X 0510 Decoder In None
Using Barcode Control SDK for Software Control to generate, create, read, scan barcode image in Software applications.
Generate QR Code ISO/IEC18004 In None
Using Barcode generator for Software Control to generate, create QR Code 2d barcode image in Software applications.
class Rational { public: Rational(int =O, int =l); Rational& operator=(const Rational&); Rational& operator*=(const Rational&); // other declarations go here private: int num, den; // other declarations go here >; Rational& Rational::operator*=(const num = num*r.num; den = den*r.den; return *this; Rational& r)
Reading QR Code JIS X 0510 In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
QR Code ISO/IEC18004 Generation In C#
Using Barcode maker for .NET framework Control to generate, create QR Code image in .NET applications.
Theoperator operator*= has the same syntax and nearly the same implementation as the basic assignment operator operator=. By returning * thi s, the operator can be chained, like this:
Quick Response Code Drawer In .NET
Using Barcode encoder for ASP.NET Control to generate, create QR Code ISO/IEC18004 image in ASP.NET applications.
QR Code 2d Barcode Creation In Visual Studio .NET
Using Barcode drawer for .NET Control to generate, create QR Code JIS X 0510 image in VS .NET applications.
X */ y *= z;
Creating QR Code In VB.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create Denso QR Bar Code image in .NET applications.
ECC200 Generation In None
Using Barcode printer for Software Control to generate, create DataMatrix image in Software applications.
OVERLOADING
Encode Bar Code In None
Using Barcode creation for Software Control to generate, create bar code image in Software applications.
Encoding Code-39 In None
Using Barcode encoder for Software Control to generate, create Code 39 image in Software applications.
OPERATORS
Drawing GTIN - 13 In None
Using Barcode printer for Software Control to generate, create UPC - 13 image in Software applications.
Painting Bar Code In None
Using Barcode printer for Software Control to generate, create barcode image in Software applications.
[CHAP. 9
Drawing Code 11 In None
Using Barcode creator for Software Control to generate, create Code11 image in Software applications.
Making UPC Symbol In Java
Using Barcode printer for Java Control to generate, create GTIN - 12 image in Java applications.
It is also important to ensure that overloaded operators perform consistently with each other. For example, the following two lines should have the same effect, even though they call different operators:
Barcode Generation In None
Using Barcode generator for Office Word Control to generate, create bar code image in Microsoft Word applications.
Draw Code 39 Extended In Java
Using Barcode generation for Android Control to generate, create Code 39 Full ASCII image in Android applications.
x = x*y; x *= y
Code39 Encoder In Java
Using Barcode generator for Java Control to generate, create Code-39 image in Java applications.
GTIN - 12 Maker In Java
Using Barcode generation for Java Control to generate, create UCC - 12 image in Java applications.
OVERLOADING
Code 3/9 Generation In None
Using Barcode printer for Office Excel Control to generate, create Code 39 Extended image in Microsoft Excel applications.
Making EAN / UCC - 14 In .NET Framework
Using Barcode creator for Visual Studio .NET Control to generate, create UCC.EAN - 128 image in VS .NET applications.
RELATIONAL
OPERATORS
The six relational operators <, >, < =, >=, ==, and ! = can be overloaded the same way that the arithmetic operators are overloaded: as friend functions.
EXAMPLE 9.7 Overloading the Equality Operator = = in the Rat ional Class
Like other friend functions, the equality operator is declared above the pub1
i c section of the class:
class Rational { friend int operator==(const Rational&, const Rational&); friend Rational operator*(const Rational&, const Rational&); // other declarations go here public: Rational(int =0, int =l); Rational(const Rational&); Rational& operator=(const Rational&); // other declarations go here private: int num; int den; // other declarations go here 1 ; int operator== (const Rational& x, const Rational& y) return (x.num * y.den == y.num * x.den);
The test for equality of two fractions a/b and c/d is equivalent to the test a*d == b*c. So we end up using the equality operator for in ts to define the equality operator for Rat i onals. Note that the relational operators return an int type, representing either true (1) or false (0). 9.7 OVERLOADING THE STREAM OPERATORS
C++ allows you to overload the stream insertion operator >> for customizing input and the stream deletion operator << for customizing output. Like the arithmetic and relational operators, these should also be declared as friend functions. For a class T with data member d, the syntax for the output operator is
friend ostream& operator<<(ostream& { return ostr << t.d; } ostr, const T& t)
Here, ostream is a standard class defined (indirectly) in the iostream. h header file. Note that all the parameters and the return value are passed by reference.
CHAP. 91
OVERLOADING OPERATORS
This function can then be called using the same syntax that we used for fundamental types:
tout << "x = " C-C x -CC ", y = " -C-K y -CC endl;
Here is an example of how custom output can be written:
EXAMPLE 9.8 Overloading the Output Operator C-C the Rat ional #include <iostream.h> Class
class Rational { friend ostream& operator<<(ostream&, const Rational&); public: Rational(int n=O, int d=l) : num(n), den(d) { } // other declarations go here private: int num, den; // other declarations go here >; main0 Rational x(22,7), y(-3,8); tout << "x = << x << Y =
<< y =c< endl;
ostream&
operator<<(ostream&
ostr,
const
Rational&
return ostr << r.num <<
<< r.den;
J X= 2217, y sr -3/83
When the second line of main ( > executes, the expression cou t C-K I x = I executes first. This calls the standard output operator C-C, passing the standard output stream cou t and the string I x = I to it. As usual, this inserts the string into the output stream and then returns a reference to cou t. This return value is then passed with the object x to the overloaded << operator. This call to operator<< executes with cou t in place of o s t r and with x in place of r. The result is the execution of the line
return ostr << r.num << '/' << r.den;
which inserts 2 2 / 7 into the output stream and returns a reference to cou t. Then another call to the standard output operator << and another call to the overloaded operator are made, with the output (a reference to tout) of each call cascading into the next call as input. Finally the last call to the standard output operator << is made, passing cou t and endl. This flushes the stream, causing the complete line x = 22/7, y = -3/8 to be printed.
Copyright © OnBarcode.com . All rights reserved.