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

const 成员

将const修饰的成员函数称之为const成员函数。
在一个成员函数里面,对于this指针指向的对象,是隐藏式的,没有办法用常规的方法去修饰它,所以我们是这样进行修饰的:
在这里插入图片描述

注意事项:
在这里插入图片描述

对一个日期类的实现

源码

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定义的

构造函数的验证

在这里插入图片描述

在我们定义时,我们已经加上了缺省值了,
在这里插入图片描述
所以在定义时就不用加上缺省值了,加上反而非产生冲突,使编译器无法辨别要哪个缺省值,产生矛盾;

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

我们将给出三组数据进行验证:
在这里插入图片描述

运算符的重复利用

对于重复的步骤,我们可以运用另一个函数来进行完成:
在这里插入图片描述
而这里也是有一定讲究的,对于参数和返回值来说,都会创建一个临时变量对象来拷贝传参的值,而这就表示着要调用拷贝构造函数,所以我们在写程序时,要尽量的少调用函数,提高一定的效率

上述程序中,调用函数次数:
在这里插入图片描述
如果我们反过来写的话:
!](https://img-blog.csdnimg.cn/eac3af7c12554ba0b67aef2d8c27db9a.png)

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

在这里插入图片描述
我们在date.cpp中没有写明=的重载,类会生成一个默认的运算符重载;
在这里插入图片描述

前置++与后置++

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

在这里插入图片描述

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

其他运算符的验证

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_74068921/article/details/134226749