Monday, 8 May 2023

Sample Programs overload ostream istream operator « Operator Overloading « C++

 Today i will share one of many programs that i found. Following the examples program :

// 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; }


Source : http://www.java2s.com/Tutorial/Cpp/0200__Operator-Overloading/friendostreamoperatorforprivatefields.htm

No comments:

Post a Comment