C++之日期类的实现之 默认成员函数和运算符重载的综合应用(+、-、=、<、>、+=、-=)

在完成日期类时,涉及到对应月份天数的限制,先构造出下面的函数,用以判断当月天数。

//获取某一月的天数
int Date::GetMonthDay(int year, int month)
{
	static int daysArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int days = daysArray[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		days = 29;
	}
	return days;
}
Date.h文件
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
	int GetMonthDay(int year, int month);
	void Print();

	Date(int year = 1900, int month = 1, int day = 1);

	/*拷贝构造函数*/

	Date(const Date& d);

	// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d);
	// 析构函数
	~Date();

	//d1 += 10
	Date& operator+=(int day);

	//d1 + 10
	Date operator+(int day);
	 
	//d1 -= 10
	Date& operator-=(int day);

	//d1 - 10
	Date operator-(int day);

	//++d1
	//d1.operator++(&d1)
	Date& operator++();

	//d1++
	//d1.operator++(&d1,0)
	//为了跟前置++区分,加了一个参数,但是这个参数不使用
	Date operator++(int);

	bool operator>(const Date& d);
	// ==运算符重载
	bool operator==(const Date& d);
	// >=运算符重载
	inline 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);
private:
	int _year;
	int _month;
	int _day;
};
Date.cpp文件
#include"Date.h"

//拷贝构造函数
 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)
 {
	 if (this != &d)
	 {
		 _year = d._year;
		 _month = d._month;
		 _day = d._day;
	 }
	 return *this;
 }
 
 // 析构函数
 Date:: ~Date()
 {
	 // 清理工作,Date类中没有需要清理的资源
	 // 所以严格来说Date不需要写析构函数,因为我们不写,编译器默认生成就可以用
	 cout << "~Date()" << endl;
 }

 //打印函数
void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

//获取某一月的天数
int Date::GetMonthDay(int year, int month)
{
	static int daysArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int days = daysArray[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		days = 29;
	}
	return days;
}
//判断日期是否非法
	Date::Date(int year, int month, int day) 
{
	_year = year;
	_month = month;
	_day = day;
	//cout << "Date(int year,int month,int day)" << endl;
	if (!(year >= 0 && month > 0 && month < 13 && day > 0 && day < GetMonthDay( year,  month)))
	{
		cout << "日期非法" << endl;
	}
		
}
	//d1 += 10
    Date& Date::operator += (int day)//赋值运算符重载的特点
	{
		if (day < 0)
		{
			return *this -= -day;
		}
		_day += day;
		//日期是否合法
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month == 13)
			{
				_year++;
				_month = 1;
			}
		}
		return *this;
	}
	//d1 + 10
	//要实现+,d1的值是不能被改变的,所以拷贝构造一个ret
	//ret为+后的结果
	Date Date::operator+(int day)
	{
		//Date ret(*this);//拷贝构造
		//ret._day += day;
		//while (ret._day > GetMonthDay(ret._year, ret._month))
		//{
		//	ret._day -= GetMonthDay(ret._year, ret._month);
		//	ret._month++;
		//	if (ret._month == 13)
		//	{
		//		ret._year++;
		//		ret._month = 1;
		//	}
		//}
		Date ret(*this);
		ret += day;//直接调用+=,ret.operator(day)
		return ret;
 	}
	//d1-=day
	Date& Date::operator-=(int day)
	{
		if (day < 0)
		{
			return *this += -day;
		}
		//日期是否合法
		_day -= day;
		while (_day < 0)
		{
			_day += GetMonthDay(_year, _month);
			_month--;
			if (_month == 0)
			{
				_year--;
				_month = 1;
			}
		}
		return *this;
	}
	//d1-day
	Date Date::operator-(int day)
	{
		Date ret(*this);
		ret -= day;
		return ret;
	}
	//++d1 等价于 d1 += 1
	Date& Date::operator++()
	{
		*this += 1;//直接
		return *this;
	}
	//d1++
	Date Date::operator++(int)
	{
		Date ret(*this);//要返回++之前的值,所以先拷贝构造一个保存起来
		*this += 1;
		return ret;//出了作用域就不存在了,不能用引用返回
	}
	// ==运算符重载
	bool Date :: operator==(const Date& d)
	{
		return _year == d._year && _month == d._month && _day == d._day;
	}
	bool Date::operator>(const Date& d)
	{
		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)
	{
		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);
	}
	// 日期-日期 返回天数
	int Date::operator-(const Date& d)//函数名相同,参数不同,构成重载
	{
		//直接进行日期之间的相减很难
		Date max = *this, min = d;
		int flag = 1;
		if (*this < d)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int n = 0;
		while (min != max)//n 的数就是相差的天数
		{
			++n;
			++min;
		}
		return n*flag;
	} 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个完整的 C++ 程序,满足您的要求: ```cpp #include <iostream> using namespace std; class Complex { private: double real; // 实部 double imag; // 虚部 public: Complex() { // 默认构造函数 real = 0; imag = 0; } Complex(double r, double i) { // 带参数构造函数 real = r; imag = i; } Complex(const Complex& c) { // 拷贝构造函数 real = c.real; imag = c.imag; } ~Complex() { // 析构函数 // do nothing } void setReal(double r) { // 设置实部 real = r; } void setImag(double i) { // 设置虚部 imag = i; } double getReal() const { // 获取实部 return real; } double getImag() const { // 获取虚部 return imag; } Complex operator+(const Complex& c) const { // 运算符重载 + return Complex(real + c.real, imag + c.imag); } Complex operator-(const Complex& c) const { // 运算符重载 - return Complex(real - c.real, imag - c.imag); } Complex operator*(const Complex& c) const { // 运算符重载 * return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real); } Complex operator/(const Complex& c) const { // 运算符重载 / double denominator = c.real * c.real + c.imag * c.imag; return Complex((real * c.real + imag * c.imag) / denominator, (imag * c.real - real * c.imag) / denominator); } Complex operator++() { // 运算符重载前置 ++ real++; imag++; return *this; } Complex operator++(int) { // 运算符重载后置 ++ Complex tmp(*this); real++; imag++; return tmp; } Complex operator--() { // 运算符重载前置 -- real--; imag--; return *this; } Complex operator--(int) { // 运算符重载后置 -- Complex tmp(*this); real--; imag--; return tmp; } friend istream& operator>>(istream& is, Complex& c) { // 运算符重载 >> is >> c.real >> c.imag; return is; } friend ostream& operator<<(ostream& os, const Complex& c) { // 运算符重载 << os << c.real << " + " << c.imag << "i"; return os; } explicit operator double() const { // 型转换函数 return real; } }; int main() { Complex c1(1.0, 2.0); Complex c2(3.0, 4.0); // 运算符重载 Complex c3 = c1 + c2; Complex c4 = c1 - c2; Complex c5 = c1 * c2; Complex c6 = c1 / c2; Complex c7 = ++c1; Complex c8 = c2++; Complex c9 = --c1; Complex c10 = c2--; cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl; cout << "c1 + c2 = " << c3 << endl; cout << "c1 - c2 = " << c4 << endl; cout << "c1 * c2 = " << c5 << endl; cout << "c1 / c2 = " << c6 << endl; cout << "++c1 = " << c7 << endl; cout << "c2++ = " << c8 << endl; cout << "--c1 = " << c9 << endl; cout << "c2-- = " << c10 << endl; // 型转换 double d = (double)c1; cout << "double(c1) = " << d << endl; return 0; } ``` 程序中定义了一个 `Complex` 实现了复数的加、减、乘、除、前置/后置自增/自减、输入输出运算符重载以及型转换。在 `main` 函数中进行了测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值