中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

做植物提取物的專業(yè)網(wǎng)站線上推廣是做什么的

做植物提取物的專業(yè)網(wǎng)站,線上推廣是做什么的,大嶺山營(yíng)銷型網(wǎng)站建設(shè),什么網(wǎng)站做招聘比較好文章目錄 前言一、日期類的實(shí)現(xiàn)二、this指針的const修飾總結(jié) 前言 C日期類的完整實(shí)現(xiàn)&#xff0c;以及this指針的const修飾等的介紹 一、日期類的實(shí)現(xiàn) // Date.h #pragma once#include <iostream> using namespace std;#include <assert.h>class Date {// 友元函…

文章目錄

  • 前言
  • 一、日期類的實(shí)現(xiàn)
  • 二、this指針的const修飾
  • 總結(jié)


前言

C++日期類的完整實(shí)現(xiàn),以及this指針的const修飾等的介紹


一、日期類的實(shí)現(xiàn)

// Date.h
#pragma once#include <iostream>
using namespace std;#include <assert.h>class Date
{// 友元函數(shù)friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);
public:// 構(gòu)造函數(shù)Date(int year = 1970, int month = 1, int day = 1);void Print() const{cout << _year << '-' << _month << '-' << _day << endl;}// 拷貝構(gòu)造函數(shù)Date(const Date& d);// 析構(gòu)函數(shù)~Date();// 賦值運(yùn)算符重載Date& operator=(const Date& d);// 運(yùn)算符重載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;// 獲取每月天數(shù)int GetMonthDay(int year, int month);// + += - -= 天數(shù)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"// 構(gòu)造函數(shù)
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);}}// 拷貝構(gòu)造函數(shù)
Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}// 析構(gòu)函數(shù)
Date::~Date()
{_year = 0;_month = 0;_day = 0;
}// 賦值運(yùn)算符重載
Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}// 運(yùn)算符重載
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);
}// 獲取月份天數(shù)
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];}
}// + += - -= 天數(shù)
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修飾

// 簡(jiǎn)單的日期類
#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;
}

上述日期類實(shí)現(xiàn)無(wú)法打印d2,原因如下:
使用const修飾創(chuàng)建類,使d2在調(diào)用Print函數(shù)時(shí),傳入的this指針是有const修飾的,與日期類定義類型沖突,所以報(bào)錯(cuò)。如下:
在這里插入圖片描述

修改如下:

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

使this指針變?yōu)閏onst修飾,需要在成員函數(shù)名后加const修飾。

在這里插入圖片描述


總結(jié)

C++日期類的完整實(shí)現(xiàn),以及this指針的const修飾等的介紹

http://www.risenshineclean.com/news/11783.html

相關(guān)文章:

  • 北京商場(chǎng)打折沈陽(yáng)seo整站優(yōu)化
  • 正則表達(dá)式匹配網(wǎng)站交換友鏈要注意什么
  • wordpress圖片主題破解鄭州seo外包服務(wù)
  • wordpress怎么訪問(wèn)seo網(wǎng)站推廣經(jīng)理招聘
  • 網(wǎng)站續(xù)費(fèi)模版怎么優(yōu)化網(wǎng)站性能
  • 專業(yè)移動(dòng)微網(wǎng)站建設(shè)優(yōu)秀軟文范例200字
  • html5 手機(jī)網(wǎng)站模板深圳網(wǎng)絡(luò)營(yíng)銷運(yùn)營(yíng)
  • 中山市兩學(xué)一做網(wǎng)站阿里媽媽推廣網(wǎng)站
  • 高端網(wǎng)站建設(shè)設(shè)搜索引擎的網(wǎng)站
  • 視頻網(wǎng)站用什么做的制作網(wǎng)頁(yè)一般多少錢
  • 網(wǎng)站開(kāi)發(fā)培訓(xùn)光山自媒體平臺(tái)大全
  • b站推廣網(wǎng)站2024年跨境電商seo
  • wordpress站點(diǎn)一百數(shù)據(jù)卡不友情鏈接交易網(wǎng)站源碼
  • 廠房裝修公司深圳寧波網(wǎng)站快速優(yōu)化
  • jsp網(wǎng)站開(kāi)發(fā)教程深圳網(wǎng)絡(luò)公司推廣公司
  • 電子商務(wù)網(wǎng)站與建設(shè)課件西地那非片能延時(shí)多久有副作用嗎
  • wordpress圖片插件放大珠海百度搜索排名優(yōu)化
  • 蘭州做網(wǎng)站公司哪家好十大門(mén)戶網(wǎng)站
  • 延安城鄉(xiāng)建設(shè)規(guī)劃局網(wǎng)站百度帳號(hào)登錄個(gè)人中心
  • 做網(wǎng)站哪家最好acca少女網(wǎng)課視頻
  • 2017年網(wǎng)站設(shè)計(jì)惠州網(wǎng)絡(luò)推廣平臺(tái)
  • 聊城冠縣網(wǎng)站建設(shè)網(wǎng)站結(jié)構(gòu)優(yōu)化的內(nèi)容和方法
  • 二手網(wǎng)站建設(shè)的策劃網(wǎng)站如何賺錢
  • wordpress 任務(wù)馮耀宗seo博客
  • 企業(yè)網(wǎng)站開(kāi)發(fā)技術(shù)湛江百度seo公司
  • 如何用java做網(wǎng)站視頻珠海網(wǎng)站設(shè)計(jì)
  • 揚(yáng)州外貿(mào)網(wǎng)站seo正規(guī)的培訓(xùn)機(jī)構(gòu)有哪些
  • 沈陽(yáng)企業(yè)網(wǎng)站優(yōu)化排名方案如何免費(fèi)注冊(cè)網(wǎng)站
  • 江蘇運(yùn)營(yíng)網(wǎng)站建設(shè)業(yè)務(wù)免費(fèi)建站哪個(gè)最好
  • 做網(wǎng)站需要字體切換鎮(zhèn)江關(guān)鍵字優(yōu)化公司