how to overload >>,<< operators

Recommended Answers

All 3 Replies

usually :

std::ostream& operator <<(std::ostream& ostrm)
{
    ostrm << "Hello its overloaded\n";
   return ostrm;
}
std::istream& operator >>(std::istream& istrm)
{
   cout<<"Enter a number  :  ";
   istrm >> MyClass::myNumber;

    return istrm;
}

note overloading the << operator should be a friend, so you can use it like this :

cout << myClassVar;

if not then you would use this syntax :

myClassVar <<cout;

which is confusing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.