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

外贸营销型网站建设免费二级域名建站

外贸营销型网站建设,免费二级域名建站,天眼查企业查询在线查询,平台网站建设有哪些方面文章目录 前言1. 实现Date类的构造函数2. 实现Date类的拷贝构造函数3. 实现Date类的赋值运算符重载4. 实现各Date对象之间的比较接口5. 实现Date对象的加减接口6. const成员7. 取地址及const取地址操作符重载 前言 在我们前面学习到了"类和对象"的四大默认成员函数(…

文章目录

  • 前言
  • 1. 实现Date类的构造函数
  • 2. 实现Date类的拷贝构造函数
  • 3. 实现Date类的赋值运算符重载
  • 4. 实现各Date对象之间的比较接口
  • 5. 实现Date对象的加减接口
  • 6. const成员
  • 7. 取地址及const取地址操作符重载

前言

在我们前面学习到了"类和对象"的四大默认成员函数(构造函数、析构函数、拷贝构造函数、赋值运算符重载),这四大默认成员函数也是我们在以后使用"类和对象"这块知识时经常遇到的。本章将会围绕着如何实现一个Date类,来让大家尽快学会编写和更加深刻理解关于"类"封装的思想在实际当中的应用!

本文会分板块逐一讲解,在文章的末尾放有本次实现Date类的全部源码。

哈哈哈

1. 实现Date类的构造函数

所谓的 “Date” 翻译过来就是 “日期” 的意思,那它的成员变量一定是年月日。那我们就可以这么实现Date类的构造函数。

class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}private:int _year; //年int _month; //月int _day; //日
};

这里我们写成全缺省的默认构造函数是十分有讲究的,一方面当我们先使用这个缺省值时,我们就直接有类实例化出对象就行,不需要显式的传递参数;另一方面,当我们想用自己的传递的参数,就直接显式传递函数即可。一举两得。

2. 实现Date类的拷贝构造函数

class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//拷贝构造函数Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}private:int _year;int _month;int _day;
};

这里对于拷贝构造函数的思路没有太多的讲解,只需要大家注意一个点就是,形参一定是对于类类型的引用,避免引发无穷的递归调用。

3. 实现Date类的赋值运算符重载

class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//拷贝构造函数Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}//赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}private:int _year;int _month;int _day;
};

关于赋值运算符重载的返回值,一定是*this,而不是this。因为我们是要返回这个待赋值对象的引用,而this是这个待赋值对象的指针类型为Date* const。还有的人会考虑生命周期的问题,其实这里大可不必担心,虽然this指针的生命周期在这个成员函数中,出了这个作用域就会被销毁,但是我们返回的不是this而是*this*this就是那个待拷贝的对象,所以其的生命周期是在main函数中的!

4. 实现各Date对象之间的比较接口

本次的是实现主要涉及到大小之间的比较,目的是锻炼大家对于运算符重载的用法。

//年份之间的比较大小
class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//拷贝构造函数Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}//年份之间的比较大小bool operator>(const Date& d){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;}bool operator==(const Date& d){return _year == d._year && _month == d._month && _day == d._day;}bool operator!=(const Date& d){return !(*this == d);}bool operator>=(const Date& d){return (*this == d) || (*this > d);}bool operator<=(const Date& d){return (*this == d) || (*this < d);}bool operator<(const Date& d){return !(*this >= d);}//赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}private:int _year;int _month;int _day;
};

这里不仅体现了运算符重载,还体现出了代码复用的重要性!

5. 实现Date对象的加减接口

这里Date类对象的加减是指:年份与年份之间的相减,年份与天数之间的相减,年份与天数的相加。

class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//拷贝构造函数Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}Date operator+(int day) const{Date tmp = *this;tmp._day += day;//我们还要考虑一下天数相加会导致的 月进位 甚至是 年进位//所以我们得获取每个月的天数while (tmp._day > tmp.GetMonthDay()){tmp._day -= tmp.GetMonthDay();tmp._month++;if (tmp._month > 12){tmp._year++;tmp._month = 1;}}return tmp;}Date& operator+=(int day){_day += day;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return *this;}Date& operator-=(int day){_day -= day;while (_day <= 0){--_month;if (_month < 1){_month = 12;--_year;}_day += GetMonthDay();}return *this;}//赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}Date& operator++(){_day += 1;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return *this;}Date operator++(int){Date tmp = *this;_day += 1;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return tmp;}private:int GetMonthDay(){int month_day[] = { 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;}return month_day[_month];}private:int _year;int _month;int _day;
};

这个可以很好的锻炼大家对于前置++和后置++的写法。

到这里Date类我们就全部实现完毕了,是不是很简单呢。一下就是Date类实现全部的源码:

class Date
{
public://全缺省的默认构造函数Date(int year = 1, int month = 1, int day = 1){_year = year;_month = month;_day = day;}//拷贝构造函数Date(const Date& d){_year = d._year;_month = d._month;_day = d._day;}Date operator+(int day) const{Date tmp = *this;tmp._day += day;//我们还要考虑一下天数相加会导致的 月进位 甚至是 年进位//所以我们得获取每个月的天数while (tmp._day > tmp.GetMonthDay()){tmp._day -= tmp.GetMonthDay();tmp._month++;if (tmp._month > 12){tmp._year++;tmp._month = 1;}}return tmp;}//年份之间的比较大小bool operator>(const Date& d){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;}bool operator==(const Date& d){return _year == d._year && _month == d._month && _day == d._day;}bool operator!=(const Date& d){return !(*this == d);}bool operator>=(const Date& d){return (*this == d) || (*this > d);}bool operator<=(const Date& d){return (*this == d) || (*this < d);}bool operator<(const Date& d){return !(*this >= d);}//赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}Date& operator+=(int day){_day += day;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return *this;}Date& operator++(){_day += 1;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return *this;}Date operator++(int){Date tmp = *this;_day += 1;while (_day > GetMonthDay()){_day -= GetMonthDay();++_month;if (_month > 12){++_year;_month = 1;}}return tmp;}Date& operator-=(int day){_day -= day;while (_day <= 0){--_month;if (_month < 1){_month = 12;--_year;}_day += GetMonthDay();}return *this;}//赋值运算符重载Date& operator=(const Date& d){_year = d._year;_month = d._month;_day = d._day;return *this;}private:int GetMonthDay(){int month_day[] = { 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;}return month_day[_month];}private:int _year;int _month;int _day;
};

6. const成员

将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改

函数

#include<iostream>
using namespace std;class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}void Print(){cout << "Print()" << endl;cout << "year:" << _year << endl;cout << "month:" << _month << endl;cout << "day:" << _day << endl << endl;}void Print() const{cout << "Print()const" << endl;cout << "year:" << _year << endl;cout << "month:" << _month << endl;cout << "day:" << _day << endl << endl;}
private:int _year; // 年int _month; // 月int _day; // 日
};
void Test()
{Date d1(2022, 1, 13);d1.Print();const Date d2(2022, 1, 13);d2.Print();
}int main()
{Test();return 0;
}

图片

请思考下面的几个问题:

  1. const对象可以调用非const成员函数吗?

不可以,因为权限被放大了。

  1. 非const对象可以调用const成员函数吗?

可以,因为权限被缩小了。

  1. const成员函数内可以调用其它的非const成员函数吗?

不可以,因为权限被放大了

  1. 非const成员函数内可以调用其它的const成员函数吗?

可以,因为权限被缩小了。

那这里我们就可以根据这个const成员进一步优化我们的Date类的实现!

7. 取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成

class Date
{
public:Date* operator&(){return this;}const Date* operator&() const{return this;}private:int _year;int _month;int _day;
};

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如不想让别人获取到指定的内容的地址
我们就可以改造上面的函数:

class Date
{
public:Date* operator&(){return nullptr;}const Date* operator&() const{return nullptr;}private:int _year;int _month;int _day;
};

好了本文到这里就结束了。

如果觉得本文写的还不错的话,麻烦给偶点个赞吧!!!

哈哈哈

http://www.hrbkazy.com/news/31225.html

相关文章:

  • php记录网站访问次数宁波seo关键词
  • 广西桂林最新事件兴安盟新百度县seo快速排名
  • 大型做网站的公司中国营销型网站有哪些
  • 网站的制作成品百度关键词seo外包
  • 门户网站的建设目的网页设计大作业
  • 虚拟机网站建设广州做seo整站优化公司
  • 礼品网站如何做发外链比较好的平台
  • 做景观设施的网站成都百度网站排名优化
  • 开发区网站建设工作职责郑州seo线下培训
  • 怎么样备份网站数据aso优化吧
  • 网站移动站百度信息流广告投放
  • 嘉兴营销型网站建设网站推广网络营销
  • 建网站用什么工具网页设计与制作个人网站模板
  • wordpress单页主题制作视频教程网络seo外包
  • 做网站页面大小多大seopc流量排名官网
  • 宁波网站建设培训哪家好如何制作一个网页页面
  • wordpress回复显示插件广州seo做得比较好的公司
  • 做好网站建设静态化恶意点击软件哪个好
  • 专业的网站开发公司网络推广外包
  • 网站banner怎么更换预测2025年网络营销的发展
  • 做直播券的网站有多少互动营销的概念
  • 做网站怎么分手机版和电脑版sem竞价专员是干什么的
  • 长安区网站建设东莞网站排名提升
  • 做网站的图片尺寸怎么设定免费seo在线工具
  • 大型建站网站吉林seo关键词
  • 做网站累吗知乎营销推广
  • 网站营销平台代理商要怎么做网络推广
  • 网上制作seo的含义
  • 怎么做网站策划网站seo排名免费咨询
  • 网站后台模版爱站网长尾关键词挖掘查询工具