member functions for classes: several overloaded functions

Source: Internet
Author: User


(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;&LT;&LT;&NBSP;_DAY&NBSP;&LT;&LT;&NBSP;ENDL;} return value operatorvoid operator<< (ostream&_cout) {cout << _year <<  "- " << _month << "-"&NBSP;&LT;&LT;&NBSP;_DAY&NBSP;&LT;&LT;&NBSP;ENDL;} private:int _year;int _month;int _day;};  ostream& operator<< (ostream&_cout, const cdate&d) {_COUT&NBSP;&Lt;< 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;&LT;&LT;&NBSP;_DAY&NBSP;&LT;&LT;&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;&LT;&LT;&NBSP;_DAY&NBSP;&LT;&LT;&NBSP;ENDL;} public:int _year;int _month;int _day; }; ostream& operator<< (Ostream&_cout, const &NBSP;CDATE&AMP;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&AMP;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;&LT;&LT;&NBSP;ENDL;} ~cdate () {count--;}  void display () {cout << _year <<  "-"  << _month  <<  "-" &NBSP;&LT;&LT;&NBSP;_DAY&NBSP;&LT;&LT;&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;&LT;&LT;&NBSP;_DAY&NBSP;&LT;&LT;&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 &AMP;_COUT,&NBSP;CONST&NBSP;CDATE&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.