Print string representation of an object in C++
This post will discuss how to print string representation of an object in C++.
In contrast to Java, C++ does not offer any implicit way to print an object. With C++11, std::to_string is added to the standard, which allows you to convert an integer to a std::string. However, C++ does not permit overloading of std::to_string for objects.
1. Overload operator<<
The standard solution to print an object implicitly in the output stream is to overload the operator<< for ostream. Now, whenever the string representation of an object is required, you can directly send the object contents to the output stream.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> class Point { public: int x, y; Point(): x(0), y(0) {} Point(int x, int y): x(x), y(y) {} }; std::ostream& operator<<(std::ostream &s, const Point &point) { return s << "(" << point.x << ", " << point.y << ")"; } int main() { Point point(1, 2); std::cout << point << std::endl; return 0; } |
Output:
(1, 2)
In case you need to include the private and protected members of the class in its string representation, declare the operator<< as a friend function of the class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> class Point { int x, y; friend std::ostream& operator<<(std::ostream &s, const Point &point); public: Point(): x(0), y(0) {} Point(int x, int y): x(x), y(y) {} }; std::ostream& operator<<(std::ostream &s, const Point &point) { return s << "(" << point.x << ", " << point.y << ")"; } int main() { Point point(1, 2); std::cout << point << std::endl; return 0; } |
Output:
(1, 2)
2. Using std::stringstream
Alternatively, you can use a string stream to get the string representation of the object and store it for later use.
|
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 |
#include <iostream> #include <sstream> class Point { public: int x, y; Point(): x(0), y(0) {} Point(int x, int y): x(x), y(y) {} }; std::string getString(Point point) { std::stringstream ss; ss << "(" << point.x << ", " << point.y << ")"; return ss.str(); } int main() { Point point(1, 2); std::string str = getString(point); std::cout << str << std::endl; return 0; } |
Output:
(1, 2)
3. Creating toString() function
Finally, you can create a Java-style toString() function inside your class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> class Point { int x, y; public: Point(): x(0), y(0) {} Point(int x, int y): x(x), y(y) {} std::string toString() { return "(" + std::to_string(this->x) + ", " + std::to_string(this->y) + ")"; } }; int main() { Point point(1, 2); std::string str = point.toString(); std::cout << str << std::endl; return 0; } |
Output:
(1, 2)
That’s all about printing string representation of an object in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)