当前位置: 首页 > wzjs >正文

黄浦品牌网站建设谷歌网址

黄浦品牌网站建设,谷歌网址,有没有帮别人做创意的网站,绵阳网站建设设计上一篇文章只实现了operator操作符重载&#xff0c;由于运算符较多&#xff0c;该篇文章单独实现剩余所有的运算符重载。继续以Date类为例&#xff0c;实现运算符重载&#xff1a; 1.Date.h #pragma once#include <iostream> #include <assert.h>using namespace …

在这里插入图片描述

上一篇文章只实现了operator==操作符重载,由于运算符较多,该篇文章单独实现剩余所有的运算符重载。继续以Date类为例,实现运算符重载:
1.Date.h

#pragma once#include <iostream>
#include <assert.h>using namespace std;class Date
{
private:int _year;int _month;int _day;
public:void Print();Date(int yaer, int month, int day);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 GetMonthDays(int year, int month){assert(month > 0 && month < 13);static int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };if (month==2&&(year % 4 == 0 && year % 100 != 0)){return 29;}return MonthDay[month];}Date& operator+=(int day);Date operator+(int day);Date& operator-=(int day);Date operator-(int day);//++d,前置++Date& operator++();//d++,后置++Date operator++(int);//前置--Date& operator--();//后置--Date operator--(int);//两个日期相减:d1-d2int operator-(const Date& d);
};

Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1 
#include <stdio.h>#include "Date.h"void Date::Print()
{cout << _year << "/" << _month << "/" << _day << endl;
}Date::Date(int year=1, int month=1, int day=1)
{_year = year;_month = month;_day = 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) && (_day < d._day))return true;elsereturn false;}elsereturn false;
}bool Date::operator==(const Date& d)
{if ((_year == d._year) && (_month == d._month) && (_day == d._day))return true;elsereturn 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 || *this == d);
}bool Date::operator!=(const Date& d)
{return !(*this == d);
}Date& Date::operator+=(int day)
{_day += day;//先加//这里是while,因为如果是if的话,如果一次加了很大的数据减一次不一定能减得完!!!while(_day > GetMonthDays(_year, _month)){_day -= GetMonthDays(_year, _month);++_month;if (_month == 13){++_year;_month = 1;}}return *this;
}Date Date::operator+(int day)
{Date tmp=*this;tmp += day;return tmp;
}Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){--_month;if (_month == 0){--_year;_month = 12;}_day += GetMonthDays(_year, _month);}return *this;
}Date Date::operator-(int day)
{Date tmp = *this;tmp -= day;return tmp;
}Date& Date::operator++()
{return *this += 1;
}Date Date::operator++(int) 
{Date tmp = *this;*this += 1;return tmp;
}Date& Date::operator--()
{*this - 1;return *this;
}Date Date::operator--(int)
{Date tmp = *this;*this -= 1;return tmp;
}
//日期-日期,计算两个日期之间相差多少天int Date::operator-(const Date& d)
{int flag = 1;Date max = *this;Date min = d;if (*this < d){//赋值为-1的原因:因为这个函数是有顺序的d1-d2,如果遇到d1<d2,也就是小减大,最终返回的结果是个负数,所以说这里要变成-1。flag = -1;max = d;min = *this;}//定义一个变量int n = 0;// 用循环算两个日期中间的差值while (min != max){++min;++n;}return n * flag;
}

3.Test.cpp

#define _CRT_SECURE_NO_WARNINGS 1 
#include <stdio.h>
#include "Date.h"int main()
{Date d1(2024, 2, 15);Date d2 = d1 + 20;d1.Print();d2.Print();bool ret=d1 > d2;if (ret){d1.Print();}d2 += 100;d2.Print();d2 -= 100;d2.Print();Date d3 = d2 - 10;d3.Print();Date d4(2024, 1, 29);Date d5(2024, 8, 1);cout << d5 - d4 << endl;++d5;d5.Print();d5++;d5.Print();--d5;d5.Print();d5--;d5.Print();return 0;
}
http://www.dtcms.com/wzjs/154876.html

相关文章:

  • 网监关闭的网站怎么恢复培训心得体会范文大全2000字
  • 从零精通网站建设重庆网站seo多少钱
  • 网站建设方案书是啥正规网络推广服务
  • 详情页设计素材seo包括什么
  • 乌鲁木做兼职的网站域名查询网入口
  • wordpress点击分类目录空白整站优化系统
  • 微信认证 网站山东做网站公司
  • 做网站需要什么营业执照永久域名查询
  • wap搜索引擎东莞seo网络营销
  • 保亭整站优化天津谷歌优化
  • 网站域名到期叫开发一个小程序一般需要多少钱呢
  • 公司名字大全英文seo外链
  • 微网站开发方案模板产品线上推广方式都有哪些
  • 信誉好的永州网站建设产品推广运营的公司
  • 网站主页模板图片aso优化
  • 微信群公告如何做网站链接百度一下你就知道手机版官网
  • 旅游网站建设实训报告站长网站查询工具
  • 58网站怎么样做效果会更好制作网站要找什么公司
  • 企业网站建设及推广研究互联网营销师是什么
  • 站长工具平台seo教学
  • 营销网站建设规划方案seo平台怎么样
  • 免费个人手机网站营销型网站建设多少钱
  • 促销型网站制作公司网站
  • 中国做网站的公司全网推广怎么做
  • 射洪哪里可以做网站产品软文范例
  • 微信公众号手机网站开发自媒体
  • 海阳玖网站建设百一度一下你就知道
  • 注重网站开发设计与建设设计培训学院
  • 网站运营每天做啥工作域名注册服务网站
  • 企业门户网站建设公司成人厨师短期培训班