C++——类和对象练习(日期类)

日期类

  • 1. 构造函数和析构函数
  • 2. 拷贝构造和赋值运算符重载
  • 3. 运算符重载
    • 3.1 日期的比较
    • 3.2 日期加减天数
    • 3.3 日期减日期
    • 3.4 流插入和流提取
  • 4. 取地址和const取地址重载
  • 5. 完整代码
    • Date.h
    • Date.c

对日期类进行一个完善,可以帮助我们理解六个默认成员函数,这里选择全部显式实现,包括构造函数,运算符重载,赋值重载等。对有之前的知识点的提示,以及注意事项,这里采用声明和定义分离的形式,下面代码放的全部是定义,除非必要说明。在最后会有完整的代码。包括头文件和源文件内的。

1. 构造函数和析构函数

//构造函数,初始化列表
Date::Date(int year = 2024, int month = 1, int day = 1)
	:_year(year)
	,_month(month)
	,_day(day)
{}

//析构函数
Date::~Date() {}

2. 拷贝构造和赋值运算符重载

//拷贝构造函数
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

//赋值运算符重载
Date& Date::operator=(const Date& d)
{
    //可能出现d=d的情况,即自己给自己赋值,可以不用执行代码
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

3. 运算符重载

3.1 日期的比较

比较包括大于,等于,小于,大于或等于,小于或等于,不等于六种。

不需要全部都实现,复用代码即可。我们先写一个大于,日期的比较按照年月日依次比较,年大日期就大,年等的情况,月大日期就大,年等月等的情况,天大日期就大。剩下的所有情况就是不大于。

//运算符重载  大于
bool Date::operator>(const Date& d)const
{
	//年大日期就大
	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)const
{
	return _year == d._year &&
		_month == d._month &&
		_day == d._day;
}

剩下的四种比较全部复用这两个函数即可。
小于就是不大于或不等于。
小于等于就是不大于
大于等于就是不小于
不等于就是等于取反

这种方法适用于所有需要重载这六种运算符的类,只需要写一个大于和等于或小于和等于,剩下的都复用代码即可。

//小于
bool Date::operator<(const Date& d)const
{
	return !(*this >= d);
}

//小于等于
bool Date::operator<=(const Date& d)const
{
	return !(*this > d);
}

//大于等于
bool Date::operator>=(const Date& d)const
{
	return (*this == d) || (*this > d);
}

//不等于
bool Date::operator!=(const Date& d)const
{
	return !(*this == d);
}

3.2 日期加减天数

加包括 + 和 +=,减包括 - 和 -=。前者不会修改左操作数,后者会修改左操作数,比如

a=10;
a+5 //不会修改a
a+=5 //会修改a

减同理。

这里先实现 += ,我们需要判断闰年,因为闰年2月有29天,因此,这里封装一个函数,用来获得某年某月的最大天数。

//内联函数声明定义不能分离,在调用处直接展开,不用建立栈帧
inline int GetMonthDay(int year, int month) //获得某年某月的天数
{
	int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
	{
		++MonthDay[2];
	}
	return MonthDay[month];
}

由于这个函数会频繁调用,因此将这个函数声明为内联函数,由于声明定义不能分离,所有这段代码是在头文件内。

有了这个函数后,在实现+=就比较容易了,我们不需要判断是不是闰年,是不是二月了,全部交给这个函数就可以了。

思路:把天全部加上,判断是否超过该月最大天数。有则进行修改,在判断是否超过最大月份,有则进行修改。循环,直到天数小于该月的最大天数。

那么+=代码如下

//日期加等天数(会改变*this)
Date& Date::operator+=(int day)
{
    //+=一个负数,相当于-=一个正数
	if (day < 0)
	{
		*this -= -day;
	}
	else
	{
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month == 13)
			{
				_year++;
				_month = 1;
			}
		}
	}
	return *this;
}

+的实现,可以复用+=的代码

如下

//日期加天数(不会改变*this)
Date Date::operator+(int day) const
{
	//加不改变*this,需要创建局部变量
	Date retDate = *this;//拷贝构造
	retDate += day;
	//出作用域局部变量销毁,不能传引用返回
	return retDate;
}

