当前位置:   article > 正文

移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——2.类和对象(中(2))

移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——2.类和对象(中(2))

5. 赋值运算符重载

5.1 运算符重载

• 重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多。⼀元运算符有⼀个参数,⼆元 运算符有两个参数,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。

• 如果⼀个重载运算符函数是成员函数,则它的第⼀个运算对象默认传给隐式的this指针,因此运算 符重载作为成员函数时,参数⽐运算对象少⼀个。

• 运算符重载以后,其优先级和结合性与对应的内置类型运算符保持⼀致。

• 不能通过连接语法中没有的符号来创建新的操作符:⽐如operator@。

.*  ::  sizeof  ?:  . 注意以上5个运算符不能重载。(选择题⾥⾯常考,⼤家要记⼀ 下)

• 重载操作符⾄少有⼀个类类型参数,不能通过运算符重载改变内置类型对象的含义,如: int operator+(int x, int y)

• ⼀个类需要重载哪些运算符,是看哪些运算符重载后有意义,⽐如Date类重载operator-就有意 义,但是重载operator+就没有意义。

• 重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,⽆法很好的区分。 C++规定,

后置++重载时,增加⼀个int形参,跟前置++构成函数重载,⽅便区分。

  1. #include<iostream>
  2. using namespace std;
  3. class Date
  4. {
  5. public:
  6. Date(int year = 1, int month = 1, int day = 1)
  7. {
  8. _year = year;
  9. _month = month;
  10. _day = day;
  11. }
  12. void Print()
  13. {
  14. cout << _year << "-" << _month << "-" << _day << endl;
  15. }
  16. bool operator==(const Date& d)
  17. {
  18. return _year == d._year
  19. && _month == d._month
  20. && _day == d._day;
  21. }
  22. Date& operator++()
  23. {
  24. cout << "前置++" << endl;
  25. *this += 1;
  26. return *this;
  27. }
  28. Date operator++(int)
  29. {
  30. date tmp(*this);
  31. * this += 1;
  32. return tmp;
  33. }
  34. private:
  35. int _year;
  36. int _month;
  37. int _day;
  38. };
  39. int main()
  40. {
  41. Date d1(2024, 7, 5);
  42. Date d2(2024, 7, 6);
  43. // 运算符重载函数可以显⽰调⽤
  44. d1.operator==(d2);
  45. // 编译器会转换成 d1.operator==(d2);
  46. d1 == d2;
  47. // 编译器会转换成 d1.operator++();
  48. ++d1;
  49. // 编译器会转换成 d1.operator++(0);
  50. d1++;
  51. return 0;
  52. }

 5.2 赋值运算符重载

赋值运算符重载是⼀个默认成员函数,⽤于完成两个已经存在的对象直接的拷⻉赋值,这⾥要注意跟 拷⻉构造区分,拷⻉构造⽤于⼀个对象拷⻉初始化给另⼀个要创建的对象。

赋值运算符重载的特点:

1. 赋值运算符重载是⼀个运算符重载,规定必须重载为成员函数。赋值运算重载的参数建议写成 const 当前类类型引⽤,否则会传值传参会有拷⻉

2. 有返回值,且建议写成当前类类型引⽤,引⽤返回可以提⾼效率,有返回值⽬的是为了⽀持连续赋 值场景。

