Mega Code Archive

 
Categories / C++ / Overload
 

Demonstrate a custom inserter and extractor for objects of type ThreeD

#include <iostream> using namespace std; class ThreeD {   int x, y, z; public:   ThreeD(int a, int b, int c) { x = a; y = b; z = c; }   // Make the inserter and extractor friends of ThreeD.   friend ostream &operator<<(ostream &stream, const ThreeD &obj);   friend istream &operator>>(istream &stream, ThreeD &obj); }; // ThreeD inserter. Display the X, Y, Z coordinates. ostream &operator<<(ostream &stream, const ThreeD &obj) {   stream << obj.x << ", ";   stream << obj.y << ", ";   stream << obj.z << "\n";   return stream; } // ThreeD extractor. Get three-dimensional values. istream &operator>>(istream &stream, ThreeD &obj) {   stream >> obj.x >> obj.y >> obj.z;   return stream; } int main() {   ThreeD td(1, 2, 3);   cout << "The coordinates in td: " << td << endl;   cout << "Enter new three-d coordinates: ";   cin >> td;   cout << "The coordinates in td are now: " << td << endl;   return 0; }