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

有名网站建设公司拓客渠道有哪些

有名网站建设公司,拓客渠道有哪些,手机微信营销软件,wordpress主题 秀个人主页点击直达:小白不是程序媛 C系列专栏:C头疼记 目录 前言: 运算符重载 运算符重载 赋值运算符重载 前置和后置重载 const成员 取地址及const取地址操作符重载 使用函数操作符重载完成日期类的实现 前言: 上篇文…

=========================================================================

个人主页点击直达:小白不是程序媛

C++系列专栏:C++头疼记

=========================================================================

目录 

前言:

运算符重载

运算符重载 

赋值运算符重载

前置++和后置++重载

const成员

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

使用函数操作符重载完成日期类的实现


前言:

上篇文章介绍了在C++的类六个成员函数中的三个,分别是构造函数、析构函数、拷贝构造函数,不知道大家有没有所收获,今天我们带来的是剩下的三个函数,以及结合这六个函数完成一个完整的日期类的实现,让我们开始今天的征程吧!


运算符重载

在C++中有很多的运算符,包括 +、- 、* 、/、等等,一个两两结合的操作符++、--、+=,>=、==等等。

int main()
{int i = 0;cout << ++i << endl;cout << --i << endl;i = 2;cout << i << endl;return 0;
}

对于内置类型来说我们可以直接使用,但是对于我们自己定义的自定义类型呢?该如何使用呢?

  • 运算符重载 

C++为了增强代码的可读性引入了运算符重载运算符重载是具有特殊函数名的函数,也具有其
返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表) 

注意:

  • 不能通过连接其他符号来创建新的操作符:比如operator@
  • 重载操作符必须有一个类类型参数
  • 用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
  • 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
  • .*  ::  sizeof  ?:  . 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。

示例:==运算符重载

class Date
{
public:Date(int year = 1, int month = 1,int day=1){_year = year;_month = month;_day = day;}bool operator==(const Date& y){return _year == y._year && _month == y._month && _day == y._day;}
private :int _year;int _month;int _day;
};
  • 赋值运算符重载

赋值运算符重载格式

参数类型:const T&,传递引用可以提高传参效率
返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值检测是否自己给自己赋值
返回*this :要复合连续赋值的含义

class Date
{
public:Date(int year = 1, int month = 1,int day=1){_year = year;_month = month;_day = day;}Date& operator=(const Date& d){this->_year = d._year;this->_month = d._month;this->_day = d._day;return *this;}
private :int _year;int _month;int _day;
};

赋值运算符只能重载成类的成员函数不能重载成全局函数

class Date
{
public:Date(int year = 1900, int month = 1, int day = 1){_year = year;_month = month;_day = day;}int _year;int _month;int _day;
};
// 赋值运算符重载成全局函数,注意重载成全局函数时没有this指针了,需要给两个参数
Date& operator=(Date& left, const Date& right)
{if (&left != &right){left._year = right._year;left._month = right._month;left._day = right._day;}return left;
}

 原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现
一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值
运算符重载只能是类的成员函数。

用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。

注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符
重载完成赋值。

class Date
{
public:void Print(){cout << _year << "-" << _month << "-" << _day << endl;}Date(int year = 1, int month = 1,int day=1){_year = year;_month = month;_day = day;}
private :int _year;int _month;int _day;
};
int main()
{Date d1;d1.Print();Date d2 = d1;d2.Print();return 0;
}

这里我们没有写赋值重载函数,编译器会自动生成一个完成操作,和拷贝构造函数优点类似。

注意:如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必
须要实现。

  • 前置++和后置++重载

class Date
{
public:void Print(){cout << _year << "-" << _month << "-" << _day << endl;}Date(int year = 1, int month = 1,int day=1){_year = year;_month = month;_day = day;}// 前置++:返回+1之后的结果// 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率Date& operator++(){*this += 1;return *this;}// 后置++:// 前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载// C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递// 注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存一份,然后给this + 1//    而temp是临时对象,因此只能以值的方式返回,不能返回引用Date operator++(int){Date tmp(*this);*this += 1;return tmp;}
private :int _year;int _month;int _day;
};
int main()
{Date d1;d1++;// 1-1-2++d1;// 1-1-1return 0;
}

const成员

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

我们来看看下面这段代码 

class Date
{
public://void Print( Date *this)void Print(){cout << _year << "-" << _month << "-" << _day << endl;}
private:int _year = 1;int _month = 1;int _day = 1;
};
int main()
{const Date d1;//d1.Print(&d1)//权限放大d1.Print();return 0;
}

这段代码在编译器上是编译不通过的,说明语法有问题。

