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

pr效果做的好的网站有哪些白帽seo公司

pr效果做的好的网站有哪些,白帽seo公司,网站建设厘金手指下拉12,黄石百度做网站多少钱引言 C类和对象练习:Date类实现日期的差,比较日期的大小,日期的前置后置,--,输入输出Date类,对默认函数的练习。 _涂色_-博客主页 C基础专栏 分三个文件来写: Date.cpp //类函数的实现 Date.h…

引言

        C++类和对象练习:Date类实现日期的差,比较日期的大小,日期的前置后置++,--,输入输出Date类,对默认函数的练习。

_涂色_-博客主页

C++基础专栏

分三个文件来写: 

Date.cpp //类函数的实现
Date.h   //类函数的声明
Test.cpp //测试类的功能是否正确

一、先在Date.h中声明类里面的函数 

#pragma once
#include<assert.h>
#include<stdio.h>
#include<iostream>using namespace std;class Date
{
public:// 友函数的声明,外面的函数要访问类里面的对象,需要使用友函数。friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);int GetMonthDay(int year, int month){assert(month > 0 && month < 13);int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && ((year % 4 == 0 && year % 400 != 0) || year % 400 == 0))return 29;return monthDayArray[month];}// 只要不改变调用对象的函数都建议加constvoid Print() const;bool CheckDate()const; //检查日期是否合法Date(int year = 1, int month = 1, int day = 1);Date& operator+=(int day);Date  operator+(int day) const ;Date& operator-=(int day);Date  operator-(int day) const;// ++d1; -> d1.operator++();Date& operator++();// d1++; -> d1.operator++(1);Date& operator++(int);// --d1;Date& operator--();//d1;Date operator--(int);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 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中实现)

1、实现检查日期是否合法,打印日期的函数

bool Date::CheckDate() const
{if (_month < 1 || _month > 12|| _day < 1 || _day > GetMonthDay(_year, _month)){return false;}return true;
}// void Print(const Date* const this) const
void Date::Print() const
{cout << _year << "/" << _month << "/" << _day << endl;
}

2、构造函数,拷贝构造函数

下一章介绍实例化构造函数,所以这里先不使用实例化构造

Date::Date(int year, int month, int day) //构造函数
{_year = year;_month = month;_day = day;if (!CheckDate()){cout << "非法日期" << endl;}
}Date::Date(const Date& d)  // 拷贝构造函数
{_year = d._year;_month = d._month;_day = d._day;
}

3、日期+=day函数,日期+day函数

// += 
Date& Date::operator+=(int day)
{_day += day;//进位while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){_month = 1;_year++;}}return *this;
}// + 
Date Date::operator+(int day) const
{Date tmp(*this);  //使用的是默认的拷贝构造tmp += day;return tmp;
}

4、日期 -= day函数,日期-day函数

Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;
}// 日期-天数
// d1 - 100
Date Date::operator-(int day) const
{Date tmp(*this);tmp -= day;return tmp;
}

5、前置++,后置++,前置--,后置--

//++d1;  -> d1.operator++()
Date& Date::operator++()
{*this += 1;return *this;
}// d1++; -> d1.operator++(int)
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}//--d1
Date& Date::operator--()
{*this -= 1;return *this;
}//d1--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}

 6、两个日期比较大小

技巧:实现== 和 < 逻辑,其他比较使用这两个逻辑来实现

bool Date::operator==(const Date& d) const
{return _year == d._year&& _month == d._month&& _year == d._year;
}
bool Date::operator!=(const Date& d) const
{return !(*this == d);
}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;}return false;
}// d1 <= d2
bool Date::operator<=(const Date& d) const
{return (*this < d || *this == d);
}// d1 > d2
bool Date::operator>(const Date& d) const
{return !(*this <= d);
}// d1 >= d2;
bool Date::operator>=(const Date& d) const
{return !(*this < d);
}

7、日期-日期

// 日期-日期 
//d1 - d
int Date::operator-(const Date& d) const
{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; //大的减小的是正数,小的减大的是负数
}

8、实现类的输入和输出

将重载函数写在类里面:

ostream& Date::operator<<(ostream& out)
{out << this->_year << "年" << this->_month << "月" << this->_day << "日" << endl;return out;
}

输出的格式:会发现不符合常规逻辑。

原因:

• 重载  <<  和  >>  时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第一个形参位置,第一个形参位置是左侧运算对象,调用时就变成了对象 <<cout,不符合使用习惯和可读性。重载为全局函数把ostream/istream放到第一个形参位置就可以了,第二个形参位置当前类类型对象。

所以将重载函数写在类的外面:

        因为要访问类的私有成员,所以在类中要使用函数的友元。

 

ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}istream& operator>>(istream& in, Date& d)
{while (1){cout << "请依次输入年月日:>";in >> d._year >> d._month >> d._day;if (d.CheckDate()){break;}else{cout << "非法日期,请重新输入!" << endl;}}return in;
}

三、所有代码:

在Date.h中:

