Date类的相关操作:
#include<iostream>
#include<windows.h>
using namespace std;
//时间类
class Time
{
public:
Time(int hour = 0, int minute = 0, int second = 0)
{
cout << "Time(int,int,int):" << endl;
_hour = hour;
_minute = minute;
_second = second;
}
private:
int _hour;
int _minute;
int _second;
};
//日期类
class Date
{
public:
void SetDate(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void PrintDate()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
//类中如果没有显示(用户是否直接定义)任何构造函数,编译器将生成一个默认的无参构造函数
//带参构造函数
/*
Date(int year,int month,int day)
{
_year = year;
_month = month;
_day = day;
cout << "Date(int,int,int):" << this << endl;
}
//无参构造函数
//与带参构造函数形成重载
Date()
{
_year = 1900;
_month = 1;
_day = 1;
}
*/
//全缺省构造函数
//全缺省构造函数和无参构造函数均为默认的构造函数,并且编译器只生成其中一个
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//析构函数(既没有返回值也没有参数)----〉完成类的一些清理工作
~Date()
{
}
//拷贝构造函数
//和构造函数形成重载
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//自己编写函数比较日期大小
bool Isgreater(const Date& d)
{
if (_year > d._year ||
_year == d._year && _month > d._month ||
_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
return false;
}
//运算符重载
//只需要传单参,因为还有一个隐藏的this
Date& operator=(const Date& d)
{
if (this != &d)//防止自己给自己赋值
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
Date operator+(int days)const//给当前日期加上days后的日期
{
Date dd(*this);
dd._day = 12;
for (int i = 0; i < days; i++)
dd++;
return dd;
}
Date operator-(int days)
{
Date dd(*this);
for (int i = 0; i < days; i++)
dd--;
return dd;
}
int operator-(const Date& d)//两个日期之间差多少天
{
int count = 0;
Date dd(*this);
if (dd<d)
{
while (dd < d)
{
count--;
dd++;
}
return count;
}
else if (dd > d)
{
while (dd > d)
{
count++;
dd--;
}
return count;
}
else
return 0;
}
Date& operator++()//前置++
{
_day += 1;
return *this;
}
Date operator++(int)//后置++
{
Date temp(*this);
_day += 1;
return temp;
}
Date& operator--()//前置--
{
_day -= 1;
return *this;
}
Date operator--(int)//后置--
{
Date temp(*this);
_day -= 1;
return temp;
}
Date* operator&()//&运算符重载
{
return this;
}
bool operator>(const Date& d)const
{
if (_year > d._year ||
_year == d._year && _month > d._month ||
_year == d._year && _month == d._month && _day > d._day)
{
return true;
}
return false;
}
bool operator>=(const Date& d)const
{
if (*this > d || *this == d)
{
return true;
}
return false;
}
bool operator<(const Date& d)const
{
if (!(_year > d._year ||
_year == d._year && _month > d._month ||
_year == d._year && _month == d._month && _day > d._day))
{
return true;
}
return false;
}
bool operator<=(const Date& d)const
{
if (*this < d || *this == d)
{
return true;
}
return false;
}
bool operator==(const Date& d)const
{
return _year == d._year && _month == d._month && _day == d._day;
}
bool operator!=(const Date& d)const
{
return !(*this == d);
}
private:
int _year;
int _month;
int _day;
//Time _t;
};
void TestDate()
{
//Date d2(2019, 9, 19);
//d2.PrintDate();
Date d1(2019, 9, 19);
Date d2(d1);
}
int main()
{
//Date d1;
//d1.SetDate(2019,9,19);
//d1.PrintDate();
//为什么编译通过但结果不正确
//因为编译器生成了默认的构造函数,并且结果为随机值
//Date d2(2019, 9, 19);
//d2.PrintDate();
//TestDate();
//Date d1(2019, 9, 19);
//Date d2(d1);
//Date d1(2019, 9, 19);
//Date d2(2019, 9, 20);
//Date d3(2019, 9, 21);
//if (d1.Isgreater(d2))
//if (d1>=d2)
// cout << "d1 >= d2" << endl;
//else
// cout << "d1 <= d2" << endl;
//d1 = d2;
//d3 = d2 = d1;
/*
d2 = d3++;//d3加1之前的旧值给d2赋值
d2.operator=(d3.operator++());
d2 = --d3;//d3加1之后的结果给d2赋值
d2.operator=(d3.operator--(0));
*/
Date d1(2019, 9, 19);
cout << &d1 << endl;
const Date d2(2019, 9, 20);
cout << &d2 << endl;
system("pause");
return 0;
}