因为+不能改变*this,所以先拷贝一个对象,修改这个对象的内容,在返回。

-= 和 - 的重载很类似,就不赘述了,直接放码

//日期减天数
Date Date::operator-(int day) const
{
	Date retDate = *this;
	retDate -= day;
	return retDate;
}

Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		*this += -day;
	}
	else
	{
		_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 retDate(*this);
	*this += 1;
	return retDate;
}

//前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
//后置--
Date Date::operator--(int)
{
	Date retDate(*this);
	*this -= 1;
	return retDate;
}

3.3 日期减日期

日期的相加没有意义,但是相减还是比较有意义的,比如计算一下我们从出生到现在生活了多久,和对象已经在一起多久等。

有两种方法:
法一:
在这里插入图片描述
法二:
先比较日期,在对小日期进行自增,直到与大日期相等。

这里选择法二

//日期减日期
int Date::operator-(const Date& d) const
{
	//找出日期大的和日期小的
	Date maxDate = *this > d ? *this : d;
	Date minDate = *this < d ? *this : d;
	int flag = 1;
	int day = 0;
	//第一个日期较小,结果是负数。
	if (minDate == *this)
	{
		flag = -1;
	}
	while (minDate != maxDate)
	{
		++minDate;
		++day;
	}
	return day * flag;
}

3.4 流插入和流提取

对于内置类型,可以使用<<和>>来打印或者从键盘输入,自定义类型也可以重载一个流插入和流提取。

// >>和<<需要重载成全局函数,因为成员函数的第一个参数默认是this,重载成成员函数会打破 cout<<d 的习惯

//流插入重载
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

//流提取重载
istream& operator>>(istream& in,Date& d)
{
	cin >> d._year >> d._month >> d._day;
	return in;
}

istream和ostream是标准库内的一种输入输出类型,代码内的cou和in就是cout和cin,只不过是别名,在函数内部可以自己定义输出格式,甚至检查错误等。返回值是为了支持连续的输入和输出。

注意:在类外想使用类内部的私有成员,需要在类内部声明为友元函数。

4. 取地址和const取地址重载

这两个函数较为简单,就不过的讲解了。

//const取地址重载
const Date* Date::operator&()const
{
	return this;
}
//取地址重载
Date* Date::operator&()
{
	return this;
}

5. 完整代码

Date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
	//友元函数声明,友元函数可以在类外使用类内的私有成员
	
	friend ostream& operator<<(ostream& out,const Date& d);
	friend istream& operator>>(istream& out,Date& d);

public:
	//内联函数声明定义不能分离,在调用处直接展开,不用建立栈帧
	inline int GetMonthDay(int year, int month) //获得某年某月的天数
	{
		int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		{
			++MonthDay[2];
		}
		return MonthDay[month];
	}

	//构造函数特征:
	//              1、程序员不显示实现的话,编译器会自动生成默认构造函数
	//              2、函数名和类名相同,没有返回值(也不能写void,这是规定)
	//              3、在类对象实例化的过程中编译器对会自动调用对应的构造函数
	//              4、无参的构造函数,全缺省的构造函数,编译器自动生成的构造函数都可以称为默认构造函数,
	//                 并且默认构造函数只能存在一个,虽然编译时没有问题,但在调用时会有歧义
	//              5、构造函数支持函数重载
	//              6、对内置数据类型如int,char,double等,构造函数不做处理,
	//                 对自定义数据类型,编译器会调用对应的默认构造函数
	Date(int year = 2024, int month = 1, int day = 1);


	//析构函数特征:
	//              1、函数名为类名但需要在类名前加 ~ 符号,没有参数,没有返回值(也不能写void,这是规定)
	//              2、我们不显示实现的话,编译器会自动生成对应的析构函数
	//              3、在类对象生命周期结束后,编译器自动调用对应的析构函数
	//              4、析构函数不能重载,因为析构函数没有参数
	//              5、一个类只允许有一个析构函数
	~Date() {};


	//拷贝构造函数特征:
	//                  1、拷贝构造函数是构造函数的重载函数,所以函数名和类名相同
	//                  2、有且只有一个参数,该参数必须是类对象的引用,如果不是引用,编译器会直接报错
	//                  3、若没有显示实现,编译器会自动实现一个默认的拷贝构造函数,对已有的类对象进行拷贝
	//                     但是是按对应的字节序列完成拷贝,这种拷贝方式称为浅拷贝。
	//                  4、编译器生成的默认拷贝构造函数可以完成浅拷贝

	Date(const Date& d);


	//日期的比较,末尾const是限制*this的,此时this的类型为const Date* const this
	//不能修改this指针,也不能修改this指针指向的内容

	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;

	Date& operator=(const Date& d2);
	Date operator+(int day)const;
	Date& operator+=(int day);

	Date operator-(int day) const;
	Date& operator-=(int day);

	Date& operator++();
	Date operator++(int);

	Date& operator--();
	Date operator--(int);

	int operator-(const Date& d) const;

	const Date* operator&()const;

	Date* operator&();