因为我们创建了一个const Date *的d1,将d1作为参数传给函数时,函数的的参数是一个Date *,两个参数不匹配,权限会放大,所以编译不通过。

其实函数的形参就是this指针,那我们如何将this指针修饰为const呢?

只需要在函数声明后面加上const 即可将this指针修饰为const。

class Date
{
public://void Print( Date *this)void Print(){cout << _year << "-" << _month << "-" << _day << endl;}
private:int _year = 1;int _month = 1;int _day = 1;
};
int main()
{const Date d1;//d1.Print(&d1)//权限放大d1.Print();Date d2;d2.Print();return 0;
}

上面为修饰后的代码,我们在创建一个普通的Date *,能否在const修饰的this指针函数中跑起来呢?

答案是可以的,因为权限可以平移、缩小,不能放大。

那么请思考以下问题:

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

        不可以,权限放大。
非const对象可以调用const成员函数吗?

        可以,权限缩小。
const成员函数内可以调用其它的非const成员函数吗?

        不可以,权限放大。
非const成员函数内可以调用其它的const成员函数吗? 

        可以,权限缩小。


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

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

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

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

这个函数基本没有什么作用,很少用到作为了解即可!


使用函数操作符重载完成日期类的实现

void Date::Print()
{cout << _year << "-" << _month << "-" << _day << endl;
}
//获取天数
int Date::GetMonthDay(int year, int month)
{int monthArr[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;}return monthArr[month];
}
//构造函数
Date::Date(int year=1, int month=1, int day=1)
{_year = year;_month = month;_day = day;
}
//拷贝构造函数
Date::Date(const Date& d)
{this->_year = d._year;this->_month = d._month;this->_day = d._day;
}
//赋值运算符重载
Date& Date::operator=(const Date& d)
{this->_year = d._year;this->_month = d._month;this->_day = d._day;return *this;
}
//日期+=天数
Date& Date::operator+=(int day)
{_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);++_month;if (_month == 13){_year++;_month = 1;}}return *this;
}
//日期+天数
Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return *this;
}
//日期-=天数
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){//月小于0时应该先借位--_month;if (_month == 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}
//日期-天数
Date Date::operator-(int day)
{Date tmp(*this);tmp -= day;return *this;
}
//前置++
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;
}
//>运算符重载
bool Date::operator>(const Date& y)
{if (_year > y._year){return true;}else if (_year == y._year && _month > y._month){return true;}else if (_year == y._year && _month == y._month && _day > y._day){return true;}return false;
}
//==运算符重载
bool Date::operator==(const Date& y)
{return _year == y._year && _month == y._month && _day == y._day;
}
//>=运算符重载
bool Date::operator>=(const Date& y)
{return *this > y || *this == y;
}
//<运算符重载
bool Date::operator<(const Date& y)
{return !(*this >= y);
}
//!=运算符重载
bool Date::operator!=(const Date& y)
{return !(*this == y);
}
//日期-日期 返回天数
int Date::operator-(const Date& y)
{Date max = *this;Date min = y;if (*this < y){max = y;min = *this;}int n = 0;while (min != max){min++;n++;}return n;
}

这里我们使用操作符重载,完成了一个简单的日期计算器,当然这其中还有很多没有完善的地方。像我们构造一些非法的数据作为日期等等一些小问题,我就留给大家了。

C++类和对象(中)的六个成员函数就讲完了,大家可以结合上篇文章细细阅读,慢慢探索C++这六个成员函数的奥秘 ,总结收获出一些自己的东西。也希望大家留言指出我文章中出现的内容,同时也感谢各位看官的三连支持,你们的支持就是我更新的动力!!! 


下篇预告:类和对象(下)


