赞
踩
必须有的:年、月、日
Date类的成员类型都是内置类型,编译器不一定处理
我们可以显示的写一个全缺省的构造函数
- Date::Date(int year, int month, int day)
- {
- _year = year;
- _month = month;
- _day = day;
- }
成员都是内置类型,那么也就是没有资源需要清理也就不需要显示的写析构函数
需要显示的写吗?
不需要,编译器会自动调用默认的拷贝构造
如果要显示的写拷贝构造函数该怎么实现?
- Date::Date(const Date& d)
- {
- _year = d._year;
- _month = d._month;
- _day = d._day;
- }
拷贝构造函数名就是类名,只有一个参数,形参是类类型的引用
- int Date::GetMonthDay(int year, int month)
- {
- static int MonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
- if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
- {
- return 29;
- }
- else
- {
- return MonthDay[month];
- }
- }
将数组的长度设置为13,这样数组的下标刚好就与月份能对应得上
别忘了,闰年的二月有29天
编译器有默认的,可以不用显示写
那显示的如何实现?要注意什么?
- Date& Date::operator=(const Date& d)
- {
- //检查自赋值
- if (this == &d)
- {
- return *this;
- }
- _year = d._year;
- _month = d._month;
- _day = d._day;
- return *this;
- }
返回值类型:类类型的引用
形参:只有一个,形参类型也是类类型的引用
函数体中返回的是*this,this指针指向当前对象(当前日期),存放当前对象的地址,所以要返回当前对象就是返回this指针的解引用
想看详细一点的可以阅读:http://t.csdnimg.cn/ZE13g
里面一个核心的思想就是复用。
- friend ostream& operator<<(ostream& os, Date& d)
- {
- os << d._year << "/" << d._month << "/" << d._day << endl;
- return os;
- }
- friend istream& operator>>(istream& is, Date& d)
- {
- is >> d._year >> d._month >> d._day;
- return is;
- }
需要注意的是他们不是日期类的成员函数,而是日期类的友元函数,返回值类型前面加了关键字friend来修饰,注意友元函数虽然可以访问类的所有成员,但是由于他本身不是类的成员函数也就没有了隐藏的this指针,所以需要传两个参数.
- #pragma once
- #include<iostream>
- using namespace std;
- class Date
- {
- public:
- Date(int year = 0, int month = 0, int day = 0);
- // 拷贝构造函数
- // d2(d1)
- Date(const Date& d);
-
- // 获取某年某月的天数
- int GetMonthDay(int year, int month);
-
- // 赋值运算符重载
- // d2 = d3 -> d2.operator=(&d2, d3)
- Date& operator=(const Date& d);
-
- // 日期+=天数
- Date& operator+=(int day);
-
- // 日期+天数
- Date operator+(int day);
-
- // 日期-天数
- Date operator-(int day);
-
- // 日期-=天数
- Date& operator-=(int day);
-
- // 前置++
- Date& operator++();
-
- // 后置++
- Date operator++(int);
-
- // 后置--
- Date operator--(int);
-
- // 前置--
- Date& operator--();
-
- // >运算符重载
- bool operator>(const Date& d);
-
- // ==运算符重载
- bool operator==(const Date& d);
-
- // >=运算符重载
- bool operator >= (const Date& d);
-
- // <运算符重载
- bool operator < (const Date& d);
-
- // <=运算符重载
- bool operator <= (const Date& d);
-
- // !=运算符重载
- bool operator != (const Date& d);
-
- // 日期-日期 返回天数
- int operator-(const Date& d);
-
- void print()const;
- // 析构函数(日期类无需清理资源,析构函数不必显示写)
- //~Date()
- //{
- //cout << "~Date()" << endl;
- //}
- friend ostream& operator<<(ostream& os, Date& d)
- {
- os << d._year << "/" << d._month << "/" << d._day << endl;
- return os;
- }
- friend istream& operator>>(istream& is, Date& d)
- {
- is >> d._year >> d._month >> d._day;
- return is;
- }
- private:
- int _year, _month, _day;
- };
-

