954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

operator overloading

how to overload >>,<< operators

adsiq
Newbie Poster
1 post since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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 <

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

i too have and still have problems with operator overloading, i found the following website helpful. i hope that it does the same for you.

http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/

shaneselling
Newbie Poster
3 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You