private:
	int _year;
	int _month;
	int _day;
};

Date.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"

//构造函数,初始化列表
Date::Date(int year = 2024, int month = 1, int day = 1)
	:_year(year)
	,_month(month)
	,_day(day)
{}

//析构函数
Date::~Date() {}


//拷贝构造函数
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

//运算符重载  大于
bool Date::operator>(const Date& d)const
{
	//年大日期就大
	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)const
{
	return _year == d._year &&
		_month == d._month &&
		_day == d._day;
}

//小于
bool Date::operator<(const Date& d)const
{
	return !(*this >= d);
}

//小于等于
bool Date::operator<=(const Date& d)const
{
	return !(*this > d);
}

//大于等于
bool Date::operator>=(const Date& d)const
{
	return (*this == d) || (*this > d);
}

//不等于
bool Date::operator!=(const Date& d)const
{
	return !(*this == d);
}


//Date Date::operator+(int day) const
//{
//	Date retDate = *this;
//	retDate._day += day;
//	while (retDate._day > GetMonthDay(retDate._year, retDate._month))
//	{
//		retDate._day -= GetMonthDay(retDate._year, retDate._month);
//		retDate._month++;
//		if (retDate._month == 13)
//		{
//			retDate._year++;
//			retDate._month = 1;
//		}
//	}
//	
//	return retDate;
//}
//Date& Date::operator+=(int day)
//{
//	*this = *this + day;//赋值
//	return *this;
//}

//日期加天数(不会改变*this)
Date Date::operator+(int day) const
{
	//加不改变*this,需要创建局部变量
	Date retDate = *this;//拷贝构造
	retDate += day;
	//出作用域局部变量销毁,不能传引用返回
	return retDate;
}

//日期加等天数(会改变*this)
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		*this -= -day;
	}
	else
	{
		_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) const
{
	Date retDate = *this;
	retDate -= day;
	return retDate;
}
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		*this += -day;
	}
	else
	{
		_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 retDate(*this);
	*this += 1;
	return retDate;
}

//前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
//后置--
Date Date::operator--(int)
{
	Date retDate(*this);
	*this -= 1;
	return retDate;
}

//日期减日期
int Date::operator-(const Date& d) const
{
	//找出日期大的和日期小的
	Date maxDate = *this > d ? *this : d;
	Date minDate = *this < d ? *this : d;
	int flag = 1;
	int day = 0;
	//小日期减大日期为负数
	if (minDate == *this)
	{
		flag = -1;
	}
	while (minDate != maxDate)
	{
		++minDate;
		++day;
	}
	return day * flag;
}

// >>和<<需要重载成全局函数,因为成员函数的第一个参数默认是this,重载成成员函数会打破 cout<<d 的习惯
//流插入重载
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

//流提取重载
istream& operator>>(istream& in,Date& d)
{
	cin >> d._year >> d._month >> d._day;
	return in;
}

//赋值运算符重载
Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	return *this;
}

const Date* Date::operator&()const
{
	return this;
}

