您的位置:首页 > 新闻 > 资讯 > 公司网站设计制作长沙_视频拍摄手法_百度普通下载_百度如何发布信息推广

公司网站设计制作长沙_视频拍摄手法_百度普通下载_百度如何发布信息推广

2025/7/2 21:10:00 来源:https://blog.csdn.net/Giants2024/article/details/146989161  浏览:    关键词:公司网站设计制作长沙_视频拍摄手法_百度普通下载_百度如何发布信息推广
公司网站设计制作长沙_视频拍摄手法_百度普通下载_百度如何发布信息推广

本文运用c++类和对象中的构造函数, 析构函数 ,拷贝构造函数 , 赋值运算符重载等为大家模拟实现日期类的操作

#define _CRT_SECURE_NO_WARNINGS 1
#include"date.h"
void Date:: showinfo()
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
Date::Date(int year, int month, int day)
{cout<<"Date(int year, int month, int day)" << endl;_year = year;_month = month;_day = day;//_t._hour = 10;//_t._minute = 0;//_t._second = 0;
}
Date::Date()
{cout << "Date()" << endl;
}
Date::Date(const Date& d)
{cout << "Date(const Date & d)" << endl;_year = d._year;_month = d._month;_day = d._day;//_t = d._t;
}
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;
}
//赋值运算符重载//Date Test(Date d)
//{
//	Date temp(d);
//	return temp;
//}
Date& Date:: operator+=(int day)
{this->_day += day;int mday = GetMonthDay(this->_year, this->_month);while(this->_day > mday){this->_day -= mday;this->_month++;if (this->_month > 12){this->_month = 1;this->_year++;}mday = GetMonthDay(this->_year, this->_month);}return *this;}
// 日期+天数
Date Date::operator+(int day)
{Date tmp=*this;tmp._day += day;int mday = GetMonthDay(tmp._year, tmp._month);while (tmp._day > mday){tmp._day -=mday;tmp._month++;if (tmp._month > 12){tmp._month = 1;tmp._year++;}mday = GetMonthDay(tmp._year, tmp._month);}return tmp;}
// 日期-天数
Date Date::operator-(int day)
{Date tmp = *this;tmp._day -= day;int mday = GetMonthDay(tmp._year, tmp._month-1);while (tmp._day <=0){tmp._day += mday;tmp._month--;if (tmp._month <1){tmp._month = 12;tmp._year--;}if (tmp._month == 1){mday = GetMonthDay(tmp._year-1, 12);}else{mday = GetMonthDay(tmp._year, tmp._month - 1);}}return tmp;
}
//日期-=天数
Date& Date::operator-=(int day)
{this->_day -= day;int mday = GetMonthDay(this->_year, this->_month - 1);while (this->_day <= 0){this->_day += mday;this->_month--;if (this->_month < 1){this->_month = 12;this->_year--;}if (this->_month == 1){mday = GetMonthDay(this->_year - 1, 12);}else{mday = GetMonthDay(this->_year, this->_month - 1);}}return *this;
}
// 前置++
Date& Date::operator++()
{*this += 1;return *this;
}
// 后置++
Date Date::operator++(int)
{Date tmp=*this;*this += 1;return tmp;
}
// 后置--
Date Date::operator--(int)
{Date tmp = *this;*this -= 1;return tmp;
}
// 前置-
Date& Date::operator--()
{*this -= 1;return *this;
}
// >运算符重载
bool Date::operator>(const Date& d)
{if (this->_year > d._year){return 0;}if(this->_year == d._year){if (this->_month > d._month)return 0;if(this->_month == d._month){if (this->_day >= d._day)return 0;}}return 1;
}
// ==运算符重载
bool Date::operator==(const Date& d)
{if (this->_year == d._year && this->_month == d._month && this->_day == d._day)return 1;return 0;
}
// >=运算符重载
bool Date::operator >= (const Date& d)
{return *this == d || *this > d;
}
// <运算符重载
bool Date::operator < (const Date& d)
{return !(*this == d || *this > d);
}
// <=运算符重载
bool Date::operator <= (const Date& d)
{return *this == d || *this < d;
}
// !=运算符重载
bool Date::operator != (const Date& d)
{return !(*this == d);
}
// 日期-日期  返回天数
int Date::operator-(const Date& d)
{Date max = *this;Date min = d;int flag = 1;if (*this < d){max = d;min = *this;flag = -1;}int n = 0;while (min != max){++min;++n;}return n * flag;
}
ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}istream& operator>>(istream& in, Date& d)
{int year, month, day;in >> year >> month >> day;if (month > 0 && month < 13&& day > 0 && day <= d.GetMonthDay(year, month)){d._year = year;d._month = month;d._day = day;}else{cout << "非法日期" << endl;assert(false);}return in;
}
#pragma once
#include<iostream> 
#include<assert.h>
using namespace std;
using std::cout;
using std::endl;
class Time
{
public:Time(){cout << "Time()" << endl;_hour = 0;_minute = 0;_second = 0;}
private:int _hour;int _minute;int _second;
};
class Date
{friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public:void showinfo();Date(int year, int month, int day);//构造函数Date();Date(const Date& d);  //拷贝构造//拷贝构造函数典型调用场景://	1.使用已存在对象创建新对象//	2.函数参数类型为类类型对象//	3.函数返回值类型为类类型对象Date& operator=(const Date& d){if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;} // 获取某年某月的天数int GetMonthDay(int year, int month);// 日期+=天数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);private:int _year;int _month;int _day;Time _t;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com