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;
    }
  • 相关阅读:
    Objective-C 数据集合
    iOS PresentViewControlle后,直接返回根视图
    NSMutableString 常用操作
    NSString 的常用操作
    iOS 获取网络状态
    C#属性封装
    C#类的一些概念
    ref和out 传递参数(C#)
    C#字符串的恒定性
    C#方法的重载和方法的可变参数
  • 原文地址:https://www.cnblogs.com/qifeng1024/p/12603010.html
Copyright © 2011-2022 走看看