zoukankan      html  css  js  c++  java
  • c++之——重载、重写、重定义

    函数重载:

    必须在同一个类中进行;

    子类无法重载父类的函数,父类同名函数将被子类名称覆盖;

    重载是在编译期间根据参数类型和个数决定函数的调用(静态联编)。

    函数重写与重定义:

    重写:

    必须发生在基类和派生类之间,并且父类与子类的函数必须有完全相同的函数原型;

    使用virtual关键字声明之后能够产生多态(如果不使用virtual,那叫重定义);

    多态是在运行期间根据具体对象的类型决定函数调用(动态联编)。

    代码分析:

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Parent {
     5 public:
     6     virtual void fuc()
     7     {
     8         cout << "parent 无参fuc...
    ";
     9     }
    10     virtual void fuc(int i)
    11     {
    12         cout << "parent 1参fuc...
    ";
    13     }
    14     virtual void fuc(int i,int j)
    15     {
    16         cout << "parent 2参fuc...
    ";
    17     }
    18 
    19 };
    20 class Child1 :  public Parent
    21 {
    22 public:
    23     virtual void fuc(int i, int j)
    24     {
    25         cout << "child 2参fuc...
    ";
    26     }
    27     virtual void fuc(int i, int j,int k)
    28     {
    29         cout << "child 3参fuc...
    ";
    30     }
    31 };
    32 
    33 
    34 int main()
    35 {
    36     Child1 c1;
    37     c1.fuc();
    38     return 0;
    39 }

    报错如下:

     前面已经说过,子类无法重载父类函数,并且,子类同名函数会覆盖父类函数,所以,子类c1访问fuc函数,是没有参数的,编译器在child1中找到了fuc函数的名字,就不会去父类再找了,而child1类中没有提供无参数的fuc函数重载,所以报错,要想访问父类的无参fuc函数,可以加域作用符:

    c1.Parent::fuc();
  • 相关阅读:
    排序——快速排序
    文件操作(获取英文单词)
    两位整数变英文单词
    Doodle Poll 投票文档
    手机与笔记本蓝牙配对
    浏览器的断电续传功能
    3ds max 2011 安装步骤及其注意事项
    anti-alising的基本理解
    OpenGL pipeline (very important)
    secureCrt
  • 原文地址:https://www.cnblogs.com/yangguang-it/p/6534779.html
Copyright © 2011-2022 走看看