(i) input and output overloads
#include <iostream> using namespace std; #include "Time.h" class cdate {friend ostream& operator<< (ostream&_cout, const cdate&d);friend Istream& operator>> (istream&_cin, cdate&d);p Ublic:cdata (int year = 1900,&NBSP;INT&NBSP;MONTH&NBSP;=&NBSP;1,&NBSP;INT&NBSP;DAY&NBSP;=&NBSP;1)//Initialize: _year (year), _month (month) , _day (day) {} void display () {cout << _year << "-" << _month << "-" &NBSP;<<&NBSP;_DAY&NBSP;<<&NBSP;ENDL;} return value operatorvoid operator<< (ostream&_cout) {cout << _year << "- " << _month << "-"&NBSP;<<&NBSP;_DAY&NBSP;<<&NBSP;ENDL;} private:int _year;int _month;int _day;}; ostream& operator<< (ostream&_cout, const cdate&d) {_COUT&NBSP;≪< d._year << "-" << d._month << "-" << d._day << endl;return _cout;} istream& operator>> (istream&_cin, cdate&d) {_cin >> d._year >> d._month >> d._day;return _cin;} Int main () { int a = 10;cout << a << endl; CDATE&NBSP;D1;//CDATE&NBSP;D1 (2016, 3, 29);///second graph result d1.operator<< (cout);cout << d1 << endl;cin>>d1; system ("pause"); return 0;}
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7E/66/wKioL1b-NMXQi0ncAAAMOzjO1hQ985.png "style=" float: none; "title=" Image 1.png "alt=" Wkiol1b-nmxqi0ncaaamozjo1hq985.png "/>
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/6A/wKiom1b-NCGQacAYAAAUQ8mBc9o477.png "style=" float: none; "title=" Image 2.png "alt=" Wkiom1b-ncgqacayaaauq8mbc9o477.png "/>
(ii) assignment operator overloading
#include <iostream> using namespace std; #include "Time.h" class cdate{ friend ostream& operator<< (ostream&_cout, const cdate&d);friend Istream& operator>> (istream&_cin, cdate&d);p ublic:cdate (int year = 1900,&NBSP;INT&NBSP;MONTH&NBSP;=&NBSP;1,&NBSP;INT&NBSP;DAY&NBSP;=&NBSP;1)//Initialize: _year (year), _month (month) , _day (day) {}void display () {cout << _year << "-" << _month << "-" &NBSP;<<&NBSP;_DAY&NBSP;<<&NBSP;ENDL;} Cdate& operator= (const cdate&d) {if (this != &d) {_year=d._year;_month=d._ Month;_day=d._day;} Return *this;} return value operatorvoid operator<< (ostream&_cout) {cout << _year << "- " << _month << "-"&NBSP;<<&NBSP;_DAY&NBSP;<<&NBSP;ENDL;} public:int _year;int _month;int _day; }; ostream& operator<< (Ostream&_cout, const &NBSP;CDATE&D) {_cout << d._year << "-" << d._month << "-" << d._day << endl;return _cout;} istream& operator>> (istream&_cin, cdate&d) {_cin >> d._year >> d._month >> d._day;return _cin;} Int main () { cdate d1 (2016, 3, 28); cdate d2;d2 = d1;cout << d1 << endl;cout << &d2 << endl; system ("pause"); return 0;}
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7E/6A/wKiom1b-NCHDc4A0AAAFMJuMAXw019.png "style=" float: none; "title=" Image 3.png "alt=" Wkiom1b-nchdc4a0aaafmjumaxw019.png "/>
Third, static member function definition
#include <iostream> using namespace std; #include "Time.h" int count =0; class CDate{friend ostream& operator<< (ostream&_cout, CONST&NBSP;CDATE&D);friend istream& operator>> (istream&_cin, cdate&d); Public:cdate (int year = 1900, int month = 1, int day = 1)//Initialize: _year (year), _month (month), _day (day) {count++;cout << "Greate cdate" << endl;} CDate (const cdate&d) {cout << "Greate cdate" &NBSP;<<&NBSP;ENDL;} ~cdate () {count--;} void display () {cout << _year << "-" << _month << "-" &NBSP;<<&NBSP;_DAY&NBSP;<<&NBSP;ENDL;} /*cdata& operator= (const cdata&d) {if (this != &d) {_year=d._year;_month=d. _month;_day=d._day;} Return&nbSp;*this;} *///return value operatorvoid operator<< (ostream&_cout) {cout << _year << "-" << _month << "-" &NBSP;<<&NBSP;_DAY&NBSP;<<&NBSP;ENDL;} cdate* operator& () {return this;} Static int getcount () {return count;} public:int _year;int _month;int _day; static int count;};/ /static Member definition//Type class type:: Static member name int cdate::count = 0;ostream& operator<< (ostream &_COUT,&NBSP;CONST&NBSP;CDATE&D) {_cout << d._year << "-" << d._month << "-" << d._day << endl;return _cout;} istream& operator>> (istream&_cin, cdate&d) {_cin >> d._year >> d._month >> d._day;return _cin;} Int main () { cdate d1 (2016, 3, 28); Cdate d2;d2 = d1; Cdate d4 (d1); const cdate d3;cout << &d3 << endl;cout << &d2 << endl; cout << d1. GetCount () << endl;cout << d2. GetCount () << endl;cout << d3. GetCount () << endl;cout << d4. GetCount () << endl;system ("pause"); return 0;}
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7E/66/wKioL1b-N4HjEtnZAAAbCrGfpOU431.png "title=" Picture 5.png "alt=" Wkiol1b-n4hjetnzaaabcrgfpou431.png "/>
member functions for classes: several overloaded functions