Question: How do we overload the < < operator when trying to cout a string type? For example: class MyClass{ friend ostream& operator <

How do we overload the << operator when trying to cout a string type?

For example:

class MyClass{

friend ostream& operator<<(ostream&, MyClass&);

public:

void print();

private:

std::string name;

int age;

};

When overloading my << operator in my implementation file, I did:

ostream& operator<<(ostream& os, MyClass& mco){

return os << mco.name << ", " << mco.age;

}

Do I have to change the string type to char to make this work or is there a way to overload this operator to output a string? Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!