#include#include using namespace std; int main() { std::ostringstream stream; int num1 = 10; int num2 = 20; stream << num1 << " + " << num2 << " = " << num1 + num2; string output = stream.str(); cout << output << endl; return 0; }
#include#include using namespace std; class Person { string name; int age; public: Person(string name, int age) : name(name), age(age) {} friend std::ostream& operator<<(std::ostream& os, const Person& p); }; std::ostream& operator<<(std::ostream& os, const Person& p) { os << p.name << " " << p.age; return os; } int main() { std::ostringstream stream; Person person1("Bob", 30); Person person2("Alice", 25); stream << "Person 1: " << person1 << "\n"; stream << "Person 2: " << person2 << "\n"; string output = stream.str(); cout << output; return 0; }
Person 1: Bob 30 Person 2: Alice 25Both examples use the