日期类的实现

1.函数声明

#pragma once
#include<iostream>
using namespace std;
class Date
{
//重载输入输出流
//直接在类里实现的话,this会抢占第一个参数的位置,
//导致调用时d1<<cout,不符合常规,因此在外实现,并声明友元
    friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in,  Date& d);
public:
   //构造函数
	Date(int year = 2000, int month = 1, int day = 1);
	//拷贝构造
	Date(const Date& d);
	//打印
    void print();
    //当月天数
	int GetMonthDay(int year, int month);
	//比较 都可以用const修饰*this
	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);
	//日期+-,不改变自身可以用const修饰,例如+ -
	Date operator+=(int day);
	Date operator+(int day);
	Date operator-=(int day);
	Date operator-(int day);
	Date& operator++();
	Date& operator++(int);
	Date& operator--();
	Date& operator--(int);
	int operator-(const Date& d);
private:
	int _year;
	int _month;
	int _day;

};
//ostream& operator<<(ostream& out, const Date& d);
//istream& operator>>(ostream& in,  Date& d);

2.函数实现

#pragma once
#include"Date.h"
Date::Date(int year , int month , int day )
{
	if (month > 0 && month < 13
		&& (day > 0 && day <= GetMonthDay(year, month)))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
		cout << "日期非法" << endl;
}
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
void Date::print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}
int Date::GetMonthDay(int year, int month)
{
	static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = days[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		day += 1;
	}
	return day;
}
bool Date::operator==(const Date& d)
{
	return (_year == d._year) && (_month==d._month) & (_day==d._day);
}
bool Date::operator>(const Date& d )
{
	return _year > d._year ||
		(_year == d._year && _month > d._month) ||
		(_year == d._year && _month == d._month && _day > d._day);
}
bool Date::operator>=(const Date& d)
{
	return *this == d || *this > d;
}
bool Date::operator<(const Date& d)
{
	return!(*this >= d);
}	
bool Date::operator<=(const Date& d)
{
	return!(*this > d);
}
bool Date::operator!=(const Date& d)
{
	return!(*this == d);
}
Date Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}
Date Date::operator+(int day)
{
	Date temp = *this;
	temp += day;
	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)
{
	Date temp = *this;
	temp -= day;
	return temp;
}
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
Date& Date::operator++(int)
{
	Date temp = *this;
	temp += 1;
	return temp;
}
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}
Date& Date::operator--(int)
{
	Date temp = *this;
	temp -= 1;
	return temp;
}
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		flag = -1;
		max = d;
		min = *this;
	}
	int n = 0;
	while (min != max)
	{
		++min;
		n++;
	}
	return flag*n;
}
 ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
 istream& operator>>(istream& in,Date& d)
{
	 in >>d._year >> d._month >>d._day;
	 return in;
}

3.const成员

将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << "Print()" << endl;
		cout << "year:" << _year << endl;
		cout << "month:" << _month << endl;
		cout << "day:" << _day << endl << endl;
	}
	void Print() const
	{
		cout << "Print()const" << endl;
		cout << "year:" << _year << endl;
		cout << "month:" << _month << endl;
		cout << "day:" << _day << endl << endl;
	}
private:
	int _year=2000;
	int _month=1;
	int _day=1;
	
};
int main()
{
	Date d1(2022, 1, 13);
	d1.Print();
	const Date d2(2022, 1, 13);
	d2.Print();
	return 0;
}

在这里插入图片描述
请思考下面的几个问题:

  1. const对象可以调用非const成员函数吗?
    不可以,这属于权限放大
  2. 非const对象可以调用const成员函数吗?
    可以,权限缩小是可以的
  3. const成员函数内可以调用其它的非const成员函数吗?
    不可以,这属于权限放大
  4. 非const成员函数内可以调用其它的const成员函数吗?
    可以,权限缩小是可以的

4.取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成。

class Date
{
public :
Date* operator&()
{
return this ;
}
const Date* operator&()const
{
return this ;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值