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

焦作建网站网站优化策划书

焦作建网站,网站优化策划书,好的做淘宝详情页的网站有哪些,深圳如何搭建建网站文章目录 前言一、日期类的实现二、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.dtddedu.com/news/94.html

相关文章:

  • 长沙网站制作建设搜索引擎优化特点
  • 丽江网站建设 莱芜国家市场监督管理总局官网
  • 网站建设设计问卷网站排名怎么做上去
  • 可以做网站二维码吗注册网站在哪里注册
  • 国内优秀html网站学电脑培训班
  • 做个小网站 虚拟空间 买服务器seo是搜索引擎营销吗
  • 怎么做自己的cms导购网站网络推广公司深圳
  • 一起做单网站怎么样优化设计答案六年级上册语文
  • 南昌做网站公司照片查询百度图片搜索
  • 百度指数关键词志鸿优化设计电子版
  • 公司网站建设应符合哪些法规网页
  • wordpress模板创建徐州seo外包
  • 360 的网站链接怎么做如何查询域名注册人信息
  • 邵阳市网站建设广州网站优化外包
  • dns劫持网站怎么做潍坊住房公积金管理中心
  • 南充建网站的资料线上销售平台有哪些
  • 注册公司需要登录的网址优化网站哪个好
  • 企业网站建设费未付款怎样挂账网站优化推广方法
  • 珠海外贸网站建设百度账号登录个人中心
  • 网站定制开发什么意思免费网站推广软件
  • 电商网站用php做的吗今日短新闻20条
  • 济南城乡住房建设厅网站网络推广平台软件
  • 网站建设项目表百度公司排名多少
  • 盘龙网站建设公司关键词排名点击软件首页
  • 专业建设金融行业网站的公司网络营销内容
  • 怎么做免费的网站链接seo优化技巧有哪些
  • 百度搜索这个网站为什么这么差seo优化网站排名
  • 网站建设设计维片seo搜索引擎推广什么意思
  • pcb计价网站建设深圳网络营销推广专员
  • 个人网站源码php厦门网站快速排名优化