• C++基础——前后置++--,流插入提取运算符重载函数


     前言:

    再来说一说函数运算

            运算符重载函数是用来体现运算符的多态性,在学习C语言的过程中,运算符全都是用来解决内置类型数据、逻辑、关系表达式间的运算,而在学习C++的过程中,我们了解了类与对象,就会思考到自定义类型的对象之间是否也能进行+-*/等多种运算呢?基于此种想法,C++官方研发出了运算符重载函数,就是为了让自定义类型也能做运算,体现出了语言的多态性。

    目录

    前言

    一.前后置++运算重载

    代码视图:

    测试:

    二.前后置--运算重载

    测试:

    三.流插入运算符重载

    代码如图:

    优化后,代码如下:

    测试: 

    此外还有一个问题:

    代码如下: 

    测试: 

    四.流提取运算符重载

    代码如下:


    一.前后置++运算重载

    代码视图:

    1. class Date
    2. {
    3. public:
    4. Date(int year = 1900, int month = 1, int day = 1)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. void Print() {
    11. cout << _year << "-" << _month << "-" << _day << endl;
    12. }
    13. //获取月份天数
    14. int GetMonthDay(int year, int month) {
    15. int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    16. if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
    17. return 29;
    18. }
    19. return MonthDay[month];
    20. }
    21. //+=运算符函数重载
    22. Date& operator+=(int day) {
    23. _day += day;
    24. while (_day > GetMonthDay(_year, _month)) {
    25. _day -= GetMonthDay(_year, _month);
    26. _month++;
    27. if (_month == 13) {
    28. _year++;
    29. _month = 1;
    30. }
    31. } return *this;
    32. }
    33. //前置++运算重载
    34. Date& operator++() {
    35. *this+=1;
    36. return *this;
    37. }
    38. //后置为了和前置区分,在后置上约定加个int
    39. Date operator++(int) {
    40. Date tmp(*this); //调用拷贝构造
    41. *this += 1;
    42. return tmp; //返回临时对象,也要调用拷贝构造
    43. }
    44. private:
    45. int _year;
    46. int _month;
    47. int _day;
    48. };
    49. int main() {
    50. Date d1(2022, 2, 2);
    51. Date d2(d1);
    52. cout << "d1的日期前置结果:" << endl;
    53. (++d1).Print(); //前置++
    54. d1.Print();
    55. cout << "d2的日期后置结果:" << endl;
    56. (d2++).Print();
    57. d2.Print();
    58. return 0;
    59. }

           注意事项:

            使用++运算符重载,需要用到+=的运算重载。

            前置++中,this指针出了作用域已经不在了,但*this(d1)是整个main函数生命周期,仍在,所以用引用返回;而后置++中,tmp是临时拷贝对象,出了作用域不在了,不可用引用返回。

            最后就是C++官方为了区分前置++与后置++的不同,专门为后置++的重载形参处多加了个int,这是约定好的,就好比双方谈好了,已经签了合同了,就不能再改了,所以大家不要以为能够换个char、double、short,这些写法都不对!!! 

     

    函数调用图示如下: 

    测试:

    二.前后置--运算重载

    1. class Date
    2. {
    3. public:
    4. Date(int year = 1900, int month = 1, int day = 1)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. void Print() {
    11. cout << _year << "-" << _month << "-" << _day << endl;
    12. }
    13. //获取月份天数
    14. int GetMonthDay(int year, int month) {
    15. int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    16. if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
    17. return 29;
    18. }
    19. return MonthDay[month];
    20. }
    21. Date& operator-=(int day) {
    22. _day -= day;
    23. while(_day<=0) {
    24. _month--;
    25. _day += GetMonthDay(_year, _month);
    26. if (_month == 0) {
    27. _month = 12;
    28. _year--;
    29. }
    30. }
    31. return *this;
    32. }
    33. Date operator-(int day) {
    34. Date tmp(*this); //调用拷贝构造
    35. tmp -= day;
    36. return tmp; //tmp出了作用域就不见了,所以不能用引用返回
    37. }
    38. Date& operator--() {
    39. *this -= 1;
    40. return *this;
    41. }
    42. Date operator--(int) {
    43. Date tmp(*this); //调用拷贝构造
    44. *this -= 1;
    45. return tmp; //返回时也要用拷贝构造
    46. }
    47. private:
    48. int _year;
    49. int _month;
    50. int _day;
    51. };
    52. int main() {
    53. Date d1(2022, 2, 2);
    54. Date d2(d1);
    55. cout << "d1的日期前置--结果:" << endl;
    56. (--d1).Print(); //前置++
    57. d1.Print();
    58. cout << "d2的日期后置--结果:" << endl;
    59. (d2--).Print();
    60. d2.Print();
    61. return 0;
    62. }

    前后置的--与++同根同理,就不再过多解释了。 

    测试:

            总结:在编写代码的习惯上要常用前置++或前置--,因为后置的总要比前置的多进行两次拷贝,在运行效率上不如前置! 

    三.流插入运算符重载
     

            流插入运算符为 “<<",它与cout一起用,作用就是输出变量的值,若要使用cout,就要包含头文件#include,cout<<它的一般作用也只是支持内置类型的数值输出例:

    1. #include
    2. using namespace std;
    3. int main() {
    4. int a = 10, b = 20;
    5. cout << a<
    6. cout << b << endl;
    7. return 0;
    8. }

    而我们若是要想输出类的对象呢?这时我们便需要对流插入运算符<<做重载了。

            通过之前对赋值运算符=、==、+=、-=、++、--、<等多个运算符重载的学习来看,我们都是把这些运算符重载函数放到类内,好处就是方便用到this指针和成员变量,毕竟若是私有成员,类外不能访问到,所以我们先放在类内看看:

    代码如图:

    1. class Date
    2. {
    3. public:
    4. Date(int year = 1900, int month = 1, int day = 1)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. //流插入运算符重载函数
    11. void operator<<(ostream& out) {
    12. cout << _year << "年" << _month << "月" << _day << "日" << endl;
    13. }
    14. void Print() {
    15. cout << _year << "-" << _month << "-" << _day << endl;
    16. }
    17. private:
    18. int _year;
    19. int _month;
    20. int _day;
    21. };
    22. int main() {
    23. Date d1, d2;
    24. cout << d1; //报错
    25. cout << d2; //报错
    26. d1 << cout; //不报错
    27. d2<< cout; //不报错
    28. return 0;
    29. }

            通过程序的编译,我们发现cout<

    而d1<

            但我们已经写了<<的运算符重载函数了,为什么会出现这种情况?

            之前,我曾经说过,类内的每个成员函数都有一个this指针,只不过它被隐藏起来了,形参中并没有它的身影,而且运算符重载的特性之一就是: 

            注意:成员函数的第一个参数就是隐藏的this指针,那么cout<

    cout<形参不会有this指针,需要把重载函数<<放在类外。

    优化后,代码如下:

    1. class Date
    2. {
    3. public:
    4. Date(int year = 1900, int month = 1, int day = 1)
    5. {
    6. _year = year;
    7. _month = month;
    8. _day = day;
    9. }
    10. void operator<<(ostream& out) {
    11. cout << _year << "年" << _month << "月" << _day << "日" << endl;
    12. }
    13. void Print() {
    14. cout << _year << "-" << _month << "-" << _day << endl;
    15. }
    16. private:
    17. int _year;
    18. int _month;
    19. int _day;
    20. };
    21. void operator<<(ostream& out ,const Date& d) {
    22. cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    23. }
    24. int main() {
    25. Date d1, d2;
    26. cout << d1; //不报错了
    27. cout << d2;
    28. return 0;
    29. }

            但此时有了一个老问题,类外函数对于私有成员无法访问,这时,我简单提一提C++的一个新内容——友元,友元的作用就是让类外的函数能够访问到类内的private、protected型成员,C++规定:类中有这两种访问限定修饰符的成员会把类外的一切函数都当作敌人,只有身上被标识过友元的函数,它们才会认为这是朋友并让其有权限进行访问。这就是友元的作用。

            而成为友元的方式就是在类中写一句友元函数声明! 

            friend void operator<<(ostream& out, const Date& d);

            这样处在类外的operator<<函数就能够对私有成员进行访问了,从而cout<

    测试: 

    此外还有一个问题:

            上面的cout<

    代码如下: 

    1. class Date
    2. {
    3. //友元声明
    4. friend ostream& operator<<(ostream& out, const Date& d);
    5. public:
    6. Date(int year = 1900, int month = 1, int day = 1)
    7. {
    8. _year = year;
    9. _month = month;
    10. _day = day;
    11. }
    12. void operator<<(ostream& out) {
    13. cout << _year << "年" << _month << "月" << _day << "日" << endl;
    14. }
    15. void Print() {
    16. cout << _year << "-" << _month << "-" << _day << endl;
    17. }
    18. private:
    19. int _year;
    20. int _month;
    21. int _day;
    22. };
    23. //有引用返回,支持链式访问
    24. ostream& operator<<(ostream& out, const Date& d) {
    25. cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
    26. return out;
    27. }
    28. int main() {
    29. Date d1(2022,10,11);
    30. Date d2(2020, 12, 12);
    31. Date d3(1999, 1, 15);
    32. cout << d1 <
    33. return 0;
    34. }

    测试: 

    四.流提取运算符重载

            流提取运算符为>>,其写法与operator<<有着异曲同工之妙,它与cin结合使用,而使用cin需要用到头文件#include,所以写法上只需要把ostream改为istream即可。

    代码如下:

    1. class Date
    2. {
    3. friend istream& operator>>(istream& in, Date& d);
    4. public:
    5. Date(int year = 1900, int month = 1, int day = 1)
    6. {
    7. _year = year;
    8. _month = month;
    9. _day = day;
    10. }
    11. void Print() {
    12. cout << _year << "-" << _month << "-" << _day << endl;
    13. }
    14. private:
    15. int _year;
    16. int _month;
    17. int _day;
    18. };
    19. //有引用返回,支持链式访问
    20. istream& operator>>(istream& in, Date& d) {
    21. in >> d._year >> d._month >> d._day;
    22. return in;
    23. }
    24. int main() {
    25. Date d1,d2,d3;
    26. cin >>d1;
    27. //链式访问
    28. cin >>d2 >> d3;
    29. return 0;
    30. }

  • 相关阅读:
    Hbase的SQL接口之Phoenix使用心得
    【Eclipse中的Preferences窗口中没有Server的解决方案】&【Eclipse配置Tomcat】
    如何让Java的线程池顺序执行任务?
    Docker中搭建Elasticsearch+Kibana
    C语言程序设计笔记(浙大翁恺版) 第二周:计算
    怎么压缩视频?视频过大跟我这样压缩
    MySQL 存储过程创建指定表结构
    软件测试 - 软件测试流程(完整版)避免当背锅侠,测试人的生存......
    Java内部类(成员内部类、静态内部类、局部内部类、局部内部类)
    信创之国产浪潮电脑+统信UOS操作系统体验1:硬件及软件常规功能支持情况介绍
  • 原文地址:https://blog.csdn.net/weixin_69283129/article/details/127803427