Why is this called operator overloading? Doesn't cout display custom types WITHOUT overload?

 // overload_date.cpp // compile with: /EHsc #include <iostream> using namespace std; 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 << dt; } 

    3 answers 3

    And what, in standard C ++ there is an operator

     ostream& operator<<(ostream& os, const Date& dt) 

    those. for the Date output?

    No, it is not. It is created by the programmer for its custom type.

    If I define, for example, the function

     myClass sqrt(myClass& m); 

    which will perform some of its own actions - you will not say - does not sqrt consider the square root without overloading?

    But, by the way, cout prints user-defined types, generally speaking, using overloading.

    Just in case, let me remind you what overloading is ...

    An overload of a function is understood as the definition of several functions (two or more) with the same name but different parameters. The parameter sets of overloaded functions may differ in sequence, number, type.

    • but isn't it possible to do so cout << mo << da << yr << endl; ? What is the meaning of this overload? - Maryna Said
    • these are private class data - the compiler cannot know what is mo or da. Just you yourself declared them closed - AR Hovsepyan
    • It is impossible, because this data is closed. If you make it open, you can write cout << dt.mo << dt.da << dt.yr - this, of course, is shorter and clearer than writing cout << dt ... :) - Harry
    • @Harry and in this overload to get access we declare a friendly function) is it possible to write dozens of lines of code? :) - Maryna Said
    • Why dozens? And then - then, you know, well, what's the point of writing functions at all? After all, the same code can be stuck right into main and not be tormented ... There are all kinds of classes - some kind of Date . What for? Isn't it easier to simply substitute three numbers everywhere, where necessary? ... - Harry

    If the fields of your class are accessible from the outside and you are not too lazy to write everywhere with your hands

     std::cout << d.mo << d.da << d.yr << std::endl; 

    then in terms of convenience and / or reduction of the code length, you really do not need an overloaded << operator for your Date .

    However, the value of such an operator is far from reducing the record length. Moreover, the value of such an operator lies primarily in the fact that it unifies the input-output interface of your class with the interface adopted in the standard library. After redefining the operator << your class becomes compatible with many of the existing formatted output functions of the standard library.

    For example, using the standard std::copy algorithm, you can output an array of your Date objects via std::ostream_iterator like this

     Date date[] = { ... }; std::copy(std::begin(date), std::end(date), std::ostream_iterator<Date>(std::cout, "\n")); 

    but only if the << operator is defined for your Date .

    (I already wrote to you about this.)

       cout может вывести данные класса, если они открытые, и для них уже определен оператор вывода. Например: class Date { public: int mo, da, yr; Date(int m, int d, int y) : mo(m), da(d), yr(y) {} }; 

      or is it the same as

       struct Date { int mo, da, yr; Date(int m, int d, int y) : mo(m), da(d), yr(y) {} }; 

      and you can calmly write

        Date d(1, 1, 2000); cout << d.mo << d.da << d.yr; 
      • so does the point of being overloaded is to bypass privacy? - Maryna Said
      • @Maryna Said, I just told you in which cases you can not overload the operator, but Harry already very well told you about the meaning of overload ... - AR Hovsepyan
      • 2
        @Maryna Said, well, you can not make the compiler understand how the object of your class is displayed. You give a command to the artist: draw me my room. And the artist does not know what you have and what is in her. And when you describe all this, only then can he draw. And how does the compiler know how you want to see your date, in what form, and what does the class contain? Maybe you want to display the date just "ok" or something else .... - AR Hovsepyan
      • one
        Thank you very much for the clarification) more menie becomes clearer) - Maryna Said
      • one
        The phrase “the meaning of overloading is to bypass privacy” sounds like “the meaning of the door key is to easily break the door lock” - AnT