- #include"Date.h"
- Date::Date(int year, int month, int day)
- {
- _year = year;
- _month = month;
- _day = day;
- }
- Date::Date(const Date& d)
- {
- _year = d._year;
- _month = d._month;
- _day = d._day;
- }
- // 赋值运算符重载
- // d2 = d3 -> d2.operator=(&d2, d3)
- Date& Date::operator=(const Date& d)
- {
- //检查自赋值
- if (this == &d)
- {
- return *this;
- }
- _year = d._year;
- _month = d._month;
- _day = d._day;
- return *this;
- }
- int Date::GetMonthDay(int year, int month)
- {
- static int MonthDay[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
- if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
- {
- return 29;
- }
- else
- {
- return MonthDay[month];
- }
- }
- void Date::print()const
- {
- cout << _year << "年" << _month << "月" << _day << "日" << endl;
- }
- // >运算符重载
- bool Date::operator>(const Date& d)
- {
- if (_year > d._year)
- {
- return true;
- }
- else if (_year == d._year)
- {
- if (_month > d._month)
- {
- return true;
- }
- else if (_month == d._month)
- {
- if (_day > d._day)
- {
- return true;
- }
- }
- }
- return false;
- }
-
- // ==运算符重载
- bool Date::operator==(const Date& d)
- {
- return _year == d._year && _month == d._month && _day == d._day;
- }
-
- // >=运算符重载
- bool Date::operator >= (const Date& d)
- {
- return (*this > d || *this == d);
- }
-
- // <运算符重载
- bool Date::operator < (const Date& d)
- {
- return !(*this >= d);
- }
-
- // <=运算符重载
- bool Date::operator <= (const Date& d)
- {
- return !(*this > d);
- }
-
- // !=运算符重载
- bool Date::operator != (const Date& d)
- {
- return !(*this == d);
- }
- // 日期+=天数
- Date& Date::operator+=(int day)
- {
- _day += day;
- while (_day > GetMonthDay(_year, _month))
- {
- _month++;
- if (_month == 13)
- {
- _year++;
- _month = 1;
- }
- _day -= GetMonthDay(_year, _month);
- }
- return *this;
- }
-
- // 日期+天数
- Date Date::operator+(int day)
- {
- Date tmp;
- tmp += day;
- return tmp;
- }
-
- // 日期-天数
- Date Date::operator-(int day)
- {
- Date tmp;
- tmp -= day;
- return tmp;
- }
-
- // 日期-=天数
- Date& Date::operator-=(int day)
- {
- _day -= day;
- while (_day <= 0)
- {
- _month--;
- if (_month == 0)
- {
- _month = 12;
- _year--;
- }
- _day += GetMonthDay(_year, _month);
- }
- return *this;
- }
-
- // 前置++
- Date& Date::operator++()
- {
- *this += 1;
- return *this;
- }
-
- // 后置++
- Date Date::operator++(int)
- {
- Date tmp(*this);
- *this += 1;
- return tmp;
- }
-
- // 后置--
- Date Date::operator--(int)
- {
- Date tmp(*this);
- *this -= 1;
- return tmp;
- }
-
- // 前置--
- Date& Date::operator--()
- {
- *this -= 1;
- return *this;
- }
- // 日期-日期 返回天数
- int Date::operator-(const Date& d)
- {
- Date max(*this);
- Date min(d);
- int flag = 1;
- int count = 0;
- if (*this < d)
- {
- flag = -1;
- max = d;
- min = *this;
- }
- while (min < max)
- {
- min++;
- count++;
- }
- return count * flag;
- }
-

- #include"Date.h"
- void Test1()
- {
- Date d1(2023, 10, 5), d2(2024, 6, 10);
- cout << (d1 == d2) << endl;
- cout << (d1 != d2) << endl;
- cout << (d1 <= d2) << endl;
- cout << (d1 >= d2) << endl;
- cout << (d1 < d2) << endl;
- cout << (d1 > d2) << endl;
- }
- void Test2()
- {
- Date d1(2024, 6, 10), d2(2024, 6, 05);
- /*Date d3 = d1--;
- Date d4 = d2++;
- d3.print();
- d4.print();
- Date d5 = --d3;
- Date d6 = ++d4;
- d5.print();
- d6.print();*/
- int num = d1 - d2;
- cout << num << endl;
- cout << d1;
- cin >> d2;
- }
- int main()
- {
- Test2();
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。