C++第46课--继承中的构造与析构

本文学习自 狄泰软件学院 唐佐林老师的 C++课程


实验1:子类构造初探
实验2: 子类构造深度剖析
实验3:对象的析构


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
创建子类对象的时候,会调用子类的构造函数,同时会默认调用父类的无参构造函数或者默认参数的构造函数。如果父类没有提供无参构造函数或者无参构造函数,必须通过子类在初始化列表中显式调用父类的有参构造函数。

实验1:子类构造初探

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


mhr@ubuntu:~/work/c++$ g++ 46-1.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
Parent()
Child()
Parent(string s) : cc
Child(string s) : cc
mhr@ubuntu:~/work/c++$ 

在这里插入图片描述

在这里插入图片描述

实验2: 子类构造深度剖析

#include <iostream>
#include <string>

using namespace std;

class Object
{
public:
    Object(string s)
    {
        cout << "Object(string s) : " << s << endl;
    }
};

class Parent : public Object
{
public:
    Parent() : Object("Default")
    {
        cout << "Parent()" << endl;
    }
    Parent(string s) : Object(s)
    {
        cout << "Parent(string s) : " << s << endl;
    }
};

class Child : public Parent
{
    Object mO1;
    Object mO2;
public:
    Child() : mO1("Default 1"), mO2("Default 2")
    {
        cout << "Child()" << endl;
    }
    Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2")
    {
        cout << "Child(string s) : " << s << endl;
    }
};

int main()
{       
    Child cc("cc");
    
    return 0;
}


mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ g++ 46-2.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
Object(string s) : cc
Parent(string s) : cc
Object(string s) : cc 1
Object(string s) : cc 2
Child(string s) : cc
mhr@ubuntu:~/work/c++$ 

在这里插入图片描述

实验3:对象的析构

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


mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ g++ 46-3.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
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
mhr@ubuntu:~/work/c++$ 

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux老A

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值