zoukankan      html  css  js  c++  java
  • 第46课.继承中的构造与析构

    1.子类对象的构造

    a.子类中可以定义构造函数
    b.子类构造函数
    必须对继承而来的成员进行初始化
     1).直接通过初始化列表或者赋值进行初始化
     2).调用父类构造函数进行初始化

    1).直接通过初始化列表或者赋值进行初始化

    不可行,子类无法访问父类中的private成员。所以对这部分成员无法初始化

    2).调用父类构造函数进行初始化

    默认调用

     适用于无参构造函数和使用默认参数的构造函数

    显示调用

     通过初始化列表进行调用;使用于所有父类构造函数;

    class Child : public Parent
    {
    public:
        Child()            /*    隐式调用    */
        {
            cout << "Child() : " << endl;
        }
        Child(string s) : Parent("Parameter to Parent")        /*    显示调用    */
        {
            cout << "child() : " << s <<endl;
        }
    }
    

    2.子类对象的构造规则

    a.子类对象在创建时会首先调用父类的构造函数
    b.先执行父类构造函数再执行子类的构造函数
    c.父类构造函数可以被隐式调用或者显示调用

    3.对象创建时构造函数的调用顺序

    a.调用父类的构造函数
    b.调用成员变量的构造函数
    c.调用类自身的构造函数

    总结:先父母,后客人,再自己

    eg:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Parent 
    {
    public:
        Parent ()
        {
            cout << "Parent ()" << endl;
        }
        Parent (string s)
        {
            cout << "Parent (string s) :" << s << endl;
        }
    };
    
    class Child : public Parent
    {
    public:
        Child ()
        {
            cout << "Child ()" << endl;
        }
        Child (string s) : Parent(s)
        {
            cout << "Child (string s) :" << s << endl;
        }
    };
    
    int main()
    {
        Child c;
        Child cc("cc");
        
        return 0;
    }
    

    4.析构函数的调用顺序与构造函数相反

    a.执行自身的析构函数
    b.执行成员变量的析构函数
    c.执行父类的析构函数

    eg:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Object
    {
        string ms;
    public:
        Object (string s)
        {
            cout << "Object (string s) : " << s << endl;
            ms = s;
        }
            
        ~Object ()
        {
            cout << "~Object() : " << ms << endl;
        }
    };
    
    class Parent : public Object
    {
        string ms;
    public:
        Parent () : Object("Default")
        {
            cout << "Parent ()" << endl;
            ms = "Default";
        }
        Parent (string s) : Object(s)
        {
            cout << "Parent (string s) : " << s << endl;
            ms = s;
        }
        ~Parent()
        {
            cout << "~Parent () : " << ms << endl;
        }
    };
    
    class Child : public Parent
    {
        Object mO1;
        Object mO2;
        string ms;
    public:
        Child () : mO1("Default 1"), mO2("Default 2")
        {
            cout << "Child ()" << endl;
            ms = "Default";
        }
        Child (string s) : Parent(s), mO1(s + " 1" ), mO2(s + " 2" )
        {
            cout << "Child (string s)  : " << s << endl;
            ms = s;
        }
        ~Child ()
        {
            cout << "~Child () : " << ms << endl;
        }
    };
    
    int main()
    {
        Child cc("cc");
     
        cout << endl;
     
        /* Object (string s) : cc
         * Parent (string s) : cc
         * Object (string s) : cc 1
         * Object (string s) : cc 2
         * Child (string s)  : cc
         *
         * ~Child () : cc
         * ~Object() : cc 2
         * ~Object() : cc 1
         * ~Parent () : cc
         * ~Object() : cc
         */
           
        return 0;
    }
    

  • 相关阅读:
    剑指offer——关于排序算法的应用(一):归并排序
    剑指offer——关于排序算法的应用:选择排序和冒泡排序
    剑指offer:将矩阵选择、螺旋输出矩阵——Python之光
    剑指offer:链表——常见的多指针协同操作:
    剑指Offer:编程习惯篇:代码鲁棒性,代码可扩展性——防御性的编程习惯,解决问题时方法分模块考虑
    剑指offer:数字二进制含1个数,快速幂运算:二进制位运算的运用
    剑指offer:斐波那契数列,跳台阶,变态跳台阶——斐波那契数列类题目:
    回溯法实现各种组合的检索:
    剑指offer:二维数组中查找
    jdk生成https证书的方法
  • 原文地址:https://www.cnblogs.com/huangdengtao/p/11950319.html
Copyright © 2011-2022 走看看