References and More Overloaded Operators in Software

Drawer Data Matrix ECC200 in Software References and More Overloaded Operators
References and More Overloaded Operators
Making DataMatrix In C#
Using Barcode printer for VS .NET Control to generate, create DataMatrix image in .NET framework applications.
This is illustrated in Display 84 The operator << should return its rst argument, which is of type ostream (the type of cout) Thus, the declaration for the overloaded operator << (to use with the class Money) should be as follows:
Making DataMatrix In VS .NET
Using Barcode generator for ASP.NET Control to generate, create Data Matrix 2d barcode image in ASP.NET applications.
class Money { public: friend ostream& operator <<(ostream& outs, const Money& amount);
Data Matrix 2d Barcode Encoder In Visual Studio .NET
Using Barcode creator for Visual Studio .NET Control to generate, create ECC200 image in Visual Studio .NET applications.
<< returns a stream
Creating DataMatrix In Visual Basic .NET
Using Barcode generator for Visual Studio .NET Control to generate, create ECC200 image in Visual Studio .NET applications.
Display 84 << as an Operator
UPC - 13 Creation In .NET Framework
Using Barcode printer for VS .NET Control to generate, create European Article Number 13 image in Visual Studio .NET applications.
cout << "I have " << amount << " in my purse\n";
ANSI/AIM Code 39 Creator In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create ANSI/AIM Code 39 image in .NET framework applications.
means the same as
Bar Code Creator In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create barcode image in ASP.NET applications.
((cout << "I have ") << amount) << " in my purse\n";
Bar Code Generator In Java
Using Barcode generator for Java Control to generate, create barcode image in Java applications.
and is evaluated as follows: First evaluate (cout << "I have "), which returns cout:
Bar Code Drawer In .NET
Using Barcode maker for ASP.NET Control to generate, create bar code image in ASP.NET applications.
((cout << "I have ") << amount) << " in my purse\n";
EAN13 Generator In .NET
Using Barcode printer for ASP.NET Control to generate, create GS1 - 13 image in ASP.NET applications.
The string "I have" is output
GS1 - 13 Creator In Java
Using Barcode encoder for Java Control to generate, create EAN 13 image in Java applications.
(cout << amount) << " in my purse\n";
UPC-A Supplement 5 Reader In .NET
Using Barcode reader for .NET Control to read, scan read, scan image in VS .NET applications.
Then evaluate (cout << amount), which returns cout:
Barcode Creator In .NET Framework
Using Barcode encoder for ASP.NET Control to generate, create bar code image in ASP.NET applications.
(cout << amount) << " in my purse\n";
Bar Code Creator In Java
Using Barcode drawer for Java Control to generate, create barcode image in Java applications.
The value of amount is output
Code 128A Printer In Visual Basic .NET
Using Barcode generator for .NET Control to generate, create Code 128 Code Set C image in .NET applications.
cout << " in my purse\n";
Generate Bar Code In C#
Using Barcode generation for .NET framework Control to generate, create bar code image in .NET applications.
Then evaluate cout << " in my purse\n", which returns cout:
Draw Barcode In VB.NET
Using Barcode generator for .NET Control to generate, create barcode image in .NET applications.
cout << " in my purse\n";
Read EAN-13 In VS .NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
The string "in my purse\n" is output
Recognize Code39 In .NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
cout;
Generate UPC-A Supplement 2 In .NET
Using Barcode generator for VS .NET Control to generate, create Universal Product Code version A image in .NET applications.
Since there are no more << operators, the process ends
Operator Overloading, Friends, and References
Once we have overloaded the insertion (output) operator <<, we will no longer need the member function output and will delete output from our de nition of the class Money The de nition of the overloaded operator << is very similar to the member function output In outline form, the de nition for the overloaded operator is as follows:
ostream& operator <<(ostream& outputStream, const Money& amount) {
<This part is the same as the body of Money::output which is given in Display 81 (except that dollars is replaced with amountdollars and cents is replaced by amountcents)>
return outputStream; } << and >>
return a reference
Note that the operator returns a reference The extraction operator >> is overloaded in a way that is analogous to what we described for the insertion operator << However, with the extraction (input) operator >>, the second argument will be the object that receives the input value, so the second parameter must be an ordinary call-by-reference parameter In outline form the de nition for the overloaded extraction operator >> is as follows:
istream& operator >>(istream& inputStream, Money& amount) {
<This part is the same as the body of Money::input, which is given in Display 81 (except that dollars is replaced with amountdollars and cents is replaced by amountcents)>
return inputStream; }
The complete de nitions of the overloaded operators << and >> are given in Display 85, where we have rewritten the class Money yet again This time we have rewritten the class so that the operators << and >> are overloaded to allow us to use these operators with values of type Money Note that you cannot realistically overload >> or << as member operators If << and >> are to work as we want, then the rst operand ( rst argument) must be cout or cin (or some le I/O stream) But if we want to overload the operators as members of, say, the class Money, then the rst operand would have to be the calling object and so would have to be of type Money, and that will not allow you to de ne the operators so they behave in the normal way for >> and <<
References and More Overloaded Operators
Display 85 Overloading << and >> (part 1 of 3)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include <iostream> #include <cstdlib> #include <cmath> using namespace std; //Class for amounts of money in US currency class Money { public: Money( ); Money(double amount); Money(int theDollars, int theCents); Money(int theDollars); double getAmount( ) const; int getDollars( ) const; int getCents( ) const; friend const Money operator +(const Money& amount1, const Money& amount2); friend const Money operator -(const Money& amount1, const Money& amount2); friend bool operator ==(const Money& amount1, const Money& amount2); friend const Money operator -(const Money& amount); friend ostream& operator <<(ostream& outputStream, const Money& amount); friend istream& operator >>(istream& inputStream, Money& amount); private: int dollars; //A negative amount is represented as negative dollars and int cents; //negative cents Negative $450 is represented as -4 and -50 int dollarsPart(double amount) const; int centsPart(double amount) const; int round(double number) const; }; int main( ) { Money yourAmount, myAmount(10, 9); cout << "Enter an amount of money: "; cin >> yourAmount; cout << "Your amount is " << yourAmount << endl; cout << "My amount is " << myAmount << endl; if (yourAmount == myAmount) cout << "We have the same amounts\n"; else cout << "One of us is richer\n"; Money ourAmount = yourAmount + myAmount;