• C++中的运算符重载


    目录

    1、运算符重载规则

    2、运算符重载的两种形式

    2.1、成员函数重载形式   

    2.2、普通的非成员函数重载形式


    1、运算符重载规则

    C++中可以重定义或重载大部分 C++ 内置的运算符。这样,我们就能使用自定义类型的运算符。

    重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构

    成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。

    2、运算符重载的两种形式

    2.1、成员函数重载形式   

    Box operator+(const Box&);
    

    成员函数重载形式中的重载函数一定要定义在成员函数中,而且形参只有 0 个或者 1 个,绝对不能写成下面 2.2 中两个形参的形式。第一个参数使用 this 指针传递参数。

    1. #include
    2. using namespace std;
    3. class Box
    4. {
    5. public:
    6. double getVolume(void)
    7. {
    8. return length * breadth * height;
    9. }
    10. void setLength( double len )
    11. {
    12. length = len;
    13. }
    14. void setBreadth( double bre )
    15. {
    16. breadth = bre;
    17. }
    18. void setHeight( double hei )
    19. {
    20. height = hei;
    21. }
    22. // 重载 + 运算符,用于把两个 Box 对象相加,成员运算符重载
    23. Box operator+(const Box& b)
    24. {
    25. Box box;
    26. box.length = this->length + b.length;
    27. box.breadth = this->breadth + b.breadth;
    28. box.height = this->height + b.height;
    29. return box;
    30. }
    31. private:
    32. double length; // 长度
    33. double breadth; // 宽度
    34. double height; // 高度
    35. };
    36. int main( )
    37. {
    38. Box Box1; // 声明 Box1,类型为 Box
    39. Box Box2; // 声明 Box2,类型为 Box
    40. Box Box3; // 声明 Box3,类型为 Box
    41. double volume = 0.0; // 把体积存储在该变量中
    42. // Box1 详述
    43. Box1.setLength(6.0);
    44. Box1.setBreadth(7.0);
    45. Box1.setHeight(5.0);
    46. // Box2 详述
    47. Box2.setLength(12.0);
    48. Box2.setBreadth(13.0);
    49. Box2.setHeight(10.0);
    50. // Box1 的体积
    51. volume = Box1.getVolume();
    52. cout << "Volume of Box1 : " << volume <
    53. // Box2 的体积
    54. volume = Box2.getVolume();
    55. cout << "Volume of Box2 : " << volume <
    56. // 把两个对象相加,得到 Box3
    57. Box3 = Box1 + Box2;
    58. // Box3 的体积
    59. volume = Box3.getVolume();
    60. cout << "Volume of Box3 : " << volume <
    61. return 0;
    62. }

    Volume of Box1 : 210

    Volume of Box2 : 1560

    Volume of Box3 : 5400

    2.2、普通的非成员函数重载形式

    Box operator+(const Box&, const Box&);
    

    若是选择普通的非成员函数重载形式,重载函数定义在类外,并且一定要传递两个参数。而且要注意,如果类的属性是私有的,则一定要在类中使用友元函数声明该重载函数可以访问类的私有属性,否则会发生编译错误,非成员函数不能访问类的私有属性。

    1. #include
    2. using namespace std;
    3. class Box
    4. {
    5. friend Box operator+(const Box& box1, const Box& box2);
    6. public:
    7. double getVolume(void)
    8. {
    9. return length * breadth * height;
    10. }
    11. void setLength( double len )
    12. {
    13. length = len;
    14. }
    15. void setBreadth( double bre )
    16. {
    17. breadth = bre;
    18. }
    19. void setHeight( double hei )
    20. {
    21. height = hei;
    22. }
    23. private:
    24. double length; // 长度
    25. double breadth; // 宽度
    26. double height; // 高度
    27. };
    28. //类的非成员运算符重载
    29. Box operator+(const Box& box1, const Box& box2){
    30. Box box;
    31. box.length = box1.length + box2.length;
    32. box.breadth = box1.breadth + box2.breadth;
    33. box.height = box1.height + box2.height;
    34. return box;
    35. }
    36. int main( )
    37. {
    38. Box Box1; // 声明 Box1,类型为 Box
    39. Box Box2; // 声明 Box2,类型为 Box
    40. Box Box3; // 声明 Box3,类型为 Box
    41. double volume = 0.0; // 把体积存储在该变量中
    42. // Box1 详述
    43. Box1.setLength(6.0);
    44. Box1.setBreadth(7.0);
    45. Box1.setHeight(5.0);
    46. // Box2 详述
    47. Box2.setLength(12.0);
    48. Box2.setBreadth(13.0);
    49. Box2.setHeight(10.0);
    50. // Box1 的体积
    51. volume = Box1.getVolume();
    52. cout << "Volume of Box1 : " << volume <
    53. // Box2 的体积
    54. volume = Box2.getVolume();
    55. cout << "Volume of Box2 : " << volume <
    56. // 把两个对象相加,得到 Box3
    57. Box3 = Box1 + Box2;
    58. // Box3 的体积
    59. volume = Box3.getVolume();
    60. cout << "Volume of Box3 : " << volume <
    61. return 0;
    62. }

    Volume of Box1 : 210

    Volume of Box2 : 1560

    Volume of Box3 : 5400

  • 相关阅读:
    计算机二级WPS 选择题(模拟和解析五)
    541. 反转字符串 II
    建筑特种工附着式升降脚手架工种试题及答案
    人工智能模型的微小单元
    Swin-Transformer 从数据尺度变换角度解析
    Java中的异步日志记录与性能优化
    LeetCode-剑指22-链表中倒数第k个节点
    Selenium获取本地已打开的浏览器页面进行跟踪和自定义日志记录
    必不可少的UI组件二——组件库开发的基础知识(工程化篇)
    ARM/Linux嵌入式面经(六):华为【共四面】
  • 原文地址:https://blog.csdn.net/qq_51691366/article/details/133757281