zoukankan      html  css  js  c++  java
  • 构造函数的继承

    首先我们有一个父类

    class Father
    {
    public:
        Father(int tall);
        int tall;    
        void fuc();
    };
    
    Father::Father(int tall)
    {
        this->tall = tall;
    }
    
    void Father::fuc()
    {
        cout<<"height is "<<tall<<endl;
    }

    父类里面有显式的构造函数 和 一个成员函数 fuc

    然后再来看子类

    class Child:public Father
    {
    public:
        Child(int tall);
    
    };
    
    Child::Child(int tall):Father(tall)
    {
    
    }

    在主函数中调用一下

        Child tom(200);
        tom.fuc();

    运行结果

    height is 200

    表明子类已经成功继承了父类的构造函数 同时还调用了继承过来的变量 tall 以及成员函数 fuc


    还有另外一种写子类的方法

    class Child:public Father
    {
        using Father::Father;
    public:
        
    };

    using 父类::父类

    当构造函数重载比较多时,这样方便很多

    全部代码如下

    class Father
    {
    public:
        Father(int tall);
        int tall;    
        void fuc();
    };
    
    Father::Father(int tall)
    {
        this->tall = tall;
    }
    
    void Father::fuc()
    {
        cout<<"height is "<<tall<<endl;
    }
    
    
    class Child:public Father
    {
        using Father::Father;
    public:
        
    };
    
    
    
    int main(int argc,char *argv[])
    {
        Child tom(200);
        tom.fuc();
    
        return 0;
    }
  • 相关阅读:
    Linux常用命令集合
    运用栈实现表达式求值(+,-,*,/运算)
    队列
    变参函数
    C语言--递归程序的设计
    指针的灵活应用--内核链表中的container_of
    C语言-求回文数字
    压力测试和负载测试
    测试理论
    Dubbo接口调用
  • 原文地址:https://www.cnblogs.com/qifeng1024/p/12603010.html
Copyright © 2011-2022 走看看