C++第九弹---类与对象(六)

news2024/9/11 9:58:31

个人主页: 熬夜学编程的小林

💗系列专栏: 【C语言详解】 【数据结构详解】【C++详解】

日期类

1、日期类的分析和设计

1.1、日期类的功能说明

1.2、日期类的分析和设计

1.2.1、数据结构的分析

1.2.2、文件结构设计

2、日期类的结构分析

2.1、创建日期类及声明

2.2、在类内实现获取天数函数

2.3、在类外实现构造函数

 2.4、在类外实现拷贝构造函数

2.5、在类外实现赋值运算符重载

2.6、在类外实现日期的比较

2.7、在类外实现日期+-天数

2.8、在类外实现日期-日期

3、日期类分文件的代码实现

3.1、test.cpp

3.2、Date.cpp

3.3、Date.h

总结


1、日期类的分析和设计

1.1、日期类的功能说明

创建一个日期类,通过运算符重载实现对日期的相关计算以及比较功能。包含如下:

1、重载日期的大小比较(>  ==  >=  <  <=  !=)

2、重载赋值操作符

3、重载日期+= / -=天数

4、重载日期+ / -天数

5、重载日期前置++ / --

6、重载日期后置++ / --

7、重载日期 - 日期

1.2、日期类的分析和设计

1.2.1、数据结构的分析

创建一个日期类,成员变量包括年月日(最好将成员变量权限设置为private,这样在类外就无法访问类中成员变量,起到一定的保护作用),内部实现获取某年某月的天数的函数,为了实现分文件设计,类中只需声明构造函数,拷贝构造函数以及日期类需要实现的重载函数

1.2.2、文件结构设计

之前我们在C语言中学习了多文件的形式对函数的声明和定义,这里我们C++实践⼀下,我们设计三个文件:

test.cpp    //文件中日期类的测试逻辑
Date.cpp  //文件中写类中重载函数的实现等
Date.h      //文件中写日期类需要的数据类型和函数声明等


建议:写一些代码就测试一些代码。

2、日期类的结构分析

2.1、创建日期类及声明

注意:在实现操作函数之前判断是否会修改成员变量的值,如果不需要改变成员变量的值,可以使用const修饰来保护代码。

class Date
{
public:
	// 构造函数
	Date(int year, int month, int day);
	// 拷贝构造函数
	// d2(d1)
	Date(const Date& d);
	// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
	// >运算符重载
	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& d);
	// 日期+=天数
	Date& operator+=(int day);
	// 日期+天数
	Date operator+(int day) const;
	// 日期-天数
	Date operator-(int day) const;
	// 日期-=天数
	Date& operator-=(int day);
	// 前置++
	Date& operator++();
	// 后置++
	Date operator++(int);
	// 后置--
	Date operator--(int);
	// 前置--
	Date& operator--();

	// 日期-日期 返回天数
	int operator-(const Date& d) const;
private:
	int _year;
	int _month;
	int _day;
};

2.2、在类内实现获取天数函数

根据我们日期的规则,只有2月的天数是不同的,此时我们可以创建一个数组来存放对应年份月份的天数,如果是平年的2月则为28天,如果是闰年的2月则为29天,因此我们在创建完数组之后判断一下即可。

uu们可能不太了解什么情况是闰年,闰年的规则如下:

1、每4年为一个闰年,每100年为平年。即年份%4==0且年份%100!=0.

2、每400年为一个闰年。即年份%400==0。

代码实现如下:

// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
	int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };//下标表示月份
	if (((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))&&(month == 2) )
		return 29;
	return days[month];
}

uu们可能一开始就写出上面的代码, 那么这个代码有没有问题呢?或者说效率高不高呢?

小林的答案是有问题,效率也没有尽可能进行优化,原因如下:

1、可以将该函数加上const进行修饰,因为后面重载函数的实现中可能会被const修饰,那么如果没有const修饰,可以调用该函数,如果有const修饰的函数就不能调用此函数。但是该函数加上const修饰,不管是否有const进行修饰函数,都可以调用。

2、没有对月份进行判断,我们的月份只有1到12月,但是我们不做判断传入负数或者大于12的月份,因此此处可以用我们C语言所学习的断言语句。

3、创建的数组是在栈区创建的,但是获取天数的函数会经常调用,那么调用一次函数就会创建一个栈空间,效率很低,此处我们可以使用static修饰,将数组放在静态区,那么就只会创建一次。

4、判断是否为闰年时,我们可以做小小的调整,根据C语言学习的短路求值原则,与运算中左边的为假则结束运算,因此我们可以将容易判断的条件放在左边。