Date* Date::operator&()
{
	return this;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/566929.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

图搜索算法详解:广度优先搜索与深度优先搜索的探索之旅

图搜索算法详解&#xff1a;广度优先搜索与深度优先搜索的探索之旅 1. 广度优先搜索&#xff08;BFS&#xff09;1.1 伪代码1.2 C语言实现 2. 深度优先搜索&#xff08;DFS&#xff09;2.1 伪代码2.2 C语言实现 3. 总结 图搜索算法是计算机科学中用于在图结构中查找路径的算法。…

手撕红黑树(map和set底层结构)(2)

[TOC]红黑树 一 红黑树概念 红黑树&#xff0c;是一种二叉搜索树&#xff0c;但在每个结点上增加一个存储位表示结点的颜色&#xff0c;可以是Red或Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制&#xff0c;红黑树确保没有一条路径会比其他路径长出俩倍&…

54-摄像头DVP接口电路设计

视频链接 摄像头电路设计-DVP接口01_哔哩哔哩_bilibili 摄像头DVP接口电路设计 1、摄像头简介 1.1、Camera介绍 在各类信息中&#xff0c;图像含有最丰富的信息&#xff0c;作为机器视觉领域的核心部件&#xff0c;摄像头被广泛应用。 目前市面上最常用的摄像头为OV5640。…

【面试必备】Python 快问快答

什么是 Python&#xff0c;它有哪些主要特点 答&#xff1a;Python 是一种高级解释型编程语言&#xff0c;以简单易读著称。其主要特点包括动态类型、自动内存管理&#xff08;垃圾回收&#xff09;、丰富的标准库以及对多种编程范式&#xff08;过程式、面向对象、函数式&…

内容营销ROI提升秘籍:Kompas.ai的高效内容分析

在内容营销的领域中&#xff0c;投资回报率&#xff08;ROI&#xff09;是衡量营销活动成效的关键指标。一个高ROI的内容营销策略不仅能够为企业带来直接的经济收益&#xff0c;还能够提升品牌价值和市场影响力。本文将深入探讨内容营销中ROI的重要性&#xff0c;介绍Kompas.ai…

【嵌入式】Arduino IDE + ESP32开发环境配置

一 背景说明 最近想捣鼓一下ESP32的集成芯片&#xff0c;比较了一下&#xff0c;选择Arduino IDE并添加ESP32支持库的方式来开发&#xff0c;下面记录一下安装过程以及安装过程中遇到的坑。 二 下载准备 【1】Arduino IDE ESP32支持一键安装包&#xff08;非常推荐&#xff0…

canvas 学习

最近的项目涉及到 canvas 相关的知识&#xff0c;就在网站上找资源先大概了解一下&#xff0c;然后再细细研究。 看到了一篇 “canvas详细教程” 的资源&#xff0c;感觉十分不错&#xff0c;就分享给大家&#xff1a; canvas详细教程! ( 近1万字吐血总结)这期是潘潘整理的万…

C++入门(3)

文章目录 C入门auto同一行中定义多个变量auto不能推到的场景基于范围的for循环(C11)10. 指针空值nullptr(C11) C入门 auto auto&#xff1a;C11中&#xff0c;标准委员会赋予了auto全新的含义即&#xff1a;auto不再是一个存储类型指示符&#xff0c;而是作为一个新的类型指示…

基于ontape的备份与恢复实验

通过本文的备份恢复实验&#xff0c;我们可以深入了解ontape的使用方法和原理&#xff0c;包括如何进行完整备份、增量备份以及如何利用备份文件进行数据恢复。 1. 配置onconfig参数 通过修改onconfig参数文件&#xff0c;或使用onmode -wf命令&#xff0c;设置备份默认使用的…

pmp好考么?知道这些PMP一个多月拿下~

过来人想说的是PMP考试没那么难&#xff0c;学习也没那么复杂的&#xff0c;只要能好好学基础知识&#xff0c;积累实践案例&#xff0c;接着多做做题摸清题意套路&#xff0c;考试的时候就跟考科一似的。 尽管听上去描述的可能过于简单&#xff0c;但事实便是如是&#xff0c…

容器云平台运维的范围与架构设计新思潮

容器云平台运维的范围与架构设计 【导读】容器云平台有其独特的特点&#xff0c;不同于传统系统的运维。本文分享了作者对容器云平台运维范围和运维架构设计的思考与实践。 一、容器云平台运维范围 &#xff08;一&#xff09; 梳理要运维哪些内容 作为运维专家&#xff0c;…

被删除的照片和视频能找回吗?如何恢复手机删除的照片和视频?

手机里的照片和视频是我们记录生活的每一个瞬间&#xff0c;也是工作学习等场合经常用到的东西&#xff0c;一旦不慎丢失&#xff0c;将对我们造成很大损失。那么我们该如何恢复手机删除的照片和视频呢&#xff1f;通过掌握正确的恢复方法&#xff0c;能够最大程度地保护手机中…

【Leetcode笔记】501.二叉搜索树中的众数

文章目录 题目要求ACM 模式代码知识点 题目要求 给你一个含重复值的二叉搜索树&#xff08;BST&#xff09;的根节点 root &#xff0c;找出并返回 BST 中的所有 众数&#xff08;即&#xff0c;出现频率最高的元素&#xff09;。 如果树中有不止一个众数&#xff0c;可以按 …

PDF文件去除文字水印

文章目录 0、背景1、准备工作2、查看是否是文字水印3、批量去除水印 0、背景 本文主题为去除PDF文件中的水印。源文件来自这里。防止丢失&#xff0c;我在这里做个记录&#xff0c;感谢原作者的付出&#xff0c;也欢迎大家关注原作者。 1、准备工作 下载Adobe Acrobat DC软件…

GUI测试首推!TestComplete 帮助有效缩短 40-50% 测试时长!

TestComplete 是一款自动化UI测试工具&#xff0c;这款工具目前在全球范围内被广泛应用于进行桌面、移动和Web应用的自动化测试。 TestComplete 集成了一种精心设计的自动化引擎&#xff0c;可以自动记录和回放用户的操作&#xff0c;方便用户进行UI&#xff08;用户界面&…

C++ 面向对象-封装

C 是一种多范式编程语言&#xff0c;它支持面向对象编程&#xff08;OOP&#xff09;范式。面向对象编程是一种程序设计思想&#xff0c;其中程序由对象组成&#xff0c;每个对象都是一个实例&#xff0c;具有数据和相关操作。在C中&#xff0c;实现面向对象编程主要通过类和对…

Vue3、 Vue2 Diff算法比较

Vue2 Diff算法 源码位置:src/core/vdom/patch.ts 源码所在函数:updateChildren() 源码讲解: 有新旧两个节点数组:oldCh和newCh; 有下面几个变量: oldStartIdx 初始值=0 oldStartVnode 初始值=oldCh[0] oldEndIdx 初始值=oldCh.length - 1 oldEndVnode 初始值=oldCh[ol…

如何在PostgreSQL中设置定期任务(如定时备份、数据分析等),并使用pgAgent或其他方式实现

文章目录 使用pgAgent实现定期任务步骤一&#xff1a;安装pgAgent步骤二&#xff1a;配置pgAgent步骤三&#xff1a;创建和调度任务示例代码&#xff1a; 使用操作系统的任务调度功能实现定期任务步骤一&#xff1a;编写脚本步骤二&#xff1a;设置cron任务示例代码&#xff1a…

ssh日志的独立与ssh远程日志

日志相关介绍&#xff1a; 1.系统日志&#xff1a;是记录了历史事件&#xff1a;包括时间地点人物事件等。日志级别&#xff1a;事件的关键性程度&#xff0c;Loglevel。 级号消息级别说明0EMERG紧急会导致主机系统不可用的的情况1ALERT警告必须马上采取措施解决的问题2CRIT严…

vue3实现全局事件总线

1、vue3中使用全局事件总线是变化最大的。在vue2中&#xff0c;我们在new Vue中在beforeCreate钩子函数中使用vue.prototype.$busthis来创建全局事件总线。vue3中我需要借助第三方库来完成创建全局事件总线。 2、安装依赖 npm i mitt -s3、封装event-bus.js文件 import mitt …