zoukankan      html  css  js  c++  java
  • 同名隐藏,子类和父类的赋值兼容规则

    子类和父类的赋值兼容规则

    1.同名隐藏(非常重要)

    当子类的成员方法(show)和父类的成员方法(show)的名字相同的时候,父类的所有(show)方法,都不能用子类的对象来调用了,这种现象就是同名隐藏。

    #include <iostream>
    using namespace std;
    
    class Base{
    public:
      Base():d(0){}
      ~Base(){}
      void show(){
        cout << "Base show" << endl;
      }
      void show(int i){
        cout << "Base show(" << i << endl;
      }
    private:
      int d;
    };
    
    class D : public Base{
    public:
      D():x(0){}
      ~D(){}
      void show(){
        cout << "D show" << endl;
      }
    private:
      int x;
    };
    int main(){
      D d;
      d.show();
      //编译不过
      //d.show(20);//因为子类也定义了show方法,所以父类的2个show方法,就都不可以用子类的对象来调用了
      d.Base::show(20);
    }
    

    2.子类和父类的对象/指针/引用赋值

    可以用子类的对象/地址/引用去赋值给父类,叫对象切片,或者叫向上赋值,但是反过来是不可以的。

    赋值后,只能调用父类里的成员方法,不能调用子类里的成员方法。

    #include <iostream>
    using namespace std;
    
    class Base{
    public:
      Base():d(0){}
      ~Base(){}
      void show(){
        cout << "Base show" << endl;
      }
      void show(int i){
        cout << "Base show(" << i << endl;
      }
    private:
      int d;
    };
    
    class D : public Base{
    public:
      D():x(0){}
      ~D(){}
      void show(){
        cout << "D show" << endl;
      }
      void list(){
        cout << "D list" << endl;
      }
    private:
      int x;
    };
    
    int main(){
      D d;
        
      Base b;
      b = d;
      b.show();
      //编译不过
      //b.list();//不可以用父类的对象调用子类的方法  
        
      Base *pb;
      pb = &d;
      pb->show();//调用的是父类的show方法
      //编译不过
      //pb->list();//不可以用父类的指针调用子类的方法                                                                   
    
      Base &ps = d;
      ps.show();//调用的是父类的show方法
      //编译不过
      //ps.list();//不可以用父类的引用调用子类的方法 
    }
    
    
    3. 虚函数

    当用子类的对象/指针/引用去赋值给父类时,如果子类和父类都有同名方法show,当调用show的时候,发现调用的是父类的show方法。如果想要达到调用子类的show方法的效果的话,必须用virtual关键字,修饰父类的show方法,而且父类必须是指针或者引用,不可以是对象。

    #include <iostream>
    using namespace std;
    
    class Base{
    public:
      Base():d(0){}
      ~Base(){}
      virtual void show(){//虚函数
        cout << "Base show" << endl;
      }
      void show(int i){
        cout << "Base show(" << i << endl;
      }
    private:
      int d;
    };
    
    class D : public Base{
    public:
      D():x(0){}
      ~D(){}
      void show(){
        cout << "D show" << endl;
      }
      void list(){
        cout << "D list" << endl;
      }
    private:
      int x;
    };
    
    int main(){
      D d;
    
      Base b;
      b = d;
      b.show();//调用的是父类的show
    
      Base *pb;
      pb = &d;
      pb->show();//调用的是子类的show
    
      Base &ps = d;
      ps.show();//调用的是子类的show
    }
    
    
  • 相关阅读:
    [LeetCode] Implement Queue using Stacks
    [LintCode] 带重复元素的排列
    [LintCode] 全排列
    [LeetCode] Substring with Concatenation of All Words
    linux下安装eclipse并使用xstart远程使用(centos7)
    linux下安装jsp开发运行环境(centos7)
    linux下扩展root分区
    linux下安装telnet(centos7)
    linux下搭建java开发环境
    linux通过脚本获取内存信息
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/9137833.html
Copyright © 2011-2022 走看看