13. c++中继承的构造与析构

本文深入探讨了C++中的赋值兼容性原则,详细解释了子类对象如何作为父类对象使用,以及子类构造函数如何调用父类构造函数进行初始化。同时,文章还分析了成员变量初始化的顺序,以及同名成员变量的处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

赋值兼容性原则子类对象可以当作父类对象使用

        1. 子类对象可以直接赋值给父类对象;
               2. 子类对象可以直接初始化父类对象;
               3. 父类指针可以直接指向子类对象;
               4. 父类引用可以直接引用子类对象;

注:子类就是特殊的父类!!!

#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;
}

编译运行结果如下:

 

类在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;
}

编译运行结果如下:

 

 1. 子类对象在创建时会首先调用父类的构造函数;
        2. 父类构造函数执行结束后,执行子类的构造函数;
        3. 当父类的构造函数有参数时,需要在子类的初始化列表中显示调用;
        4. 析构函数调用的先后顺序与构造函数相反;

#include <cstdlib>
#include <iostream>

using namespace std;

class Object
{
public:
    Object(const char* s)
    {
        cout<<"Object()"<<" "<<s<<endl;
    }
};

class Parent
{
public:
    Parent()
    {
        cout<<"Parent()"<<endl;
    }
};

class Child : public Parent
{
protected:
    Object o1;
    Object o2;
public:
    Child() : o2("o2"), o1("o1")
    {
        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;
}

编译运行结果如下:

 

#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;
}

编译运行结果如下:

 

我们知道类中的成员变量可以是其它类的对象。如果一个类继承自父类并且有其它的对象作为成员,那么构造函数如何调用? 口诀:先父母,后客人,再自己。

#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;
}

编译运行结果如下:

 

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

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

#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;
}

编译运行结果如下:

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值