继承中的构造与析构

子类对象可当作父类对象使用,子类对象可直接赋值给父类对象,子类对象可直接初始化父类对象,父类指针可直接指向子类对象,父类引用可直接引用子类对象
赋值兼容性原则: 子类是就是特殊的父类
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();	//调用child的print函数 
    rp.print();		//调用child的print函数
    return EXIT_SUCCESS;
}
结果:
Lyl //因调用子类构造函数时,默认先调用父类构造函数
Name: Child
Name: Child
Name: Child
类在C++编译器内部可理解为结构体,子类由父类成员叠加子类新成员得到
如何初始化父类成员?在子类构造函数进行赋值,但若继承成员为private属性,则无法访问
在子类对象构造时需调用父类构造函数对其继承得来的成员进行初始化
在子类对象析构时需调用父类析构函数对其继承得来的成员进行清理
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!")	//不能放child函数体内
    {
        cout<<"Child()"<<endl;
    }
    ~Child()
    {
        cout<<"~Child()"<<endl;
    }
};
int main(int argc, char *argv[])
{
	Child child;
    return EXIT_SUCCESS;
}
结果:
Parent() Parameter from Child!
Parent()
Child()
~Child()
~Parent()
子类对象创建时首先调用父类构造函数,父类构造函数执行结束,执行子类构造函数,当父类构造函数有参数时,需在子类初始化列表中显式调用,析构函数调用顺序与构造函数反
类中成员变量可是其它类的对象,如果一个类继承父类且有其它类的对象作为成员,那么构造函数如何调用?先父母,后客人,再自己
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;
    }
};

int main(int argc, char *argv[])
{
    Child child;
    return EXIT_SUCCESS;
}
结果:
Object() Parameter from Child!
Parent() Parameter from Child!
Object() o1
Object() o2
Child()
子类成员变量与父类成员变量同名时,子类依然从父类继承同名成员, 子类中通过作用域分别符::进行同名成员区分,同名成员存储在内存中不同位置
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重,故必须如此 
        f();	//默认Child中 
    }
};

int main(int argc, char *argv[])
{
    Child child(1, 3);
    return EXIT_SUCCESS;
}
结果:
Parent::i = 1
Child::i = 3
Parent::f = 4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值