class myClass
{
public:
string name;
int age;
friend ostream& operator<<(ostream& out, myClass& data)
{
return out << "My name is " << name << " and I am " << age << endl;
}
};
// Which allows this:
cout << instanceOfMyClass;
Edit:
I see the problem! If _name is a constchar* type instead of a std::string type, then you are invoking this function: constchar* operator+ (constchar* lhs, constchar* rhs);
That function doesn't exist and you can't make it because you can't redefine operator functions with only basic types.
Second, if age is an integer, then you are invoking constchar* operator+(constchar* lhs, constint rhs)
which also doesn't exist and can't be created because it only uses basic types.
C++ is much lower-level than Java. You are closer to the processor and so a lot of this automatic type stuff doesn't exist. In C++ we know exactly what every bit does at every moment instead of relying on high-level magic to do things for us. This lets us write code that is more specific to what we need and prevents us from accidently calling functions that will add overhead to our project. It may be more difficult to write at times, but the trade-off is that it will usually be faster (fewer instructions). There is also less "guessing" when we want to know exactly what is happening.