• C++--第16课


    第16课 - 继承中的构造与析构

    1. 赋值兼容性原则

    (1)      子类对象可以当作父类对象使用。

    (2)      子类对象可以直接赋值给父类对象。

    (3)      子类对象可以直接初始化父类对象。

    (4)      父类指针可以直接指向子类对象。

    (5)      父类引用可以直接引用子类对象。

    子类就是特殊的父类

    #include <cstdlib>

    #include <iostream>

    using namespace std;

    class Parent

    {

    protected:

        const char* name;

    public:

        Parent()

        {

            name = "Parent";

        }

        void print()

        {

            cout<<"Name: "<<name<<endl;

        }

    };

    class Child : public Parent

    {

    protected:

        int i;

    public:

        Child(int i)

        {

            this->name = "Child";

            this->i = i;

        }

    };

    int main(int argc, char *argv[])

    {

        Child child(1000);

        Parent parent = child;  //用子类对象赋值父类对象

        Parent* pp = &child;   //父类指针指向子类对象,因为子类就是特殊的父类

        Parent& rp = child;    //父类的引用,引用子类的内容

        parent.print();   //打印父类内容

        pp->print();     //打印父类指针

        rp.print();       //打印父类引用

        cout << "Press the enter key to continue ...";

        cin.get();

        return EXIT_SUCCESS;

    }

    运行结果:

    Name:Child

    Name:Child

    Name:Child

    2. 继承对象模型

    类在C++编译器的内部可以理解为结构体,子类是由父类成员叠加子类新成员得到的。

     

    问题:如何初始化父类成员?父类与子类的构造函数有什么关系?

    3. 继承与构造

    在子类对象构造的时候需要调用父类构造函数对其继承得来的成员进行初始化。

     

     

    4. 继承与析构

    在子类对象析构的时候需要调用父类析构函数对其继承得来的成员进行清理。

     

     

    C++特有继承的构造与析构的关系,使得当拿来一段程序的时候,我们可以直接建立我们使用的子类的构造与析构函数,即使我们不了解父类的构造与析构函数,我们也能正常的写程序。

    程序:

    #include <cstdlib>

    #include <iostream>

    using namespace std;

    class Parent

    {

    public:

        Parent()

        {

            cout<<"Parent()"<<endl;

        } 

        ~Parent()

        {

            cout<<"~Parent()"<<endl;

        }

    };

    class Child : public Parent

    {

    public:

        Child()

        {

            cout<<"Child()"<<endl;

        } 

        ~Child()

        {

            cout<<"~Child()"<<endl;

        }

    };

    void run()

    {

        Child child;

    }

    int main(int argc, char *argv[])

    {

        run();

        cout << "Press the enter key to continue ...";

        cin.get();

        return EXIT_SUCCESS;

    }

    运行结果:

    Parent<>

    Child<>

    ~Child<>

    ~Parent<>

    5. 继承的构造与析构

    (1)      子类对象在创建时会首先调用父类的构造函数。

    (2)      父类构造函数执行结束后,执行子类的构造函数。

    (3)      当父类的构造函数有参数时,需要在子类的初始化列表中显示调用。

    (4)      析构函数调用的先后顺序与构造函数相反。

    程序:

    #include <cstdlib>

    #include <iostream>

    using namespace std;

    class Parent

    {

    public:

        Parent(const char* s)   //父类的构造函数是有参数的

        {

            cout<<"Parent()"<<" "<<s<<endl;

        }

        ~Parent()

        {

            cout<<"~Parent()"<<endl;

        }

    };

    class Child : public Parent

    {

    public:

        Child() : Parent("Parameter from Child!") /*在初始化子类的时候,先初始化父类的构造函数 */

        {

            cout<<"Child()"<<endl;

        }   

        ~Child()

        {

            cout<<"~Child()"<<endl;

        }

    };

    void run()

    {

        Child child;

    }

    int main(int argc, char *argv[])

    {

        run();

        cout << "Press the enter key to continue ...";

        cin.get();

        return EXIT_SUCCESS;

    }

    运行结果:

    Parent<> Parameter from Child!

    Child<>

    ~Child<>

    ~Parent<>

    6. 继承与组合的混搭

    类中的成员变量可以是其它类的对象。

    问题:

    如果一个类继承自父类并且有其它的对象作为成员,那么构造函数如何调用?

    口诀:先父母,后客人,再自己。

    程序:

    #include <cstdlib>

    #include <iostream>

    using namespace std;

    class Object

    {

    public:

        Object(const char* s)

        {

            cout<<"Object()"<<" "<<s<<endl;

        }

    };

    class Parent : public Object

    {

    public:

        Parent(const char* s) : Object(s)

        {

            cout<<"Parent()"<<" "<<s<<endl;

        }

    };

    class Child : public Parent

    {

    protected:

        Object o1;

        Object o2;

    public:

        Child() : o2("o2"), o1("o1"), Parent("Parameter from Child!")

    /*后面这排叫做初始化列表,用于父类继承的初始化*/

        {

            cout<<"Child()"<<endl;

        }

    };

    void run()

    {

        Child child;

    }

    int main(int argc, char *argv[])

    {

        run();

        cout << "Press the enter key to continue ...";

        cin.get();

        return EXIT_SUCCESS;

    }

    运行结果:

    Object<> Parameter from Child!

    Parent<> Parameter from Child!

    Object<> o1

    Parent<>o2

    Child<>

    分析:

    我们要“先父母”,打印Parameter from Child!,但是Parent还是有上面的继承的,所以要先打印Object<> Parameter from Child!  后打印Parent<> Parameter from Child!

    初始化列表里面的顺序是没有用的,我们要看程序中的初始化顺序。

    7. 同名成员变量

    思考:当子类中定义的成员变量与父类中的成员变量同名时会发生什么?

     

    当子类成员变量与父类成员变量同名时,子类依然从父类继承同名成员。在子类中通过作用域分别符::进行同名成员区分,同名成员存储在内存中的不同位置。

    程序:

    #include <cstdlib>

    #include <iostream>

    using namespace std;

    class Parent

    {

    protected:

        int i;

        int f;

    };

    class Child : public Parent

    {

    protected:

        int i; 

        void f()

        {

            cout<<"Parent::i = "<<Parent::i<<endl;

            cout<<"Child::i = "<<Child::i<<endl;

            cout<<"Parent::f = "<<Parent::f<<endl;

        }

    public:

        Child(int i, int j)

        {

            Parent::i = i;

            Child::i = j;

            Parent::f = i + j;

            f();

        }

    };

    void run()

    {

        Child child(1, 3);

    }

    int main(int argc, char *argv[])

    {

        run();

        cout << "Press the enter key to continue ...";

        cin.get();

        return EXIT_SUCCESS;

    }

    运行结果:

    Parent::i = 1

    Child::i = 3

    Parent::f = 4

    类有点像命名空间,命名空间的同名变量的区分,是根据::来区分的。我们这里面用的区分方法一样。

    小结:

    (1)      子类对象可以当作父类对象使用。

    (2)      子类对象在创建时需要调用父类构造函数进行初始化。

    (3)      子类对象在销毁时需要调用父类析构函数进行清理。

    (4)      先执行父类构造函数(执行到父类,别忘了父类的父类),再执行成员构造函数。

    (5)      在继承中的析构顺序与构造顺序对称相反。

    (6)      同名成员通过作用域分辨符进行区分。

  • 相关阅读:
    获取cookie
    EF数据迁移(当模型改变时更新数据库)
    EF有外键的查询
    EF查询
    前台主页面给子页面赋值(回调)
    jquery取元素值
    chart画图
    IIS 7.0 的 ASP.NET 应用程序生命周期概述(转载)
    查询服务器登录个数
    gridview中判断隐藏还是现实
  • 原文地址:https://www.cnblogs.com/free-1122/p/11336220.html
Copyright © 2020-2023  润新知