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

专业做棋牌网站的网站建设晋icp备

专业做棋牌网站的,网站建设晋icp备,如何做广告推广赚钱,前端开发工资文章目录 前言一、日期类的实现二、this指针的const修饰总结 前言 C日期类的完整实现&#xff0c;以及this指针的const修饰等的介绍 一、日期类的实现 // Date.h #pragma once#include <iostream> using namespace std;#include <assert.h>class Date {// 友元函…

文章目录

  • 前言
  • 一、日期类的实现
  • 二、this指针的const修饰
  • 总结


前言

C++日期类的完整实现,以及this指针的const修饰等的介绍


一、日期类的实现

// Date.h
#pragma once#include <iostream>
using namespace std;#include <assert.h>class Date
{// 友元函数friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public:// 构造函数Date(int year = 1970, int month = 1, int day = 1);void Print() const{cout << _year << '-' << _month << '-' << _day << endl;}// 拷贝构造函数Date(const Date& d);// 析构函数~Date();// 赋值运算符重载Date& operator=(const Date& d);// 运算符重载bool operator<(const Date& d) const;bool operator==(const Date& d) const;bool operator<=(const Date& d) const;bool operator>(const Date& d) const;bool operator>=(const Date& d) const;bool operator!=(const Date& d) const;// 获取每月天数int GetMonthDay(int year, int month);// + += - -= 天数Date& operator+=(const int day);Date operator+(const int day) const;Date& operator-=(const int day);Date operator-(const int day) const;// 前置++Date& operator++();// 后置++Date operator++(int);// 前置--Date& operator--();// 后置--Date operator--(int);// 日期-日期int operator-(const Date& d) const;private:int _year;int _month;int _day;
};// 流插入
ostream& operator<<(ostream& out, const Date& d);// 流提取
istream& operator>>(istream& in, Date& d);

// Date.cpp
#define  _CRT_SECURE_NO_WARNINGS#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;assert(false);}}// 拷贝构造函数
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}// 析构函数
Date::~Date()
{_year = 0;_month = 0;_day = 0;
}// 赋值运算符重载
Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}// 运算符重载
bool Date::operator<(const Date& d) const
{if (_year < d._year){return true;}else if (_year == d._year && _month < d._month){return true;}else if (_year == d._year && _month == d._month && _day == d._day){return true;}else{return false;}
}bool Date::operator==(const Date& d) const
{if (_year != d._year){return false;}else if (_year == d._year && _month != d._month){return false;}else if (_year == d._year && _month == d._month && _day != d._day){return false;}else{return true;}
}bool Date::operator<=(const Date& d) const
{return (*this < d || *this == d);
}bool Date::operator>(const Date& d) const
{return !(*this <= d);
}bool Date::operator>=(const Date& d)  const
{return !(*this < d);
}bool Date::operator!=(const Date& d) const
{return !(*this == d);
}// 获取月份天数
int Date::GetMonthDay(int year, int month)
{static int dayArray[13] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };if (month == 2 && year % 4 == 0 && year % 100 != 0 || year % 400 == 0){return 29;}else{return dayArray[month];}
}// + += - -= 天数
Date& Date::operator+=(const 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;
}Date Date::operator+(const int day) const
{Date tmp(*this);tmp += day;return tmp;
}Date& Date::operator-=(const int day)
{if (_day < 0){return *this += -_day;}_day -= day;while (_day <= 0){_month--;if (_month == 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}
Date Date::operator-(const int day) const
{Date tmp(*this);tmp -= day;return tmp;
}// 前置++
Date& Date::operator++()
{*this += 1;return *this;
}// 后置++
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) const
{Date max = *this;Date min = d;int flag = 1;if (max < min){max = d;min = *this;flag = -1;}int num = 0;while (min != max){num++;++min;}return num * 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;
}

// test.cpp
#define  _CRT_SECURE_NO_WARNINGS#include "Date.h"void TestDate1()
{Date d1(2023, 9, 1);Date d2(2000, 1, 1);Date d3(2000, 3, 1);cout << (d1 < d2) << endl;cout << (d1 == d2) << endl;cout << (d2 == d3) << endl;cout << (d1 <= d2) << endl;cout << (d1 > d2) << endl;cout << (d2 <= d3) << endl;cout << (d2 > d3) << endl;Date d4(1949, 10, 1);Date d5(1949, 10, 1);cout << (d4 != d5) << endl;
}void TestDate2()
{Date d1(2024, 5, 5);d1 += 1000;d1.Print();Date d2(2024, 10, 1);(d2 + 100).Print();Date d3(2024, 12, 31);d3 -= 100;d3.Print();
}void TestDate3()
{Date d1(2024, 5, 5);Date d2(2050, 12, 30);cout << d1 - d2 << endl;cout << d2 - d1 << endl;
}void TestDate4()
{Date d1(2024, 5, 5);Date d2(2050, 12, 30);Date d3(1328, 1, 4);d1 = d2 = d3;
}void TestDate5()
{Date d1(2024, 5, 5);Date d2(2050, 12, 30);Date d3(1328, 1, 4);d1 += 1000;d2 -= 50;d3 += 500;cout << d1;cout << d2;cout << d3;
}void TestDate6()
{Date d1;Date d2;cin >> d2 >> d1;cout << d1 << d2;}void TestDate7()
{Date d1(1328, 1, 1);d1.Print();const Date d2(1368, 1, 4);d2.Print();}void TestDate8()
{Date d1(1328, 1, 1);const Date d2(1368, 1, 4);cout << (d1 < d2) << endl;}int main()
{TestDate8();return 0;
}

二、this指针的const修饰

// 简单的日期类
#include <iostream>
using namespace std;class Date
{
public:Date(int year = 1368, int month = 1, int day = 4){_year = year;_month = month;_day = day;}void Print() {cout << _year << "-" << _month << "-" << _day << endl;}Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}~Date(){_year = 0;_month = 0;_day = 0;}
private:int _year;int _month;int _day;
};int main()
{Date d1(1949, 10, 1);d1.Print();const Date d2(1945, 8, 15);d2.Print();return 0;
}