3. 没有显式实现时,编译器会⾃动⽣成⼀个默认赋值运算符重载,默认赋值运算符重载⾏为跟默认拷 ⻉构造函数类似,对内置类型成员变量会完成值拷⻉/浅拷⻉(⼀个字节⼀个字节的拷⻉),对⾃定义 类型成员变量会调⽤他的赋值重载函数。 (和其他成员函数一样)

  1. class Date
  2. {
  3. public:
  4. Date(int year = 1, int month = 1, int day = 1)
  5. {
  6. _year = year;
  7. _month = month;
  8. _day = day;
  9. }
  10. Date(const Date& d)
  11. {
  12. cout << " Date(const Date& d)" << endl;
  13. _year = d._year;
  14. _month = d._month;
  15. _day = d._day;
  16. }
  17. // 传引⽤返回减少拷⻉
  18. // d1 = d2;
  19. Date& operator=(const Date& d)
  20. {
  21. // 不要检查⾃⼰给⾃⼰赋值的情况
  22. if (this != &d)
  23. {
  24. _year = d._year;
  25. _month = d._month;
  26. _day = d._day;
  27. }
  28. // d1 = d2表达式的返回对象应该为d1,也就是*this
  29. return *this;
  30. }
  31. void Print()
  32. {
  33. cout << _year << "-" << _month << "-" << _day << endl;
  34. }
  35. private:
  36. int _year;
  37. int _month;
  38. int _day;
  39. };
  40. int main()
  41. {
  42. Date d1(2024, 7, 5);
  43. Date d2(d1);
  44. Date d3(2024, 7, 6);
  45. d1 = d3;
  46. // 需要注意这⾥是拷⻉构造,不是赋值重载
  47. // 请牢牢记住赋值重载完成两个已经存在的对象直接的拷⻉赋值
  48. // ⽽拷⻉构造⽤于⼀个对象拷⻉初始化给另⼀个要创建的对象
  49. Date d4 = d1;
  50. return 0;
  51. }

5.3 日期类的实现 

date.h:

  1. #include<iostream>
  2. using namespace std;
  3. class date
  4. {
  5. public:
  6. date(int year = 1, int month = 1, int day = 1);//全缺省的初始化,定义处需要给出数据
  7. void print();
  8. int getmonthday(int year, int month);
  9. date& operator=(const date& d); //赋值运算符重载
  10. bool operator<(const date& d);
  11. bool operator==(const date& d);
  12. //
  13. bool operator>(const date& d);
  14. bool operator!=(const date& d);
  15. bool operator<=(const date& d);
  16. bool operator>=(const date& d);
  17. date& operator+=(int day);
  18. date operator+(int day);
  19. date& operator-=(int day);
  20. date operator-(int day);
  21. /
  22. date& operator++(); //前置++
  23. date operator++(int); //后置++
  24. date& operator--(); //前置--
  25. date operator--(int); //后置--
  26. int operator-(const date& d);
  27. int deal(int year, int month, int day);
  28. private:
  29. int _year;
  30. int _month;
  31. int _day;
  32. };

