相关概念

  • 重载

    • 在同一作用域中为某个函数和运算符指定多个定义,分别成为函数重载和运算符重载
  • 重载声明
    • 与之前已经在作用域内声明过的函数或方法具有相同名称的声明,参数列表和定义不同
  • 重载决策
    • 调用一个重载函数或重载运算符时,编译器需要比较调用函数时的参数类型与定义时的参数类型,来选择最合适的重载函数和重载运算符,这个过程称为重载决策

函数重载

  • 规则

    • 函数名相同
    • 参数列表不同
    • 与返回类型无关
  • 示例

    // 两个整数相加
    int sum(int a, int b) {
    cout << "sum of two int number: " << a + b << endl;
    return a + b;
    }
    // 两个双精度小数相加
    double sum(double a, double b) {
    cout << "sum of two double number: " << a + b << endl;
    return a + b;
    }
    int main(int argc, const char * argv[]) {
    sum(1, 2); // 两个整数相加
    sum(1.0, 2.0); // 两个双精度小数相加
    sum(1, 2.0); //Call to 'sum' is ambiguous
    return 0;
    }

运算符重载

  • 运算符的实质

    • 带有特殊名称的函数
  • 不可重载的运算符

    运算符 含义
    :: 作用于解析运算符
    .* 成员对象选择运算符
    . 以对象方式访问成员运算符
    ?: 条件判断运算符
  • 示例

    • 一元运算符++

      • 具有前缀形式和后缀形式,前缀形式的运算符重载函数没有参数,后缀形式的运算符重载函数具有参数
      // 创建一个Apple的类
      class AppleBasket {
      private:
      double appleWeight; // 苹果的重量
      double priceOfPerKg; // 每千克苹果的价格
      double totalPrices; // 苹果的总价格
      public:
      // set、get方法
      void setAppleWeight(double weight) {
      appleWeight = weight;
      }
      double getAppleWeight() {
      return appleWeight;
      }
      void setPriceOfPerKg(double price) {
      priceOfPerKg = price;
      }
      double getPriceOfPerKg() {
      return priceOfPerKg;
      }
      double getTotalPrices() {
      if (totalPrices == 0) { // 不打折
      totalPrices = appleWeight * priceOfPerKg;
      }
      return totalPrices;
      }
      // 构造方法
      AppleBasket() {
      appleWeight = 0;
      priceOfPerKg = 0;
      }
      AppleBasket(double priceOfPer, double weight) {
      appleWeight = weight;
      priceOfPerKg = priceOfPer;
      }
      // 运算符重载
      /** 一筐苹果的重量自增 */
      AppleBasket operator++() { // 前缀
      AppleBasket apple;
      apple.appleWeight = ++this->appleWeight;
      apple.priceOfPerKg = this->priceOfPerKg;
      return apple;
      }
      AppleBasket operator++(int) { // 后缀,参数必须是int
      AppleBasket apple = *this;
      this->appleWeight++;
      return apple;
      }
      };
    • 二元运算符+

      /** 两筐苹果的重量相加 */
      AppleBasket operator+(AppleBasket other) {
      AppleBasket apple;
      apple.appleWeight = this->appleWeight + other.appleWeight;
      apple.priceOfPerKg = this->priceOfPerKg;
      return apple;
      }
    • 赋值运算符=

      • 通常用来创建一个新的对象
      /** 赋值运算符 */
      void operator=(AppleBasket other) {
      appleWeight = other.appleWeight;
      priceOfPerKg = other.priceOfPerKg;
      }
    • 逻辑运算符<

      /** 比较this框苹果的重量是否小于other框苹果的质量 */
      bool operator<(AppleBasket other) {
      if (this->appleWeight < other.appleWeight) {
      return true;
      } else {
      return false;
      }
      }
    • 函数调用运算符

      • 不是创建一种新的函数调用方式
      • 实际上是创建了一个可以传递任意数目参数的运算符函数
      /** 函数调用运算符 */
      AppleBasket operator()(double w, double p, double totalP) {
      AppleBasket apple;
      apple.appleWeight = w;
      apple.priceOfPerKg = p;
      apple.totalPrices = totalP;
      return apple;
      }
    • 类成员访问运算符->

      • 通常用于为一个类赋予“指针”行为
      • 重载的运算符函数,必须是一个成员函数,且返回类型必须是指针或者类的对象
      • 运算符->通常与指针引用运算符*结合使用,用来实现“智能指针”的功能,通过智能指针访问对象,可以执行与普通指针不同的任务
      // Apple类的容器
      class AppleBasketContainer {
      private:
      vector<AppleBasket *> container;
      public:
      void add(AppleBasket *apple) {
      container.push_back(apple);
      }
      // 友元类
      friend class SmartPointer;
      };
      //AppleBasketContainer类的友元类
      class SmartPointer {
      private:
      AppleBasketContainer appleContainer;
      int index;
      public:
      // 构造函数
      SmartPointer(AppleBasketContainer& ac) {
      appleContainer = ac;
      index = 0;
      }
      // 运算符重载
      /** 返回值表示列表结束 */
      bool operator++() { // 前缀版本
      if (index > appleContainer.container.size()) return false;
      if (appleContainer.container[++index] == 0) return false;
      return true;
      }
      bool operator++ (int) { // 后缀版本
      return operator++();
      }
      /** 重载运算符 -> */
      Apple* operator->() {
      if (!appleContainer.container[index]) {
      cout << "Zero value!";
      return (Apple*)0;
      }
      return appleContainer.container[index];
      }
      };
    • 输入/输出运算符

      • 通常将运算符重载函数定义为友元函数,使得在不创建对象的情况下调用函数
      // 输入运算符
      friend istream &operator>>(istream &input, AppleBasket &apple) {
      cout << "请输入每kg苹果的价钱:";
      input >> apple.priceOfPerKg;
      cout << "请输入苹果的总重量:";
      input >> apple.appleWeight;
      apple.totalPrices = apple.appleWeight * apple.priceOfPerKg;
      return input;
      }
      // 输出运算符
      friend ostream &operator<<(ostream &output, const AppleBasket &apple) {
      output << "每kg苹果的价格为:" << apple.priceOfPerKg << endl;
      output << "苹果的总重量为:" << apple.appleWeight << endl;
      output << "苹果的总价为:" << apple.totalPrices << endl;
      return output;
      }
    • 下表运算符[]

      • 通常用来增强数组的功能,如:数组越界检查
      // 每个班级中最多的学生个数
      const int MaxStudentCountOfPerTeacher = 45;
      class Student {
      private:
      int studentId;
      int age;
      public:
      // set,get方法
      void setAge(int a) {
      age = a;
      }
      int getAge() {
      return age;
      }
      // studentId通常是自增的,不暴露set方法
      int getStudentId() {
      return studentId;
      }
      // 设置Teacher为Student的友元类,使Teacher可以访问Student的私有成员
      friend class Teacher;
      };
      class Teacher {
      private:
      Student students[MaxStudentCountOfPerTeacher];
      public:
      // 构造函数
      Teacher() {
      for (int i = 0; i < 45; i++) {
      students[i].studentId = i + 1;
      }
      }
      // 运算符重载(数组越界检查)
      Student operator[](int i) {
      if (i >= MaxStudentCountOfPerTeacher) {
      cout << "Index out of bounds" << endl;
      return students[0];
      }
      return students[i];
      }
      };
  • 重载运算符的简单使用

    // 重载运算符的使用示例
    int main(int argc, const char * argv[]) {
    const int size = 10;
    AppleBasket apples[size];
    AppleContainer appleContainer;
    // 初始化apples与appleContainer
    for (int i = 0; i < size; i++) {
    apples[i].setAppleWeight(i);
    apples[i].setPriceOfPerKg(8.8);
    appleContainer.add(&apples[i]);
    }
    // 创建一个迭代器
    SmartPointer smartPointer(appleContainer);
    do {
    double totalPrice = smartPointer->getTotalPrices();
    cout << "苹果的总价格为:" << totalPrice << endl;
    } while (smartPointer++);
    // 自增运算符++(前缀)
    cout << "第一筐苹果的总价格为:" << (++apples[0]).getTotalPrices() << endl;
    // 自增运算符++(后缀)
    cout << "第二筐苹果的总价格为:" << (apples[0]++).getTotalPrices() << endl;
    // 运算符+
    cout << "前两筐苹果的总价为:" << (apples[0] + apples[1]).getTotalPrices() << endl;
    // 逻辑运算符<
    if (apples[0] < apples[1]) {
    cout << "第一筐苹果的重量小于第二筐苹果的重量" << endl;
    } else {
    cout << "第一筐苹果的重量大于等于第二筐苹果的重量" << endl;
    }
    // 赋值运算符=
    AppleBasket apple = apples[2];
    cout << "第三筐苹果的重量为:" << apple.getAppleWeight() << endl;
    // 函数调用运算符()
    cout << "3kg苹果打折后的价格为:" << apple(3, 8.8, 20.0).getTotalPrices() << endl;
    // 输入输出运算符<<,>>
    cin >> apple;
    cout << apple;
    // 下标运算符[]
    Teacher teacher;
    Student student;
    student = teacher[MaxStudentCountOfPerTeacher - 1];
    cout << "最后一个学生的学号为:" << student.getStudentId() << endl;
    student = teacher[MaxStudentCountOfPerTeacher];
    cout << student.getStudentId() << endl;
    return 0;
    }

C++语言-04-重载的更多相关文章

  1. ActionScript语言函数重载

    更新:你见过JavaScript支持重载吗,规范就是这么定义的.如果不是研究Java和Flex对象的Serialization,我也不会注意它. 距离写这篇文章已有8年了,时光匆匆啊,今天整理资料时看 ...

  2. C语言-04函数

    1.参数 参数注意点 1.形式参数:定义函数时函数名后面中的参数,简称形参 2.实际参数:调用函数式传入的具体数据,简称实参 3.实参个数必须等于形参个数 4.函数体内部不能定义和形参一样的变量 5. ...

  3. C#语言-04.OOP基础

    a. OOP:面对对象思想 i. 类:是一种功能强大的数据类型,而且是面向对象的基础 . 语法:访问修饰符 class 类名{ //类的主体 } . 成员变量:不以“函数”形式体现 a. 常量:代表与 ...

  4. C语言 04 进制

    %d 或者%i 十进制 %c 输出字符 %p 输出地址 %f 输出小数 %o 八进制 %x 十六进制 一个int类型变量占4字节,占32bit(位) 例子:十进制 int=12  转二进制 0000 ...

  5. 重学C语言---04字符串和格式化输入/输出

    1.程序示例 //talkback.c一个能为你提供一些信息的对话框 #include <stdio.h> #include <string.h> //提供strlen函数原型 ...

  6. Javascript函数重载,存在呢—还是存在呢?

    1.What's is 函数重载? );//Here is int 10 print("ten");//Here is string ten } 可以发现在C++中会根据参数的类型 ...

  7. C语言面试题汇总之一

    C语言面试题汇总之一 1.static有什么用途?(请至少说明两种) l 限制变量的作用域: 设置变量的存储域. 2.引用和指针有什么区别? 引用必须被初始化,指针不必: 引用初始化以后不能被改变,指 ...

  8. javascript arguments与javascript函数重载

    1.所 有的函数都有属于自己的一个arguments对象,它包括了函所要调用的参数.他不是一个数组,如果用typeof arguments,返回的是’object’.虽然我们可以用调用数据的方法来调用 ...

  9. C++ 重载操作符与转换

    <C++ Primer 4th>读书笔记 重载操作符是具有特殊名称的函数:保留字 operator 后接需定义的操作符号. Sales_item operator+(const Sales ...

  10. 【转】c++重载、覆盖、隐藏——理不清的区别

    原文网址:http://blog.sina.com.cn/s/blog_492d601f0100jqqm.html 再次把林锐博士的<高质量c++编程指南>翻出来看的时候,再一次的觉得这是 ...

随机推荐

  1. form表单action提交表单,页面不跳转且表单数据含文件的处理方法

    在最近的项目中需要将含 input[type='file']的表单提交给后台 ,并且后台需要将文件存储在数据库中.之前所用的方法都是先将文件上传到七牛服务器上,然后七牛会返回文件的下载地址,在提交表单 ...

  2. [ML] Concept Learning

    Candidate Elimination Thanks for Sanketh Vedula. This is a good demo to understand candidate elimina ...

  3. C#语法糖之 ReflectionSugar 通用反射类

    用法很简单: ReflectionSugar rs = new ReflectionSugar(100);//缓存100秒 ,可以不填默认不缓存 rs.有嘛点嘛   性能测试: 性能测试类源码: ht ...

  4. BZOJ1087状压DP 解题报告

    1087: [SCOI2005]互不侵犯King Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的 ...

  5. C#设计模式——工厂方法模式(Factory Method Pattern)

    一.概述在软件系统中,经常面临着“某个对象”的创建工作,由于需求的变化,这个对象的具体实现经常面临着剧烈的变化,但是它却拥有比较稳定的接口.如何应对这种变化?如何提供一种封装机制来隔离出“这个易变对象 ...

  6. WinForm公共控件

    公共控件:1.Button:按钮 用户点击时触发事件 行为属性 Enabled -是否启用 Visible -是否隐藏2.CheckBox .CheckListBox - 复选框 复选框组 3.Com ...

  7. [新手学Java]使用内省(Introspector)操作JavaBean属性

    获取类bean中的所有属性: @Test //获取类bean中的所有属性 public void test1() throws Exception{ BeanInfo info = Introspec ...

  8. Android读写SD卡

    SD卡的读写是我们在开发Android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 String sdCardRoot = Environment.getEx ...

  9. EntityFramework left join

       var result = from u in db.Order                              join n in db.Equipment on u.OrderId  ...

  10. 【Effective Java】3、避免创建不必要的对象

    创建对象的时候,有些变量可以一直保持的时候,可以不必要每次实例化对象的时候都把这些变量初始化一遍,可以使用静态和静态块的方式把这些变量的数据固定下来 package cn.xf.cp.ch02.ite ...