'How to print the objects specific member using only object name? [duplicate]

string var = "Hello";
cout << var << endl;

We get the result using only the object, without the help of a member variable. I want to implement a class that will work like string. For example:

class Test {
public:
    int x = 3;
};

Test var2;
cout << var2 << endl;

How can I get implement the class so the cout line prints the value of x without referring to it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source