上述日期类实现无法打印d2,原因如下:
使用const修饰创建类,使d2在调用Print函数时,传入的this指针是有const修饰的,与日期类定义类型冲突,所以报错。如下:
在这里插入图片描述

修改如下:

	void Print() const{cout << _year << "-" << _month << "-" << _day << endl;}

使this指针变为const修饰,需要在成员函数名后加const修饰。

在这里插入图片描述


总结

C++日期类的完整实现,以及this指针的const修饰等的介绍

http://www.15wanjia.com/news/155872.html

相关文章:

  • 做内贸的什么网站效果好建筑兼职网
  • 手机网站建设免费空间海淀营销型网站建设
  • 网站开发工程师应聘书范文700字小程序定制开发团队
  • 网站建设质量如何衡量智能音箱功能ui设计效果图
  • 买了一个域名如何做网站炒股网站开发
  • 高端 网站制作电脑公司网站模板
  • thinkphp网站开发实例教程wordpress设置邮箱插件
  • 网站建设结单 优帮云申请注册一个商标多少钱
  • 软件手机站中国住房和城乡建设厅官网
  • 可以做试题的网站网站关键词优化遇到的情况和解决方法
  • 小程序建站平台学做网站论坛好吗
  • 中国钓鱼网站大全网易企业邮箱入口官网
  • 建网站在线支付怎么潮阳网站建设公司
  • 做网站要租服务器网站建设调查报告
  • 北京创意网站设计修改wordpress wp-admin
  • 百度网站排名哪家好软件开发工具有哪些
  • 杭州营销网站建设公司保定网站建设报价
  • 江门网站优化经验郑州网站制作公司
  • 移动网站建设是什么韶关手机网站建站
  • 建站网站排行手工制作教程
  • 网站制作公司费用医院网站建设具体内容
  • 广州营销推广网站郑州最好的男科医院哪家好
  • 企业品牌网站建设价格建设网站前的市场分析包括
  • 手机网站建设浩森宇特app网站开发学习
  • 海淀做网站公司单页网站怎么做外链
  • 做平台网站需要多少钱昆山企业网站设计
  • 黄冈公司做网站鸿安建设集团网站
  • 做新房网站怎么弄wordpress制作小说网站模板下载
  • 霸屏网站开发阿里云域名注册官网
  • 微信公众平台怎么做微网站吗wordpress放入视频链接