|
|
| The Complete C++ Training Course (3rd Edition) (ISBN: 0130895644) |
 |
List Price: $109.99
Our Price: $76.99
Used Price: $26.93
Release Date: 15 December, 2000
Manufacturer: Prentice Hall PTR (CD-ROM)
Sales Rank: 82,673
Author: Deitel and Deitel
|
More Info
|
|
|
Overloading << and >>
|
2002-01-14 23:47:10
|
| |
cpp
|
|
|
|
|
Category: source:cpp:overloading
|
|
Description: How to overload the << and >> operators
|
|
Platform: unix/win
|
|
Author: detour
|
|
Viewed: 1864
|
|
Rating: 3.8/5 (4 votes)
|
|
|
| If you have any questions about
this piece of code or still need help, try posting your question on the forum. |
/* ovrldcout.cpp by detour@metalshell.com
*
* This is a example of how to overload the <<
* and >> operator in c++.
*
* http://www.metalshell.com/
*
*/
#include <string>
#include <iostream>
class Beer{
private:
char btype[30];
int supply;
public:
Beer() { supply = 6; strcpy(btype, "Bud Light"); }
friend ostream& operator<<(ostream& output, const Beer &b) {
output << "Beer: " << b.btype << "\n" << "Supply: "
<< b.supply << endl;
return output;
}
friend istream& operator>>(istream& input, Beer &b) {
cin >> b.supply;
return input;
}
};
int main() {
Beer a;
/* This cout will use your overloaded << function */
cout << a;
cout << "Enter your new supply #: ";
/* This cin will use your overloaded >> function */
cin >> a;
cout << a;
}
|
|
|