经过调整的获取某年某月的天数函数如下:

	// 获取某年某月的天数
	int GetMonthDay(int year, int month) const
	{
		assert(month > 0 && month < 13);//月份的判断
		// 因为该函数会经常调用,但是数组的值一直是不需要变化的,因此可以使用静态数组
		// 好处是在静态区只会创建一份变量
		static int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if ((month == 2) && ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) ))
			return 29;
		return days[month];
	}

注意:该函数实现的年数对负数是不支持的,1582年也可能会出现问题,因为1582年少了10天。 

2.3、在类外实现构造函数

注意:在类外实现类中声明的构造函数需要使用域作用符。

// 构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}

 2.4、在类外实现拷贝构造函数

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

2.5、在类外实现赋值运算符重载

// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;

	return *this;
}

2.6、在类外实现日期的比较

// >运算符重载
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 || *this == d;
}
// <运算符重载
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);
}

注意:此处使用的思想是先实现两个比较操作,然后通过位运算操作符实现其他的操作。

2.7、在类外实现日期+-天数

// 日期+=天数
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) const
//{
//	Date temp(*this);
//	temp += day;
//	return temp;
//}
 
// 日期+天数 ---直接实现
Date Date::operator+(int day) const
{
	Date temp(*this);
	temp._day += day;
	while (temp._day > GetMonthDay(_year, _month))
	{
		temp._day -= GetMonthDay(temp._year, temp._month);
		++temp._month;
		if (temp._month == 13)
		{
			++temp._year;
			temp._month = 1;
		}
	}
	return temp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
// 日期-天数 ---使用前面-=运算符重载实现
Date Date::operator-(int day) const
{
	//Date temp(*this);
	Date temp(*this);
	temp -= day;

	return temp;
}

 日期-天数 ---直接实现
//Date Date::operator-(int day) const
//{
//	//Date temp(*this);
//	Date temp(*this);
//	temp._day -= day;
//	while (temp._day <= 0)
//	{
//		--temp._month;
//		if (temp._month == 0)
//		{
//			--temp._year;
//			temp._month = 12;
//		}
//		temp._day += GetMonthDay(temp._year, temp._month);
//	}
//	return temp;
//}

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

2.8、在类外实现日期-日期

思想:计算第一个类与第二个类的差值,我们在前面已经实现了++和比较操作,此处只需要先判断哪个日期类大,小的日期类++多少次等于大的日期类就是相差的天数。

// 日期-日期 返回天数
int Date::operator-(const Date& d) const
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		flag = -1;
		max = d;
		min = *this;
	}
	int n = 0;
	while (min != max)
	{
		min++;
		n++;
	}
	return n * flag;
}

3、日期类分文件的代码实现

3.1、test.cpp

#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"

int main()
{
	Date d1(2022, 1, 1);
	d1 += 1;

	Date d2(2022, 1, 2);
	d2 = d2 + 10;

	Date d3(2022, 1, 3);
	d3 -= 2;
	
	Date d4(2022, 1, 4);
	d4 = d4 - 10;

	Date d5(2022, 1, 5);
	Date d6 = d5--;

	//Date d6 = d5.operator--(10);  //后置--

	Date d7(2022, 1, 18);
	Date d8(2022, 1, 8);

	cout << (d7 - d8) << endl;

	return 0;
}

3.2、Date.cpp

#define _CRT_SECURE_NO_WARNINGS
#include "Date.h"


// 构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
// 拷贝构造函数
// d2(d1)
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)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;

	return *this;
}

// >运算符重载
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 || *this == d;
}
// <运算符重载
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);
}


// 日期+=天数
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) const
//{
//	Date temp(*this);
//	temp += day;
//	return temp;
//}
 
// 日期+天数 ---直接实现
Date Date::operator+(int day) const
{
	Date temp(*this);
	temp._day += day;
	while (temp._day > GetMonthDay(_year, _month))
	{
		temp._day -= GetMonthDay(temp._year, temp._month);
		++temp._month;
		if (temp._month == 13)
		{
			++temp._year;
			temp._month = 1;
		}
	}
	return temp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
// 日期-天数 ---使用前面-=运算符重载实现
Date Date::operator-(int day) const
{
	//Date temp(*this);
	Date temp(*this);
	temp -= day;

	return temp;
}

 日期-天数 ---直接实现
//Date Date::operator-(int day) const
//{
//	//Date temp(*this);
//	Date temp(*this);
//	temp._day -= day;
//	while (temp._day <= 0)
//	{
//		--temp._month;
//		if (temp._month == 0)
//		{
//			--temp._year;
//			temp._month = 12;
//		}
//		temp._day += GetMonthDay(temp._year, temp._month);
//	}
//	return temp;
//}

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



// 日期-日期 返回天数
int Date::operator-(const Date& d) const
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		flag = -1;
		max = d;
		min = *this;
	}
	int n = 0;
	while (min != max)
	{
		min++;
		n++;
	}
	return n * flag;
}

