利用运算符重载实现Date类
2016-03-04 11:07
169 查看
运算符重载:
重载,就是重新赋予新的含义。运算符重载的方法是定义一个重载运算符的函数,使指定的运算符不仅能实现原有的,而且能实现在函数中指定的新的功能。在使用被重载的运算符时,系统会自动调用该函数,以实现相应的功能。即运算符重载实质是函数的重载。
重载运算符的格式如下:
函数类型 operator 运算符名称(形参表)
{
对运算符的重载处理
}
c++中绝大多数的运算符容许重载,不能重载的运算符只有5个:即
. (成员访问运算符)
* (成员指针访问运算符)
:: (域运算符)
sizeof (长度运算符)
?: (条件运算符)
例:创建一个Date类,实现日期+天数=日期,日期-天数=日期及日期-日期=天数。
重载,就是重新赋予新的含义。运算符重载的方法是定义一个重载运算符的函数,使指定的运算符不仅能实现原有的,而且能实现在函数中指定的新的功能。在使用被重载的运算符时,系统会自动调用该函数,以实现相应的功能。即运算符重载实质是函数的重载。
重载运算符的格式如下:
函数类型 operator 运算符名称(形参表)
{
对运算符的重载处理
}
c++中绝大多数的运算符容许重载,不能重载的运算符只有5个:即
. (成员访问运算符)
* (成员指针访问运算符)
:: (域运算符)
sizeof (长度运算符)
?: (条件运算符)
例:创建一个Date类,实现日期+天数=日期,日期-天数=日期及日期-日期=天数。
#define _CRT_SECURE_NO_WARNING 1 #include<iostream> using namespace std; class Date { public: friend ostream& operator<<(ostream&os, Date&d); Date(int year = 1900, int month = 1, int day = 1) :_year(year) , _month(month) , _day(day) { if (IsRightDay()) { _year = 1900; _month = 1; _day = 1; } } Date(const Date &d) { _year = d._year; _month = d._month; _day = d._day; } Date &operator=(const Date &d) { if (this != &d) //防止自引用 { _year = d._year; _month = d._month; _day = d._day; } return*this; } Date operator+(int day) { Date d(*this); d._day += day; while(d._day > DayInMonth()) { d._day -= DayInMonth(); if (d._month == 12) { d._month = 1; d._year += 1; } else d._month += 1; } return d; } Date &operator+=(int day) { _day += day; while (_day > DayInMonth()) { _day -= DayInMonth(); if (_month == 12) { _month = 1; _year += 1; } else _month += 1; } return *this; } Date operator-(int day) { Date d(*this); d._day -= day; while (d._day <= 0) { if (d._month == 1) { d._month = 12; d._year -= 1; } else d._month -= 1; d._day += DayInMonth(); } return d; } bool operator>(const Date& d) { if(_year > d._year) return true; if(_year == d._year) { if(_month > d._month) return true; if(_month == d._month) { if(_day > d._day) { return true; } } } return false; } bool operator==(const Date& d) { if((_year == d._year) &&(_month == d._month) &&(_day == d._day)) { return true; } return false; } bool operator>=(const Date& d) { return ((*this > d) || (*this == d)); } bool operator!=(const Date& d) { return !(*this == d); } int operator-(Date&d) { int days=0; Date d1(d); Date d2(*this); if (d1 > d2) { d1 = (*this); d2 = d; } while (d1 != d2) { days++; d1 += 1; } return days; } int DayInMonth() { int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (((_year % 4 == 0) && (_year % 100 != 0) || (_year & 400 == 0))) { days[2] = 29; } return days[_month]; } bool IsRightDay() { if (_year < 1900) { return true; } if (_month<1 || _month>12) { return true; } if (_day<1 || _day>DayInMonth()) { return true; } return false; } private: int _year; int _month; int _day; }; ostream& operator<<(ostream&os, Date&d) { os << d._year << "-" << d._month << "-" << d._day << endl; return os; } int main() { Date d1(1994, 5, 25); Date d2 = d1 + 31; Date d3(2016, 3, 14); Date d4 = d3 - 31; cout << d1 << endl; cout << d2 << endl; cout << d4 << endl; cout << d3 - d1 << endl; system("pause"); return 0; }
相关文章推荐
- Lua中关系运算符的使用教程
- Lua学习笔记之运算符和表达式
- C#运算符重载用法实例分析
- C#中重载与重写区别分析
- C#中多态、重载、重写区别分析
- sql server 2008中的apply运算符使用方法
- C#中38个常用运算符的优先级的划分和理解
- 讲解C++编程中Address-of运算符&的作用及用法
- 详解C++中的成员访问运算符和指针到成员运算符
- C++中的按位与&、按位与或|、按位异或^运算符详解
- 深入解析C++编程中__alignof 与__uuidof运算符的使用
- 深入解析C++编程中范围解析运算符的作用及使用
- C#基础语法:as 运算符使用实例
- 与MSSQL对比学习MYSQL的心得(五)--运算符
- MySQL中的运算符使用实例展示
- 轻松学习C#的运算符
- Ruby的运算符和语句优先级介绍
- C#编程自学之运算符和表达式
- Lua中的运算符简明总结
- python运算符