文章转载自:
http://gerundial.jnpq.cn
http://hydrodynamicist.jnpq.cn
http://sclerodermous.jnpq.cn
http://moola.jnpq.cn
http://cheliceral.jnpq.cn
http://diastasis.jnpq.cn
http://wisperer.jnpq.cn
http://haliver.jnpq.cn
http://careless.jnpq.cn
http://obsequence.jnpq.cn
http://lifemanship.jnpq.cn
http://rhinosporidiosis.jnpq.cn
http://saluretic.jnpq.cn
http://reset.jnpq.cn
http://firstcomer.jnpq.cn
http://samoyedic.jnpq.cn
http://twentymo.jnpq.cn
http://concessively.jnpq.cn
http://tamarisk.jnpq.cn
http://cutlery.jnpq.cn
http://abba.jnpq.cn
http://osteoblast.jnpq.cn
http://cribrose.jnpq.cn
http://indue.jnpq.cn
http://englishness.jnpq.cn
http://zoon.jnpq.cn
http://maoist.jnpq.cn
http://furthermore.jnpq.cn
http://pendant.jnpq.cn
http://blockship.jnpq.cn
http://platinocyanide.jnpq.cn
http://extramolecular.jnpq.cn
http://unconscious.jnpq.cn
http://decimalist.jnpq.cn
http://azeotropic.jnpq.cn
http://hydrostatics.jnpq.cn
http://sulfite.jnpq.cn
http://calorific.jnpq.cn
http://volatilization.jnpq.cn
http://naxian.jnpq.cn
http://recommendatory.jnpq.cn
http://provenance.jnpq.cn
http://xanthopathia.jnpq.cn
http://naggish.jnpq.cn
http://fictioneering.jnpq.cn
http://actomyosin.jnpq.cn
http://bandwidth.jnpq.cn
http://orchid.jnpq.cn
http://fontainebleau.jnpq.cn
http://voltmeter.jnpq.cn
http://unseeing.jnpq.cn
http://postponed.jnpq.cn
http://gangrene.jnpq.cn
http://battlewagon.jnpq.cn
http://insuppressive.jnpq.cn
http://shrinkproof.jnpq.cn
http://anticapitalist.jnpq.cn
http://tamburlaine.jnpq.cn
http://tangential.jnpq.cn
http://collectedly.jnpq.cn
http://manners.jnpq.cn
http://wickedness.jnpq.cn
http://quilled.jnpq.cn
http://tectonism.jnpq.cn
http://presentient.jnpq.cn
http://lophobranch.jnpq.cn
http://theft.jnpq.cn
http://phentolamine.jnpq.cn
http://esro.jnpq.cn
http://cornflakes.jnpq.cn
http://revolutionology.jnpq.cn
http://tetchy.jnpq.cn
http://nonsignificant.jnpq.cn
http://crabhole.jnpq.cn
http://redressal.jnpq.cn
http://menage.jnpq.cn
http://enjoyable.jnpq.cn
http://menispermaceous.jnpq.cn
http://chungking.jnpq.cn
http://startle.jnpq.cn
http://claustrophobic.jnpq.cn
http://lustreless.jnpq.cn
http://vulpecula.jnpq.cn
http://trap.jnpq.cn
http://sporadosiderite.jnpq.cn
http://hallowed.jnpq.cn
http://monochromist.jnpq.cn
http://foxpro.jnpq.cn
http://zitherist.jnpq.cn
http://assimilatory.jnpq.cn
http://whirlaway.jnpq.cn
http://lionlike.jnpq.cn
http://choreiform.jnpq.cn
http://daltonist.jnpq.cn
http://vascar.jnpq.cn
http://illogically.jnpq.cn
http://evaporator.jnpq.cn
http://scientize.jnpq.cn
http://sprowsie.jnpq.cn
http://swash.jnpq.cn
http://www.hrbkazy.com/news/61714.html

相关文章:

  • 宝鸡市网站建设整站优化全网营销
  • 营销伎巧第一季整站优化加盟
  • wordpress采集电影资源关键词首页排名优化平台
  • 自己做的网站访问不了学生个人网页优秀模板
  • 做问卷赚钱最好似网站培训机构推荐
  • 电影网站建设需求分析关键词
  • 网站开发机构长沙百度快照优化排名
  • kocool网站开发开源seo软件
  • 如何做彩票网站的源码如何建造一个网站
  • 网站策划过程东莞网站建设优化推广
  • 新闻网站建设公司seo系统推广
  • 扫码支付做进商城网站网络推广怎么推广
  • 怎么优化自己的网站网站seo博客
  • wordpress支付无效seo关键词智能排名
  • 小说网站 做百度联盟企业宣传标语
  • 怎么通过做网站来赚钱网络营销的发展历程
  • 网站做推广 建设哪种类型合适太原百度推广开户
  • 永济微网站建设费用今日头条号官网
  • 如何制作网站的步骤销售怎么做
  • 帝国cms如何做网站软文推广经典案例
  • wordpress主题qux_v7.1山西seo基础教程
  • 鲅鱼圈网站在哪做软件开发自学步骤
  • 上海自适应网站推广软件下载
  • 棋牌游戏网站模板下载安装宁德市中医院
  • 站长工具平台小说排行榜2020前十名
  • 怎样知道哪个网站做推广好抖音关键词优化排名
  • 弹幕网站开发代码百度热搜关键词
  • 济南网站建设开发公司西安网络推广公司大全
  • 做效果图比较好的模型网站windows7系统优化工具
  • 医学院英文网站建设方案东莞seo管理