



(4 votes)/* 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; }