【C++】日期类的实现 (class Date)
创始人
2024-05-09 11:25:20
0

文章目录

  • 前言
  • 1. Date中构造函数
  • 2. Date中析构函数
  • 3. Date中拷贝构造函数
  • 4. Date中运算符重载
    • 4.1 返回Date类的变量,使用传值返回(如:+)还是传引用返回(如:+=)?
    • 4.2 通过复用写过的运算符,提高效率
    • 4.3 区分前置++(--)和后置++(++)
    • 4.4 友元函数
  • 5. Date类的实现


前言

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

1. Date中构造函数

Date类中构造函数的功能是对变量进行初始化,若不写,系统会默认生成。但对于Date类而言,系统默认生成的构造函数初始化的年月日是随机值。为使得d1在初始化时就有一个确定的值,因此自定义一个全缺省的构造函数。这样,每个创建出来的日期类变量都有一个初始值1900,1,1。

Date(int year = 1900, int month = 1, int day = 1);
// 全缺省构造函数
Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;//检查日期是否合法if (!(year >= 1&& (month >= 1 && month <= 12)&& (day >= 1 && day <= GetMonthDay(year, month)))){cout << "非法日期" << endl;}
}

2. Date中析构函数

析构函数是销毁某些特定的成员变量,销毁的意义是取消指定变量地址的使用权。对于malloc出的内存,若不通过析构函数指定操作,则很可能造成内存泄漏。对于Date类中的成员,生长在栈帧上,会随着函数栈帧结束自动释放掉,因此使用默认析构函数即可。

3. Date中拷贝构造函数

创建对象时使用同类对象初识化,拷贝构造函数其实是一个构造函数的重载,依次拷贝类成员进行初始化。若不写,系统也会默认生成。

4. Date中运算符重载

4.1 返回Date类的变量,使用传值返回(如:+)还是传引用返回(如:+=)?

传值返回:返回过程中生成一个临时变量,并且会调用一次构造函数,开销较大
传引用返回:若要返回的值出了当前函数作用域还在(如:静态变量 static 或者传值传来的变量),只要生命周期大于函数的周期,便可使用传引用返回。另外还需考虑这个变量会不会改变,如d1+1,d1不会改变,需要返回的值只可以是类里面创建的对象,只能传值返回;如d1+=1,d1改变,即可通过传引用返回。

4.2 通过复用写过的运算符,提高效率

若写好了 ’ > ‘,则可通过‘ !< ’实现‘ <= ’逻辑。这样代码效率更高,需要修改和调整的部分也更少,只需检查实现’ > '逻辑的函数即可。

4.3 区分前置++(–)和后置++(++)

两者实现的逻辑相似,区别是后置操作会传一个(int)形参,作用只用于区分前置和后置。

4.4 友元函数

C++中,可以重载输入输出流操作符<<与>>来自定义其输入输出功能。
但若在流对象之前使用const,编译器会报错输入流对象 没有匹配的 常量类型 输出。
这是由于:const 是用来修饰不会改变的对象或变量状态的常量修饰符。而此处不能在重载的流对象之前加 const 说明Date类的友元函数输出流对象之后改变了流对象本身的状态。
另外,为实现cout<流向,需要传入两个形参,不然方向会搞反。

5. Date类的实现

Date.h

class Date{// 友元声明(类的任意位置)friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public:// 获取某年某月的天数int GetMonthDay(int year, int month);// 全缺省的构造函数Date(int year = 1900, int month = 1, int day = 1);// 拷贝构造函数// d2(d1)// 形参加const,防止写反Date(const Date& d);// 析构函数~Date(){}// 打印void Print();// 赋值运算符重载// 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);private:int _year;int _month;int _day;};// cout << d1 => operator<<(cout, d1);
inline ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}// cin >> d1   operator(cin, d1);
inline istream& operator>>(istream& in, Date& d)
{in >> d._year >> d._month >> d._day;return in;
}

Date.cpp

// 获得某年某月某日
int Date::GetMonthDay(int year, int month)
{static int monthDayArray[13] = { 0,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 monthDayArray[month];}
}// 全缺省构造函数
Date::Date(int year, int month, int day)
{_year = year;_month = month;_day = day;//检查日期是否合法if (!(year >= 1&& (month >= 1 && month <= 12)&& (day >= 1 && day <= GetMonthDay(year, month)))){cout << "非法日期" << endl;}
}// 拷贝构造函数
// 形参加const,防止写反
Date::Date(const Date & d)
{/*cout << "Data 拷贝构造" << endl;*/_year = d._year;_month = d._month;_day = d._day;}// 打印
void Date::Print()
{cout << _year << "/" << _month << "/" << _day << endl;
}// 赋值运算符重载// d2 = d3 -> d2.operator=(&d2, d3)Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}// 日期+=天数Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){++_year;_month = 1;}}return *this;
}// 日期+天数Date Date::operator+(int day)
{Date ret(*this);ret += day;return ret;
}// 日期-天数Date Date::operator-(int day)
{Date ret(*this);ret -= day;return ret;}// 日期-=天数Date& Date::operator-=(int day)
{if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}// 前置++
Date& Date::operator++()
{*this += 1;return *this;
}// 后置++ -- 多一个int参数主要是为了区分前置
// 构成函数重载
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;
}// >运算符重载
// d1>d2
bool Date::operator>(const Date& d)
{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;
}// ==运算符重载
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);
}// 日期-日期 返回天数
// d1-d2
int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++n;++min;}return n * flag;
}

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...