Skip to content

Instantly share code, notes, and snippets.

@jeffcrouse
Created October 27, 2013 15:58
Show Gist options
  • Select an option

  • Save jeffcrouse/7184171 to your computer and use it in GitHub Desktop.

Select an option

Save jeffcrouse/7184171 to your computer and use it in GitHub Desktop.
// tostring.cpp
#include <iostream>
using namespace std;
class Rectangle {
public:
void init(float _x, float _y, float _width, float _height) {
x = _x;
y = _y;
width = _width;
height = _height;
}
friend ostream& operator<<(ostream&, const Rectangle&); // optional
float x, y;
float width, height;
};
ostream& operator<<(ostream &strm, const Rectangle &r) {
return strm << "Rectangle(x=" << r.x << ", y=" << r.y << ", width=" << r.width << ", height=" << r.height << ")";
}
int main ()
{
Rectangle r;
r.init(10, 20, 30, 40);
cout << r << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment