Date类实现运算符和赋值重载


通过Date类来实现对于前文学习接触的赋值型重载,构造函数,析构函数,下面分为三个文件

1.test.cpp 进行测试的.cpp文件

2.Date.h Date的头文件,里面是Date类的成员函数声明和成员变量

3.Date.cpp 对于头文件Date.h的处理,定义Date的成员函数

Date.h

头文件,包括Date类的成员变量和成员函数的声明

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class Date {
    
    
public:
	Date(int year = 1900, int month = 1, int day = 1);
	// 拷贝构造函数

	// d2(d1)

	Date(const Date& d);

	// 赋值运算符重载

	// d2 = d3 -> d2.operator=(&d2, d3)

	Date& operator=(const Date& d);

	// 析构函数

	~Date();

	// 日期+=天数

	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);

	int getDays(const Date& d);
private:

	int _year;

	int _month;

	int _day;

};

Date.cpp

对于Date.h头文件的成员函数进行定义,实现赋值重载和运算符重载

#define _CRT_SECURE_NO_WARNINGS

#include"Date.h"

//Date:: 开放类域
Date::Date(int year, int month , int day) {
    
    
	_year = year;
	_month = month;
	_day = day; 
}

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


// 赋值运算符重载

	// d2 = d3 -> d2.operator=(&d2, d3)

Date& Date::operator=(const Date& d) {
    
    
	(*this)._year = d._year;
	(*this)._month = d._month;
	(*this)._day = d._day;
	return *this;
}

Date::~Date() {
    
    
	cout <<(this)<< "调用析构函数" << endl;
}

Date& Date::operator+=(int day) {
    
    
	_day += day;
	while (_day > getDays(*this)) {
    
    
		_day -= getDays(*this);
		_month++;
		if (_month == 13) {
    
    
			_month = 1;
			_year++;
		}
	}
	return *this;
}
//+的话不能改变this的数值,所以需要使用临时变量
Date Date::operator+(int day) {
    
    
	Date p = *this;
	p.operator+=(day);
	return p;
}
//日期相减
Date Date::operator-(int day) {
    
    
	Date p = *this;
	p._day-= day;
	if (p._day > 0) {
    
    
		return p;//如果减去天数之后,day是个整数,说明还在这个月内,只是天数小了,所以可以直接返回
	}
	p._day = abs(p._day);//反之取得绝对值,然后跟上一个月的天数比较,如果大于,那就减去
	
	while (true) {
    
    

		p._month--;
		if (p._month == 0) {
    
    
			p._month = 12;
		}
		if (p._day > getDays(p)) {
    
    
			p._day -= getDays(p);
		}
		else {
    
    
			p._day = getDays(p) - p._day;
			return p;
		}
	}
}
Date& Date::operator-=(int day) {
    
    
	*this = (*this).operator-(day);
	return *this;
}

//前置++  括号为无参函数
Date& Date::operator++() {
    
    
	(*this).operator+=(1);
	return *this;
}
//后置++,括号中的形参为int类型  没有实际意义,只是为了区分前置和后置++
Date Date::operator++(int) {
    
    
	//后置++  需要使用临时变量
	Date date = *this;
	(*this).operator+=(1);
	return date;//返回的是形参,需要进行拷贝赋值返回处理
}

//获得对应月的天数
int Date::getDays(const Date& d) {
    
    
	int day[13] = {
    
     0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (d._month == 2 && ((d._year % 4 == 0 && d._year % 100 != 0) || d._year % 400 == 0)) {
    
    
		return 29;
	}
	else {
    
    
		return day[d._month];
	}
}

// 后置--

Date Date::operator--(int) {
    
    
	Date date = (*this);
	(*this).operator-=(1);
	return date;
}
//
// 前置--

Date& Date::operator--() {
    
    
	(*this).operator-=(1);
	return *this;
}

// >运算符重载

//日期大小比较:如果日期靠后,就大,反之为小
bool Date::operator>(const Date& d) {
    
    
	if ((*this)._year > d._year) {
    
    
		return true;
	}
	else if(((*this)._year == d._year)&&((*this)._month>d._month)) {
    
    
		return true;
	}
	else if (((*this)._year == d._year) && ((*this)._month > d._month) && ((*this)._day > _day)) {
    
    
		return true;
	}
	else {
    
    
		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).operator>(d) || (*this).operator==(d);
}
//
// <运算符重载

bool Date::operator < (const Date& d) {
    
    
	if ((*this).operator>=(d)) {
    
    
		return false;
	}
	else {
    
    
		return true;
	}
}

 <=运算符重载

bool Date::operator <= (const Date& d) {
    
    
	return (*this).operator<(d) || (*this).operator==(d);
}
// !=运算符重载

bool Date::operator != (const Date& d) {
    
    
	if ((*this).operator==(d)) {
    
    
		return false;
	}
	else {
    
    
		return true;
	}
}

// 日期-日期 返回天数
//
bool getyear(int num) {
    
    
	if ((num % 4 == 0 && num % 100 != 0) || num % 400 == 0) {
    
    
		return true;
	}
	else {
    
    
		return false;
	}
}


int Date::operator-(const Date& d) {
    
    
	int num = 0;
	while ((*this).operator!=(d))
	{
    
    
		(*this).operator++();
		num++;
	}
	return num;
}

test.cpp

里面又main函数,是作为测试用的.cpp文件

#define _CRT_SECURE_NO_WARNINGS

#include"Date.h"

void Test()
{
    
    
	Date p1(2020,10,10);
	//cout << p.getDays(p) << endl;

	//p.operator+=(1000);

	Date c = p1.operator+(100);//366
	cout << p1.operator-(c) << endl;
}

int main()
{
    
    
	Test();
	return 0;
}

这个test.cpp文件里面的Test()函数在进行测试,请自行添加测试

总结

赋值重载的话,返回值有两种

  1. 如果是临时变量,返回值为类类型 Date
  2. 如果不是临时变量,是*this 那就是传引用返回 Date&

运算符重载的话,返回值一般是bool,而且有些是可以利用其他运算符重载搞定的

  1. 比如> 和== 这两个就可以将各种大小比较搞定,是为了提高代码复用率
  2. 且返回值一般为bool,传参是两个参数,一个是this,一个是const Date& p 使用传引用参数是为了提高效率

猜你喜欢

转载自blog.csdn.net/qq_63319459/article/details/130447377
今日推荐