类的成员函数:几个重载函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了类的成员函数:几个重载函数相关的知识,希望对你有一定的参考价值。
(一)输入输出重载
#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); public: CData(int year = 1900, int month = 1, int day = 1)//初始化 : _year(year) ,_month(month) , _day(day) {} void Display() { cout << _year << "-" << _month << "-" << _day << endl; } //返回值operator void operator<<(ostream&_cout) { cout << _year << "-" << _month << "-" << _day << endl; } private: int _year; int _month; int _day; }; ostream& operator<<(ostream&_cout, const 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() { int a = 10; cout << a << endl; CDate d1; //CDate d1(2016, 3, 29);//第二张图结果 d1.operator<<(cout); cout << d1 << endl; cin>>d1; system("pause"); return 0; }
(二)赋值运算符重载
#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); public: CDate(int year = 1900, int month = 1, int day = 1)//初始化 : _year(year) ,_month(month) , _day(day) {} void Display() { cout << _year << "-" << _month << "-" << _day << endl; } CDate& operator=(const CDate&d) { if (this != &d) { _year=d._year; _month=d._month; _day=d._day; } return *this; } //返回值operator void operator<<(ostream&_cout) { cout << _year << "-" << _month << "-" << _day << endl; } public: int _year; int _month; int _day; }; ostream& operator<<(ostream&_cout, const 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; }
三、静态成员函数定义
#include<iostream> using namespace std; #include "Time.h" int count =0; class CDate { friend ostream& operator<<(ostream&_cout, const CDate&d); friend istream& operator>>(istream&_cin, CDate&d); public: CDate(int year = 1900, int month = 1, int day = 1)//初始化 : _year(year) ,_month(month) , _day(day) { count++; cout << "greate cdate" << endl; } CDate(const CDate&d) { cout << "Greate CDate" << endl; } ~CDate() { count--; } void Display() { cout << _year << "-" << _month << "-" << _day << endl; } /*CData& operator=(const CData&d) { if (this != &d) { _year=d._year; _month=d._month; _day=d._day; } return *this; }*/ //返回值operator void operator<<(ostream&_cout) { cout << _year << "-" << _month << "-" << _day << endl; } CDate* operator&() { return this; } static int GetCount() { return count; } public: int _year; int _month; int _day; static int count; }; //静态成员定义 //类型 类类型::静态成员名字 int CDate::count = 0; ostream& operator<<(ostream&_cout, const 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; }
以上是关于类的成员函数:几个重载函数的主要内容,如果未能解决你的问题,请参考以下文章