C++primer5th十四章_几个类的运算符重载
2016-11-15 22:20
274 查看
class Cdate { friend bool operator>(const Cdate &d1, const Cdate &d2); friend bool operator<(const Cdate &d1, const Cdate &d2); friend bool operator== (const Cdate &d1, const Cdate &d2); friend bool operator!= (const Cdate &d1, const Cdate &d2); friend Cdate operator+(const Cdate&, const Cdate&); friend ostream& operator << (ostream&, const Cdate&); friend istream& operator >> (istream& in, Cdate &d); public: Cdate() = default; Cdate(int y, int m, int d) :year(y), month(m), day(d) {} Cdate operator+= (const Cdate&); Cdate& operator= (const string&); Cdate& operator++ () { year++; month++; day++; return *this; } Cdate operator++(int) { Cdate dat = *this; ++*this; return dat; } private: int year = 0, month = 0, day = 0; }; Cdate& Cdate::operator= (const string &s) { char spc1, spc2; istringstream in(s); in >> year >> spc1 >> month >> spc2 >> day; cout << "year is:" << year << " month is:" << month << " day is:" << day << endl; if (!in< 4000 /span> || spc1 != '-' || spc2 != '-') throw invalid_argument("bad date"); if (month > 12 || month < 1 || day < 1 || day>31) throw invalid_argument("bad date"); return *this; } Cdate operator +(const Cdate &d1, const Cdate &d2) { Cdate d = d1; d += d2; return d; } Cdate Cdate::operator+= (const Cdate &d) { year += d.year; month += d.month; day += d.day; return *this; } ostream& operator << (ostream& out, const Cdate &d) { out << d.year << " 年 " << d.month << " 月 " << d.day << " 日"; return out; } istream& operator >> (istream& in, Cdate &d) { in >> d.year >> d.month >> d.day; if (!in) d = Cdate(); return in; } bool operator== (const Cdate &d1, const Cdate &d2) { return d1.day == d2.day && d1.month == d2.month && d1.year == d2.year; } bool operator!= (const Cdate &d1, const Cdate &d2) { return !(d1 == d2); } bool operator<(const Cdate &d1, const Cdate &d2) { return((d1.year < d2.year) || (d1.year == d2.year&& d1.month < d2.month) || (d1.year == d2.year&&d1.month == d2.month &&d1.day < d2.day)); } bool operator>(const Cdate &d1, const Cdate &d2) { return !(d1 < d2) && d1 != d2; } int main() { Cdate d1, d2; string s1, s2; while (true) { cout << "请输入d1" << endl; cin >> d1; cout << "请输入d2" << endl; cin >> d2; cout << d1 << " " << d2 << endl; cout << "++d1:" << ++d1 << " ++d2:" << ++d2 << endl; cout << "d1++:" << d1++ << " d2++:" << d2++ << endl; cout << d1 << " " << d2 << endl; } return 0; } class PrintString { public: PrintString(ostream &o = cout, char s = ' ') :os(o), sep(s) {} void operator() (const string &str) const { os << str << sep; } private: char sep; ostream &os; }; class IfElseThen { public: IfElseThen() {}; IfElseThen(int i1, int i2, int i3) :iv1(i1), iv2(i2), iv3(i3) {} int operator() (int i1, int i2, int i3) { return i1 ? i2 : i3; } private: int iv1, iv2, iv3; }; class ReadString { public: ReadString(istream &i = cin) :in(i) {} string operator() () { string s; if (!getline(in, s)) s = ""; return s; } private: istream ∈ }; class IntCompare { public: IntCompare(int v) :val(v) {}; bool operator() (int v) { return val == v; }; private: int val; }; class IsString { public: IsString(size_t t, size_t t2) :sz1(t), sz2(t2) {} bool operator() (const string &str) { return str.size() > sz1 && str.size() < sz2; } private: size_t sz1; size_t sz2; }; int main() { IsString is(1, 9); vector<string> vi; ifstream in("data.txt"); if (!in) return -1; string line; while (getline(in, line)) { string word; istringstream iss(line); while (iss >> word) vi.push_back(word); } for (size_t i = 1; i < 11; i++) { cout << "长度为" << i << "的单词数量为:" << count_if(vi.begin(), vi.end(), is) << endl; } }
相关文章推荐
- C++第5周任务【任务2】本任务将设计一个简单的分数类,完成对分数的几个运算。一则巩固基于对象编程的方法,二则也为第10章做运算符重载等积累些感性认识。
- C++Primer Plus 第十一章-运算符重载和友元函数
- 推荐几个网上的C/C++资源
- 几个 C++ 的数据结构类
- C++ primer plus 8章3题
- 王老师 C++ 运算符重载 转换函数 第三讲
- C++中几个比较不常用的关键字
- (摘)C++ Primer Plus, Fourth Edition---Using a Dynamic Array
- C++Primer Dialy
- [学习笔记][C++Primer Plus]String类的使用
- 王老师 C++ 运算符重载 转换函数 第二讲
- C++ Primer 学习笔记关于练习题6.12
- [C++学习]矩阵运算符重载
- C++ primer plus 5章9题
- C++ primer plus 5th 学习日记
- c++中的运算符重载
- 浅谈C++中运算符重载
- C++中几个比较不常用的关键字
- C++ Primer notes: Chapter 2. Variables and Basic Types
- 查找错误的几个调试方法&一些C/C++的文章&一些零碎知识