473,396 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Understanding operator << overloading

#include <iostream>
using namespace std;

int & fonksiyon(int&);
class Date
{
int mo, da, yr;
public:
Date(int m, int d, int y)
{
mo = m; da = d; yr = y;
}
friend ostream& operator<<(ostream& os, const Date& dt);
};

ostream& operator<<(ostream& os, const Date& dt)
{
os << dt.mo << '/' << dt.da << '/' << dt.yr;
return os;
}

int main()
{
Date dt(5, 6, 92);
cout <<"Date:" << dt << ".";
}


I thought about this thing much,and searched for a good explanation on web too,but I couldn't find what I need,and I'm bored.

The thing which I don't understand is:
ostream& operator<<(ostream& os, const Date& dt);

ostream& os is a reference for cout I think ,operator << function takes cout as an argument I think.

Then what does :
return os;

do?
Dec 19 '14 #1
2 1425
weaknessforcats
9,208 Expert Mod 8TB
Here you go:

Expand|Select|Wrap|Line Numbers
  1. cout << a << b << c;
  2.  
Everything in C++is a function. Above are three calls to operator<< in one statement. The above can be rewritten as:

Expand|Select|Wrap|Line Numbers
  1. operator<<(operator<<(operator<<(cout, a), b), c);
  2.  
  3.  
This is not very readable but you can see a call to operator<<(cout,a) must return cout for the call to operator<<cout,b). If your operator<< does not return an ostream& then you can't use it in nested examples like this.
Dec 19 '14 #2
Thanks for your clear answer :)
Dec 21 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Wilhelm Heramb | last post by:
What is the best practice to implement operator overloading for == and != that handles null on either lhs or rhs. Andreas :-)
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
6
by: jay | last post by:
In the c++ primer ,i get a program. A class's name is TT,and it define the operator overload! TT first; //constructor TT second(30);//constructor TT thrid(40://constructor...
7
by: Bas Nedermeijer | last post by:
Hello, i am having trouble to use the sort() function of a list<>. I cannot seem to overload the operator<(). The variables to compare are references, like Item* itemA; Item* itemB;
2
by: Funky | last post by:
In the code snippet below, I have a base class (originally written in C++) that byte swaps data when assigned or read from the class (thus, always storing the data as big-endian). As for those...
15
by: Bart Simpson | last post by:
Can anyone explain how this works. How may I implement this? I want to be able to use the operator on both lhs and rhs of assignment statements. The rhs is a no-brainer (at least I think I got it...
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
5
by: swcamry | last post by:
class bitset::reference { friend class bitset; reference(); // no public constructor public: ~reference(); operator bool () const; //...
0
by: Lee118 | last post by:
Hi I'm currently using business objects which all inherit a common base class. i.e. Person, and Department, both inherit from Entity. To not clutter the code, I want to put "== operator"...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.