3.3、Date.h

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month) const
	{
		assert(month > 0 && month < 13);
		// 因为该函数会经常调用,但是数组的值一直是不需要变化的,因此可以使用静态数组
		// 好处是在静态区只会创建一份变量
		static int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if ((month == 2) && ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)))
			return 29;
		return days[month];
	}
	// 构造函数
	Date(int year, int month, int day);
	// 拷贝构造函数
	// d2(d1)
	Date(const Date& d);
	// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
		// >运算符重载
	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& d);
	// 日期+=天数
	Date& operator+=(int day);
	// 日期+天数
	Date operator+(int day) const;
	// 日期-天数
	Date operator-(int day) const;
	// 日期-=天数
	Date& operator-=(int day);
	// 前置++
	Date& operator++();
	// 后置++
	Date operator++(int);
	// 后置--
	Date operator--(int);
	// 前置--
	Date& operator--();

	// 日期-日期 返回天数
	int operator-(const Date& d) const;
private:
	int _year;
	int _month;
	int _day;
};

总结


本篇博客就结束啦,谢谢大家的观看,如果公主少年们有好的建议可以留言喔,谢谢大家啦!

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

深度学习 精选笔记(13.1)卷积神经网络-LeNet模型

学习参考&#xff1a; 动手学深度学习2.0Deep-Learning-with-TensorFlow-bookpytorchlightning ①如有冒犯、请联系侵删。 ②已写完的笔记文章会不定时一直修订修改(删、改、增)&#xff0c;以达到集多方教程的精华于一文的目的。 ③非常推荐上面&#xff08;学习参考&#x…

HCIA复习

上面的文件里有思维导图哦~ 一、情景再现&#xff1a;ISP网络为学校提供了DNS服务&#xff0c;所以&#xff0c;DNS服务器驻留在ISP网络内&#xff0c;而不再学校网络内。DHCP服务器运行在学校网络的路由器上。 小明拿了一台电脑&#xff0c;通过网线&#xff0c;接入到校园网…

使用el-cascader组件写下拉级联多选并且具有全选功能

样式 说明: 级联选择器中加上全选的按钮, 并且保证数据响应式。 思路 因为是有全选的功能,所以不能直接使用el-cascader组件, 而是选择使用el-select组件, 在此组件内部使用el-cascader-panel级联面板全选按钮也是写在el-select组件中, 并且去监听全选按钮的状态, 根…

The Open Group开放数字标准组合|管理您的数字景观

据麻省理工学院斯隆Sloan和凯捷咨询Capgemini称&#xff0c;90%的首席执行官认为数字经济将影响他们的行业&#xff0c;但只有不到15%的首席执行官正在执行数字战略。 数字化转型对于企业在当今不断变化的市场和技术环境中持续保持竞争力至关重要。近年来&#xff0c;商业世界发…

‍Java OCR技术全面解析:六大解决方案比较

博主猫头虎的技术世界 &#x1f31f; 欢迎来到猫头虎的博客 — 探索技术的无限可能&#xff01; 专栏链接&#xff1a; &#x1f517; 精选专栏&#xff1a; 《面试题大全》 — 面试准备的宝典&#xff01;《IDEA开发秘籍》 — 提升你的IDEA技能&#xff01;《100天精通鸿蒙》 …

HarmonyOS ArkTS 基础组件

目录 一、常用组件 二、文本显示&#xff08;Text/Span) 2.1 创建文本 2.2 属性 2.3 添加子组件(Span) 2.4 添加事件 三、按钮&#xff08;Button&#xff09; 3.1 创建按钮 3.2 设置按钮类型 3.3 悬浮按钮 四、文本输入&#xff08;TextInput/TextArea&#xff09;…

vue-生成二维码

安装 yarn add qrcodejs2 --save npm install qrcodejs2 --save 使用 <template><div><div id"qrcodeImg"></div><!-- 创建一个div&#xff0c;并设置id --></div> </template> <script> import QRCode from q…

vue3中如何实现多个侦听器(watch)

