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

外贸营销型网站建设长沙seo霜天

外贸营销型网站建设,长沙seo霜天,做二手车有哪些网站有哪些,怎么做水果网站文章目录 前言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.mmbaike.com/news/107839.html

相关文章:

  • 网站保姆-源码下载长沙关键词优化费用
  • wordpress musicseo排名助手
  • 免费的动态ppt网站网络营销比较成功的企业
  • 青岛开发区网站建设网络营销专业就业方向
  • 主页网站建设app广告联盟平台
  • 重庆小潘seoseo对各类网站的作用
  • 杭州推广平台有哪些seo的基础优化
  • 网站建设费用 发票友链价格
  • 网站上传的工具近两年成功的网络营销案例及分析
  • 标准网站有哪些北京网站排名推广
  • 中国人做外贸网站都卖什么怎样建立自己网站
  • 企业网站开源代码下载app注册推广
  • 网站虚拟主机1g如何创建自己的网址
  • 给个2021站你们懂得不花钱的湖南专业的关键词优化
  • 网站建设个人可口可乐软文范例
  • 网站域名跳转是怎么做的写软文推广
  • 2817网站东莞网站公司
  • 网站的设计原则有哪些百度热搜风云榜
  • 杭州网站建设商城价格青岛网站
  • 手机网站用什么软件做游戏推广员上班靠谱吗
  • 做网站的流程视频互联网广告投放平台加盟
  • 湖南省和城乡住房建设厅网站什么叫网络市场营销
  • 南昌网络营销网站移动端关键词排名优化
  • 长春建站公司网店推广运营策略
  • 网站可信度网络推广方法
  • c 如何做网站廊坊关键词快速排名
  • 如何作做网站百度推广排名代发
  • 手机用什么软件做网站seo基础入门教程
  • 企业建站技术关键词权重如何打造
  • 网站建设推广方法长沙网络营销哪家平台专业