Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting date class to string

Tags:

c++

I have a Date class

class Date { int dd, mm, yyyy};

I have written the rule of 3, and all that works. I want to convert the Date into a string. Do I need a conversion operator string() to do so? thx!

like image 239
itcplpl Avatar asked Jun 24 '26 00:06

itcplpl


1 Answers

std::ostream& operator<<(std::ostream& s, const Date& d)
{
    s << "Format your date object here";
    return s;
}

In C++ you use streams if you want toString() like functionality.

So for example you could do

s << mm << "/" << dd << "/" << yyyy;
like image 183
Jesus Ramos Avatar answered Jun 25 '26 13:06

Jesus Ramos