<body> <div id"app"><input type"button" value"更改名字" click"change"> </div> <script src"vue.js"></script> <script>new Vue({el: #app,data: {food: {id: 1,name: 冰激…

C语言学习 三、运算符与表达式

3.1 运算符分类 c语言提供了13种类型的运算符&#xff0c;如下所示&#xff1a; &#xff08;1&#xff09;算术运算符&#xff08; - * / %&#xff09; &#xff08;2&#xff09;关系运算符&#xff08;> < > < !&#xff09; &#xff08;3&#xff09;逻…

Python 解析CSV文件 使用Matplotlib绘图

数据存储在CSV文件中&#xff0c;使用Matplotlib实现数据可视化。 CSV文件&#xff1a;comma-separated values&#xff0c;是在文件中存储一系列以‘&#xff0c;’分隔的值。 例如&#xff1a;"0.0","2016-01-03","1","3","20…

【Pt】新建项目时的设置

新建项目时需要在如下界面做一些设置。 一、模板与文件 模板通常选择“PBR - Metallic Roughness Alpha-blend” 文件可以选择fbx&#xff0c;abc&#xff0c;obj等格式的三维模型文件 二、项目设置 2.1 文件分辨率 指的是在软件中的预览效果&#xff0c;分辨率越高预览效果…

链动3+1模式 全新升级 解决小号和断代问题!!!

随着数字经济的蓬勃发展&#xff0c;市场竞争愈演愈烈&#xff0c;传统的商业模式显得捉襟见肘&#xff0c;难以满足企业快速发展的需求。在这种背景下&#xff0c;一种创新的商业模式——“链动31”应运而生&#xff0c;以其独特的玩法和优势&#xff0c;引领着市场发展的新方…

HEVC的编码结构

编码单元划分 CTU/CTB CTU(Coding Tree Unit)和CU组成了一个四叉树的层级结构,CTU的尺寸为64 x 64,32 x 32,16 x 16,一个CTU可以分为一个或四个CTU,对标H264的MB。 CU/CB CU/CB(Coding Unit/Coding Block),CU为亮度和色度编码单元的统称,CB特指某一个分量的的编码…

网络编程:数据库

一、作业 1> 创建一个工人信息库&#xff0c;包含工号&#xff08;主键&#xff09;、姓名、年龄、薪资。 2> 添加三条工人信息&#xff08;可以完整信息&#xff0c;也可以非完整信息&#xff09; 3> 修改某一个工人的薪资&#xff08;确定的一个&#xff09; 4> …

帅帅密码管理系统使用教程

在这个账号满天飞的大环境&#xff0c;密码太多&#xff0c;又容易遗忘&#xff0c;又不方便管理&#xff0c;存在记事本上&#xff0c;又担心泄漏。帅帅密码管理系统就是帮助你解决以上烦恼&#xff0c;用来帮助个人或团队管理众多的登陆密码&#xff0c;能够快速的查询、新增…

布隆过滤器原理介绍和典型应用案例

整理自己过去使用布隆过滤器的应用案例和理解 基本介绍 1970年由布隆提出的一种空间效率很高的概率型数据结构&#xff0c;它可以用于检索一个元素是否在一个集合中&#xff0c;由只存0或1的位数组和多个hash算法, 进行判断数据 【一定不存在或者可能存在的算法】 如果这些…

广告创意的魔力:争议与诱惑并存

《广告创意的魔力&#xff1a;争议与诱惑并存》 一、广告的创意魅力之源 广告创意是广告行业的灵魂&#xff0c;它决定着广告能否在浩瀚的信息海洋中脱颖而出&#xff0c;抓住受众的注意力。优秀的广告创意往往能够打破常规&#xff0c;用新颖、独特、有趣的手法表达广告主题…

D55125ADA A型漏电保护芯片,适用于 110V⁓220V(50/60Hz)电压,可应用于 新能源充电桩(充电枪)、智能空开(智能微断开关)等工业产品

一、应用领域 新能源充电桩&#xff08;充电枪&#xff09;、智能空开&#xff08;智能微断开关&#xff09;等工业产品&#xff0c;以及电热水器、电烤箱、电烤炉等小家电产品。 二、功能介绍 D55125ADA 是一款高性能 CMOS 漏电保护器专用电路。芯片内部包含稳压电源、放大电路…

C是用什么语言写出来的?

C是用什么语言写出来的? C语言的起源和发展是一个迭代过程&#xff1a; 1. 最初的C语言编译器的开发始于对B语言的改进。B语言是由Ken Thompson设计的&#xff0c;它是基于BCPL语言简化而来的。在开始前我有一些资料&#xff0c;是我根据网友给的问题精心整理了一份「 C语言的…

文件IO(代码案例: 文件复制, 指定目录查找文件, 指定目录查找内容)

文件复制 进行普通文件的复制 使用操作字节流的对象操作文件 // 文件复制 public class Main {public static void main(String[] args) throws IOException {// 输入两个路径, 源路径, 目的路径Scanner scanner new Scanner(System.in);System.out.println("请输入拷贝文…