引言
C++类和对象练习:Date类实现日期的差,比较日期的大小,日期的前置后置++,--,输入输出Date类,对默认函数的练习。
分三个文件来写:
Date.cpp //类函数的实现
Date.h //类函数的声明
Test.cpp //测试类的功能是否正确
一、先在Date.h中声明类里面的函数
#pragma once
#include<assert.h>
#include<stdio.h>
#include<iostream>
using namespace std;
class Date
{
public:
// 友函数的声明,外面的函数要访问类里面的对象,需要使用友函数。
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
int GetMonthDay(int year, int month)
{
assert(month > 0 && month < 13);
int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2 && ((year % 4 == 0 && year % 400 != 0) || year % 400 == 0))
return 29;
return monthDayArray[month];
}
// 只要不改变调用对象的函数都建议加const
void Print() const;
bool CheckDate()const; //检查日期是否合法
Date(int year = 1, int month = 1, int day = 1);
Date& operator+=(int day);
Date operator+(int day) const ;
Date& operator-=(int day);
Date operator-(int day) const;
// ++d1; -> d1.operator++();
Date& operator++();
// d1++; -> d1.operator++(1);
Date& operator++(int);
// --d1;
Date& operator--();
//d1;
Date operator--(int);
bool operator==(const Date& d) const;
bool operator!=(const Date& d) const;
bool operator<(const Date& d) const;
bool operator<=(const Date& d) const;
bool operator>(const Date& d) const;
bool operator>=(const Date& d) const;
// 日期-日期 难
int operator-(const Date& d) const;
private:
int _year;
int _month;
int _day;
};
//这个函数需要声明的类的外面,为了书写代码符合常规逻辑
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
二、实现类里面的函数(在Date.cpp中实现)
1、实现检查日期是否合法,打印日期的函数
bool Date::CheckDate() const
{
if (_month < 1 || _month > 12
|| _day < 1 || _day > GetMonthDay(_year, _month))
{
return false;
}
return true;
}
// void Print(const Date* const this) const
void Date::Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
2、构造函数,拷贝构造函数
下一章介绍实例化构造函数,所以这里先不使用实例化构造
Date::Date(int year, int month, int day) //构造函数
{
_year = year;
_month = month;
_day = day;
if (!CheckDate())
{
cout << "非法日期" << endl;
}
}
Date::Date(const Date& d) // 拷贝构造函数
{
_year = d._year;
_month = d._month;
_day = d._day;
}
3、日期+=day函数,日期+day函数
// +=
Date& Date::operator+=(int day)
{
_day += day;
//进位
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
// +
Date Date::operator+(int day) const
{
Date tmp(*this); //使用的是默认的拷贝构造
tmp += day;
return tmp;
}
4、日期 -= day函数,日期-day函数
Date& Date::operator-=(int day)
{
_day -= day;
while (_day <= 0)
{
--_month;
if (_month == 0)
{
_month = 12;
--_year;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
// 日期-天数
// d1 - 100
Date Date::operator-(int day) const
{
Date tmp(*this);
tmp -= day;
return tmp;
}
5、前置++,后置++,前置--,后置--
//++d1; -> d1.operator++()
Date& Date::operator++()
{
*this += 1;
return *this;
}
// d1++; -> d1.operator++(int)
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
//--d1
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//d1--
Date Date::operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
6、两个日期比较大小
技巧:实现== 和 < 逻辑,其他比较使用这两个逻辑来实现
bool Date::operator==(const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _year == d._year;
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
bool Date::operator<(const Date& d) const
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year && _month < d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day < d._day)
{
return true;
}
return false;
}
// d1 <= d2
bool Date::operator<=(const Date& d) const
{
return (*this < d || *this == d);
}
// d1 > d2
bool Date::operator>(const Date& d) const
{
return !(*this <= d);
}
// d1 >= d2;
bool Date::operator>=(const Date& d) const
{
return !(*this < d);
}
7、日期-日期
// 日期-日期
//d1 - d
int Date::operator-(const Date& d) const
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag; //大的减小的是正数,小的减大的是负数
}
8、实现类的输入和输出
将重载函数写在类里面:
ostream& Date::operator<<(ostream& out)
{
out << this->_year << "年" << this->_month << "月" << this->_day << "日" << endl;
return out;
}
输出的格式:会发现不符合常规逻辑。
原因:
• 重载 << 和 >> 时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第一个形参位置,第一个形参位置是左侧运算对象,调用时就变成了对象 <<cout,不符合使用习惯和可读性。重载为全局函数把ostream/istream放到第一个形参位置就可以了,第二个形参位置当前类类型对象。
所以将重载函数写在类的外面:
因为要访问类的私有成员,所以在类中要使用函数的友元。
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
while (1)
{
cout << "请依次输入年月日:>";
in >> d._year >> d._month >> d._day;
if (d.CheckDate())
{
break;
}
else
{
cout << "非法日期,请重新输入!" << endl;
}
}
return in;
}
三、所有代码:
在Date.h中:
#pragma once
#include<assert.h>
#include<stdio.h>
#include<iostream>
using namespace std;
class Date
{
public:
// 友函数的声明,外面的函数要访问类里面的对象,需要使用友函数。
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
int GetMonthDay(int year, int month) const
{
assert(month > 0 && month < 13);
int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2 && ((year % 4 == 0 && year % 400 != 0) || year % 400 == 0))
return 29;
return monthDayArray[month];
}
// 只要不改变调用对象的函数都建议加const
void Print() const;
bool CheckDate()const; //检查日期是否合法
Date(int year = 1, int month = 1, int day = 1); //构造函数
Date(const Date& d); // 拷贝构造函数
Date& operator+=(int day);
Date operator+(int day) const ;
Date& operator-=(int day);
Date operator-(int day) const;
// ++d1; -> d1.operator++();
Date& operator++();
// d1++; -> d1.operator++(1);
Date operator++(int);
// --d1;
Date& operator--();
//d1;
Date operator--(int);
bool operator==(const Date& d) const;
bool operator!=(const Date& d) const;
bool operator<(const Date& d) const;
bool operator<=(const Date& d) const;
bool operator>(const Date& d) const;
bool operator>=(const Date& d) const;
// 日期-日期
int operator-(const Date& d) const;
private:
int _year;
int _month;
int _day;
};
//这个函数需要声明的类的外面,为了书写代码符合常规逻辑
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);
在Date.cpp中:
#include"Date.h"
bool Date::CheckDate() const
{
if (_month < 1 || _month > 12
|| _day < 1 || _day > GetMonthDay(_year, _month))
{
return false;
}
return true;
}
// void Print(const Date* const this) const
void Date::Print() const
{
cout << _year << "/" << _month << "/" << _day << endl;
}
Date::Date(int year, int month, int day) //构造函数
{
_year = year;
_month = month;
_day = day;
if (!CheckDate())
{
cout << "非法日期" << endl;
}
}
Date::Date(const Date& d) // 拷贝构造函数
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// +=
Date& Date::operator+=(int day)
{
_day += day;
//进位
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
// +
Date Date::operator+(int day) const
{
Date tmp(*this); //使用的是默认的拷贝构造
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;
}
// 日期-天数
// d1 - 100
Date Date::operator-(int day) const
{
Date tmp(*this);
tmp -= day;
return tmp;
}
//++d1; -> d1.operator++()
Date& Date::operator++()
{
*this += 1;
return *this;
}
// d1++; -> d1.operator++(int)
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
//--d1
Date& Date::operator--()
{
*this -= 1;
return *this;
}
//d1--
Date Date::operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
bool Date::operator==(const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _year == d._year;
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
bool Date::operator<(const Date& d) const
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year && _month < d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day < d._day)
{
return true;
}
return false;
}
// d1 <= d2
bool Date::operator<=(const Date& d) const
{
return (*this < d || *this == d);
}
// d1 > d2
bool Date::operator>(const Date& d) const
{
return !(*this <= d);
}
// d1 >= d2;
bool Date::operator>=(const Date& d) const
{
return !(*this < d);
}
// 日期-日期
//d1 - d
int Date::operator-(const Date& d) const
{
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n * flag; //大的减小的是正数,小的减大的是负数
}
//ostream& Date::operator<<(ostream& out)
//{
// out << this->_year << "年" << this->_month << "月" << this->_day << "日" << endl;
// return out;
//}
ostream& operator<<(ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
while (1)
{
cout << "请依次输入年月日:>";
in >> d._year >> d._month >> d._day;
if (d.CheckDate())
{
break;
}
else
{
cout << "非法日期,请重新输入!" << endl;
}
}
return in;
}
在test.c中:
#include"Date.h"
void test1()
{
Date d1(2025,1,1);
d1.Print();
d1 += 10;
d1.Print();
Date d2(2025, 1, 2);
d2 = d1 + 10;
d2.Print();
d1.Print();
}
void test2()
{
Date d1(2025, 1, 1);
Date d2(2025, 1, 1);
d1.Print();
d1 -= 10;
d2 = d1 - 10;
d2.Print();
d1.Print();
}
void test03()
{
Date d1(2025, 1, 1);
Date d2(2025, 1, 10);
++d1;
d1.Print();
d2 = d1++;
d2.Print();
d1.Print();
//d1 << cout;
cin >> d1 >> d2;
cout << d1 << d2;
}
int main()
{
//test1();
//test2();
test03();
return 0;
}