C++初阶--类与对象--const成员和日期类的实现

这篇具有很好参考价值的文章主要介绍了C++初阶--类与对象--const成员和日期类的实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

const 成员

将const修饰的成员函数称之为const成员函数。
在一个成员函数里面,对于this指针指向的对象,是隐藏式的,没有办法用常规的方法去修饰它,所以我们是这样进行修饰的:
C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言

注意事项:
C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言

对一个日期类的实现

源码

date.h

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
private:
	int _year;
	int _month;
	int _day;
public:
	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;

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

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

	//日期-日期
	int 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;
	void Print()const;
	int GetMonthDay()const;


};

date.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"

Date::Date(int year,int month,int day)
{
	_year = year;
	_month = month;
	_day = day;

	if (_year<1 || _month < 1 || _month>12 || _day>GetMonthDay())
	{
		Print();
		cout << "日期非法" << endl;

	}
}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

void Date::Print()const
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
int Date::GetMonthDay()const
{
	assert(_year >= 1 && _month >= 1 && _month<=12);
	int day=0;
	day+= _day;
	int Array[] = { 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;
	}
	return Array[_month];
}

//d1+=50;
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= (-day);
	}
	_day += day;
	while (_day > GetMonthDay())
	{
		_day -= GetMonthDay();
		_month++;
		if (_month > 12)
		{
			_month = 1;
			_year++;
		}
	}
	return (*this);
}
//d2=d1+100
Date Date::operator+(int day)const
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}

//d1-=100
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += (-day);
	}

	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_month = 12;
			_year--;
		}
		_day += GetMonthDay();
	}
	return (*this);
}
//d1=d2-100
//日期-天数
Date Date::operator-(int day)const
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}

//++d1
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
//d1++
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
{
	if (_year < d._year)
	{
		return false;
	}
	if (_year == d._year && _month < d._month)
	{
		return false;
	}
	if (_year == d._year && _month == d._month && _day < d._day)
	{
		return false;
	}
	return true;
}
bool Date::operator==(const Date& d)const
{
	if (_year == d._year && _month == d._month && _day == d._day)
	{
		return true;
	}
	return false;
}
bool Date::operator>=(const Date& d)const
{
	return *this > d || *this == d;
}

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

bool Date::operator<=(const Date& d)const
{
	return *this < d || *this == d;
}

int Date::operator-(const Date& d)const
{
	int day = 0;
	Date min = *this;
	Date max = d;
	if (*this>d)
	{
		min = d;
		max = *this;
	}
	while (min < max)
	{
		min++;
		day++;
	}
	return day;

}

解析

这里采用多文件编程的方式,所以在date.cpp中,是在Date类外使用的,需要加上作用域限定符;
对于不改变类对象,也就是this指针指向的对象的,尽量都加上const进行修饰,有一定程度提高效率;
在date.h中,不管成员变量还是成员函数,在类中都只是起到声明的作用,类的成员变量的定义将会在对象中进行定义,类的成员函数都是在date.cpp定义的

构造函数的验证

C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言

在我们定义时,我们已经加上了缺省值了,
C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言
所以在定义时就不用加上缺省值了,加上反而非产生冲突,使编译器无法辨别要哪个缺省值,产生矛盾;

在定义时我们加上了对日期是否合法进行了判断,但没有强制性退出,只是打印警告;

我们将给出三组数据进行验证:
C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言

运算符的重复利用

对于重复的步骤,我们可以运用另一个函数来进行完成:
C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言
而这里也是有一定讲究的,对于参数和返回值来说,都会创建一个临时变量对象来拷贝传参的值,而这就表示着要调用拷贝构造函数,所以我们在写程序时,要尽量的少调用函数,提高一定的效率

上述程序中,调用函数次数:
C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言
如果我们反过来写的话:
C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言

注意:对于+=来说,会改变this指针指向对象的值,所以这里不能使用const来进行修饰;
而+ 到最后会返回一个值给对应的对象,这个对象不是this指针所指向的对象的,所以可以使用const进行修饰;

C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言
我们在date.cpp中没有写明=的重载,类会生成一个默认的运算符重载;
C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言

前置++与后置++

在C++中,前置++的重载形式与后置++的重载形式是相同的,为了辨别这两种重载形式,后置++重载时在参数中多加一个int类型的参数,但调用函数时该参数是不参与传递的。

C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言

前置++就是直接+=1;而后置++会等到语句结束后才加上1,所以先找一个中间值tmp来进行代替,表示还没有+1;

其他运算符的验证

C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言
C++初阶--类与对象--const成员和日期类的实现,C++初阶,c++,开发语言文章来源地址https://www.toymoban.com/news/detail-743495.html

到了这里,关于C++初阶--类与对象--const成员和日期类的实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包