date.cpp :

  1. #include"date.h"
  2. date::date(int year, int month, int day) //声明处可以不给
  3. {
  4. _year = year;
  5. _month = month;
  6. _day = day;
  7. if (month < 1 || month>12 || year < 1 || day<1 || day>getmonthday(year, month))
  8. cout << "非法日期" << endl;
  9. } //1.构造函数
  10. void date::print()
  11. {
  12. cout << _year << "年" << _month << "月" << _day << "日" << endl;
  13. }
  14. int date::getmonthday(int year, int month)
  15. {
  16. int arrday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
  17. if (month==2&&(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
  18. return 29;
  19. else
  20. return arrday[month];
  21. }
  22. date& date::operator=(const date& d)
  23. {
  24. this->_year = d._year;
  25. this->_month = d._month;
  26. this->_day = d._day;
  27. return *this;
  28. }//赋值运算符重载 //如果return的元素出了作用域还在,那么可以用引用返回&,否则不可用
  29. bool date:: operator<(const date& d)
  30. {
  31. if (_year < d._year)
  32. return true;
  33. else if (_year == d._year && _month < d._month)
  34. return true;
  35. else if (_year == d._year && _month == d._month && _day < d._day)
  36. return true;
  37. else
  38. return false;
  39. }
  40. bool date:: operator==(const date& d)
  41. {
  42. if (_year == d._year && _month == d._month && _day == d._day)
  43. return true;
  44. else
  45. return false;
  46. }
  47. bool date:: operator>(const date& d)
  48. {
  49. return !(*this < d || *this == d);
  50. }
  51. bool date:: operator!=(const date& d)
  52. {
  53. return !(*this == d);
  54. }
  55. bool date:: operator<=(const date& d)
  56. {
  57. return !(*this > d);
  58. }
  59. bool date:: operator>=(const date& d)
  60. {
  61. return !(*this < d); //this为 Date* const 类型
  62. }
  63. date& date:: operator+=(int day)
  64. {
  65. _day = _day + day;
  66. while (_day > getmonthday(_year, _month))
  67. {
  68. _day = _day - getmonthday(_year, _month);
  69. _month++;
  70. if (_month == 13)
  71. {
  72. _year++;
  73. _month = 1;
  74. }
  75. }
  76. return *this;
  77. }
  78. date date:: operator+(int day)
  79. {
  80. date tmp(*this); //d1的值不能改变,用拷贝构造
  81. tmp += day;
  82. return tmp;
  83. }
  84. date& date:: operator-=(int day)
  85. {
  86. _day = _day - day+1;
  87. while (_day <=0 )
  88. {
  89. _day = _day + getmonthday(_year, _month);
  90. _month--;
  91. if (_month == 0)
  92. {
  93. _year--;
  94. _month = 12;
  95. }
  96. }
  97. return *this;
  98. }
  99. date date:: operator-(int day)
  100. {
  101. date tmp(*this); //d1的值不能改变,用拷贝构造
  102. tmp -= day;
  103. return tmp; //出作用域销毁,不能用&
  104. }
  105. date& date:: operator++()
  106. {
  107. *this += 1;
  108. return *this;
  109. }
  110. date date::operator++(int)
  111. {
  112. date tmp(*this);
  113. * this += 1;
  114. return tmp;
  115. }
  116. date& date:: operator--()
  117. {
  118. *this -= 1;
  119. return *this;
  120. }
  121. date date::operator--(int)
  122. {
  123. date tmp(*this);
  124. *this -= 1;
  125. return tmp;
  126. }
  127. int date::deal(int year, int month, int day)
  128. {
  129. int sum = day - 1;
  130. month--;
  131. for (; month >= 1; month--)
  132. {
  133. sum = sum + getmonthday(year, month);
  134. }
  135. return sum;
  136. }
  137. int date:: operator-(const date& d)
  138. {
  139. int year = 0;
  140. int sum = 0;
  141. for (year = _year-1; year >= d._year; year--)
  142. {
  143. if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
  144. sum = sum + 366;
  145. else
  146. sum = sum + 365;
  147. }
  148. sum = sum + deal(_year, _month, _day) - deal(d._year, d._month, d._day);
  149. return sum;
  150. }

 test.cpp:

 

  1. #include"date.h"
  2. int main()
  3. {
  4. date d1(2023, 6, 22);
  5. date d2(d1); //拷贝构造,对于只有内置类型(即不需要开动态空间的)系统自动生成(浅拷贝即值拷贝),像栈之类的需要自己去写拷贝构造(深拷贝,防止二次析构)
  6. date d3(2003, 10, 1);
  7. cout << d1 - d3 << "天" << endl;
  8. }

6. 取地址运算符重载 

6.1 const成员函数

• 将const修饰的成员函数称之为const成员函数,const修饰成员函数放到成员函数参数列表的后

• const实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进⾏修改。 const 修饰Date类的Print成员函数,Print隐含的this指针由 Date* const this 变为 const Date* const this

  1. #include<iostream>
  2. using namespace std;
  3. class Date
  4. {
  5. public:
  6. Date(int year = 1, int month = 1, int day = 1)
  7. {
  8. _year = year;
  9. _month = month;
  10. _day = day;
  11. }
  12. // void Print(const Date* const this) const
  13. void Print() const
  14. {
  15. cout << _year << "-" << _month << "-" << _day << endl;
  16. }
  17. private:
  18. int _year;
  19. int _month;
  20. int _day;
  21. };
  22. int main()
  23. {
  24. // 这⾥⾮const对象也可以调⽤const成员函数是⼀种权限的缩⼩
  25. Date d1(2024, 7, 5);
  26. d1.Print();
  27. const Date d2(2024, 8, 5);
  28. d2.Print();
  29. return 0;
  30. }

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/967775
推荐阅读
相关标签
  

闽ICP备14008679号