#pragma once
#include<assert.h>
#include<stdio.h>
#include<iostream>using namespace std;class Date
{
public:// 友函数的声明,外面的函数要访问类里面的对象,需要使用友函数。friend ostream& operator<<(ostream& out, const Date& d);friend istream& operator>>(istream& in, Date& d);int GetMonthDay(int year, int month) const{assert(month > 0 && month < 13);int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month == 2 && ((year % 4 == 0 && year % 400 != 0) || year % 400 == 0))return 29;return monthDayArray[month];}// 只要不改变调用对象的函数都建议加constvoid Print() const;bool CheckDate()const; //检查日期是否合法Date(int year = 1, int month = 1, int day = 1); //构造函数Date(const Date& d);  // 拷贝构造函数Date& operator+=(int day);Date  operator+(int day) const ;Date& operator-=(int day);Date  operator-(int day) const;// ++d1; -> d1.operator++();Date& operator++();// d1++; -> d1.operator++(1);Date operator++(int);// --d1;Date& operator--();//d1;Date operator--(int);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 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中:

#include"Date.h"bool Date::CheckDate() const
{if (_month < 1 || _month > 12|| _day < 1 || _day > GetMonthDay(_year, _month)){return false;}return true;
}// void Print(const Date* const this) const
void Date::Print() const
{cout << _year << "/" << _month << "/" << _day << endl;
}Date::Date(int year, int month, int day) //构造函数
{_year = year;_month = month;_day = day;if (!CheckDate()){cout << "非法日期" << endl;}
}Date::Date(const Date& d)  // 拷贝构造函数
{_year = d._year;_month = d._month;_day = d._day;
}// += 
Date& Date::operator+=(int day)
{_day += day;//进位while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){_month = 1;_year++;}}return *this;
}// + 
Date Date::operator+(int day) const
{Date tmp(*this);  //使用的是默认的拷贝构造tmp += day;return tmp;
}Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){--_month;if (_month == 0){_month = 12;--_year;}_day += GetMonthDay(_year, _month);}return *this;
}// 日期-天数
// d1 - 100
Date Date::operator-(int day) const
{Date tmp(*this);tmp -= day;return tmp;
}//++d1;  -> d1.operator++()
Date& Date::operator++()
{*this += 1;return *this;
}// d1++; -> d1.operator++(int)
Date Date::operator++(int)
{Date tmp(*this);*this += 1;return tmp;
}//--d1
Date& Date::operator--()
{*this -= 1;return *this;
}//d1--
Date Date::operator--(int)
{Date tmp(*this);*this -= 1;return tmp;
}bool Date::operator==(const Date& d) const
{return _year == d._year&& _month == d._month&& _year == d._year;
}
bool Date::operator!=(const Date& d) const
{return !(*this == d);
}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;}return false;
}// d1 <= d2
bool Date::operator<=(const Date& d) const
{return (*this < d || *this == d);
}// d1 > d2
bool Date::operator>(const Date& d) const
{return !(*this <= d);
}// d1 >= d2;
bool Date::operator>=(const Date& d) const
{return !(*this < d);
}// 日期-日期 
//d1 - d
int Date::operator-(const Date& d) const
{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& Date::operator<<(ostream& out)
//{
//	out << this->_year << "年" << this->_month << "月" << this->_day << "日" << endl;
//	return out;
//}ostream& operator<<(ostream& out, const Date& d)
{out << d._year << "年" << d._month << "月" << d._day << "日" << endl;return out;
}istream& operator>>(istream& in, Date& d)
{while (1){cout << "请依次输入年月日:>";in >> d._year >> d._month >> d._day;if (d.CheckDate()){break;}else{cout << "非法日期,请重新输入!" << endl;}}return in;
}

在test.c中:

#include"Date.h"void test1()
{Date d1(2025,1,1);d1.Print();d1 += 10;d1.Print();Date d2(2025, 1, 2);d2 = d1 + 10;d2.Print();d1.Print();}
void test2()
{Date d1(2025, 1, 1);Date d2(2025, 1, 1);d1.Print();d1 -= 10;d2 = d1 - 10;d2.Print();d1.Print();
}void test03()
{Date d1(2025, 1, 1);Date d2(2025, 1, 10);++d1;d1.Print();d2 = d1++;d2.Print();d1.Print();//d1 << cout;cin >> d1 >> d2;cout << d1 << d2;
}int main()
{//test1();//test2();test03();return 0;
}

http://www.cadmedia.cn/news/15342.html

相关文章:

  • 上海企业建站方案网站运营工作的基本内容
  • 优秀手机网站模板图片自动点击器软件
  • 一般网站建设都用什么字体刚刚刚刚刚刚好痛
  • 总部基地网站建设公司域名查询网址
  • 湖南省绿色建筑信息平台全国seo搜索排名优化公司
  • 企业网站模板下载哪里培训机构学校
  • 网站建设需要哪些电商运营的基本流程
  • 52影院seo属于什么职业部门
  • 商品网站建设实验记录收录优美图片app
  • 电影采集网站建设太原百度seo排名
  • 镇江积分优化网站seo报价
  • 网站建设 工作建议站长工具查询官网
  • seo建站要求seo推广优化培训
  • 上海做网站就用乐云seo十年昆明seo案例
  • 网站建设色系搭配培训优化
  • 网站建设支付安全seo怎么学
  • bbs建站站长工具域名
  • 写小说的网站自己做封面网店推广策划方案
  • 最新的新开传奇网站惠州优化怎么做seo
  • 最便宜的重庆网站建设电脑培训班一般多少钱
  • 电子网站开发技术包括多层次网络营销合法吗
  • 查建筑公司资质的网站百度指数排名热搜榜
  • 云南网站推广多合一seo插件破解版
  • 武汉生活网站爱站网长尾关键词挖掘工具电脑版
  • 广西南宁电商网站建设百度seo排名软件
  • 网站制作软件教程郑州网站建设公司排名
  • 婚纱定制网站哪个好软文推广媒体
  • 三鼎网络网站建设百度seo多久能优化关键词
  • 吉林网络公司哪家好关键词优化推广公司
  • 郑州最好的装修设计公司怎么优化关键词