如何在网站后台找到死链接,条友网,湖南优化推广,电影网站如何做seo优化个人主页#xff1a;点我进入主页 专栏分类#xff1a;C语言初阶 C语言进阶 数据结构初阶 Linux C初阶 算法 欢迎大家点赞#xff0c;评论#xff0c;收藏。 一起努力#xff0c;一起奔赴大厂 目录 一.前言
二.运算符重载
2.1概念
2.2比较的符号重载
2.2.1… 个人主页点我进入主页 专栏分类C语言初阶 C语言进阶 数据结构初阶 Linux C初阶 算法 欢迎大家点赞评论收藏。 一起努力一起奔赴大厂 目录 一.前言
二.运算符重载
2.1概念
2.2比较的符号重载
2.2.1 operator
2.2.2其余的符号重载
2.3-类实现
2.3.1operator和operator的比较
2.3.2其余的符号重载
2.4前置后置
2.5重载和
2.6类的代码
三.const成员
四.总结 一.前言 我们在前面写了关于类的实现这时候有人问到怎末实现类里边元素的运算呢比如日期类我们怎末实现日期类的基本运算呢我们知道类的成员变量是不能在类的外边进行访问的我们就选算是想进行变量的运算也不能实现那我们应该如何实现呢我们可以封装成函数来实现祖师爷对这进行了修改出现了我们的operator。那是如何实现的呢
二.运算符重载
2.1概念 C为了增强代码的可读性引入了运算符重载运算符重载是具有特殊函数名的函数也具有其返回值类型函数名字以及参数列表其返回值类型与参数列表与普通的函数类似。函数名字为关键字operator后面接需要重载的运算符符号。函数原型返回值类型 operator操作符(参数列表)。
不能通过连接其他符号来创建新的操作符比如operator重载操作符必须有一个类类型参数用于内置类型的运算符其含义不能改变例如内置的整型不 能改变其含义作为类成员函数重载时其形参看起来比操作数数目少1因为成员函数的第一个参数为隐藏的this.* :: sizeof ?: . 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。
我们的类的代码为
class Data {
public:Data(int year 1, int month 1, int day 1){_year year;_month month;_day day;}Data(const Data d){_year d._year;_month d._month;_day d._day;}
private:int _year;int _month;int _day;};
我们的运算符重载函数函数名放在public中我们采用声明和定义分离的形式大致的形式最后实现的类的代码我会在左后给出。
2.2比较的符号重载
2.2.1 operator
在这里我给出一个样例后面的和这个相同我会直接给出全部的代码我们的代码为
bool Data::operator(const Data d)
{return _year d._year _month d._month _day d._day;}
这个代码是判断两个是否相同我们运行的代码为 Data d1(2014, 2, 5);Data d2(d1);d2.operator(d1);d2 d1;
其中第一种是用d2的函数的形式在设计这一块时祖师爷还支持了我们直接用。当我们运行到这一步时会自动跳转到我们定义的函数的位置。
2.2.2其余的符号重载
bool Data::operator (const Data d)
{if (_year d._year) return true;else if (_year d._year) return false;else{if (_month d._month)return true;else if (_month d._month)return false;else{if (_day d._day)return true;else return false;}}
}
bool Data::operator (const Data d)
{return (*this d) || (*this d);
}
bool Data::operator (const Data d)
{return !(*this d);
}
bool Data::operator (const Data d)
{return !(*this d);
}
bool Data::operator !(const Data d)
{return !(*this d);
}
看到后面是不是感觉很爽使用的方法和上面的相同都是有两种。
2.3-类实现
2.3.1operator和operator的比较 在写代码之前我们需要知道和的含义返回一个天数自身不改变是让本身改变这时候我们有两种写法一种是先写在写另一种是先写再写两种方案我会一一给出然后进行比较。
先写再写代码如下
Data Data::operator(int day)
{Data tmp(*this);tmp._day day;while (GetMonthDay(tmp._year ,tmp._month) tmp._day){tmp._day - GetMonthDay(tmp._year, tmp._month);tmp._month;if (tmp._month 13){tmp._year;tmp._month 1;}}return tmp;
}
Data Data::operator(int day)
{*this *this day;return *this;
}
先写再写代码如下
Data Data::operator(int day)
{ Data tmp(*this);tmp day;return tmp;
}
Data Data::operator(int day)
{_day day;while (GetMonthDay(_year, _month) _day){_day - GetMonthDay(_year, _month);_month;if (_month 13){_year;_month 1;}}return *this;
} 我们对比第一种用了一个临时变量第二个用了一个临时变量时左边没有用临时变量右边用了一个我们知道调用临时变量我们还需要调用拷贝构造所以我们选用先写再写。我们针对返回值进行解说我们知道应该符合连续的比如123所以我们的日期类应该也满足我们的连续123的底层是先计算23得到的值为一个临时变量临时变量在和1加得到临时变量为结果所以我们需要返回我们的Data类型我们知道传参不如传引用快但是由于我们返回的是一个临时变量不能传引用因为函数结束内存释放所以我们只能返回值这个值会村早寄存器。我们看由于会改变我们的值且返回的是我们的Data类型所以我们采用Data来返回引用。
2.3.2其余的符号重载
Data Data::operator-(int day)
{Data tmp(*this);tmp day;return tmp;
}
Data Data::operator-(int day)
{_day - GetMonthDay(_year, _month);while (_day 0){_month--;if (_month 0){_year--;_month 12;}_day GetMonthDay(_year, _month);}return *this;
}2.4前置后置 我们的前置和后置符号都是我们的operator的规则是在opertaor的后面加上我们要重载的符号由于我们的前置后置符号一样所以祖师爷采用将后置的参数部分加上一个int和前置构成函数重载。前置--和后置--与前置和后置操作一样这里不做具体解释。我们直接上代码
Data Data::operator()
{*this 1;return *this;
}
Data Data::operator(int)
{Data tmp(*this);*this 1;return tmp;
}
2.5重载和
既然支持符号重载那我们的和也应该支持我们直接上代码
ostream Data::operator (ostream out)
{out _year / _month / _day;return out;
}
istream Data::operator(istream in)
{in _year _month _day;return in;
}
当我们写在类里面时我们想用时需要写成 d2 cout;d2 cin;
这个和我们的写法相仿我们需要用其他的形式才行这需要我们的友元操作下一篇文章我会给大家进行讲解。
2.6类的代码
class Data {
public:Data(int year 1, int month 1, int day 1){_year year;_month month;_day day;}Data(const Data d){_year d._year;_month d._month;_day d._day;}bool operator(const Data d);bool operator (const Data d);bool operator (const Data d);bool operator (const Data d);bool operator (const Data d);bool operator !(const Data d);int GetMonthDay(int year, int month);Data operator(int day);Data operator(int day);Data operator-(int day);Data operator-(int day);Data operator();Data operator(int);ostream operator (ostream out);istream operator(istream in);private:int _year;int _month;int _day;};
三.const成员
当我们在类里面写一个print函数时我们时写成 void Print(){cout _year / _month / _day;}
但是这是对权限的一种扩大我们这个函数应该满足不能修改我们的值但是这个函数却可以修改我们的值这个时候我们出现了我们的const应该如何写呢我们看代码在声明时写成
void Print() const;
定义写为
void Data::Print() const
{cout _year / _month / _day;
}
四.总结
今天的内容就结束了主要就是对运算符的重载的详细解析可以多体会体会其中的含义最后希望大家可以一键三连。