C++学习笔记:继承中的构造与析构

来源:互联网 发布:四川广电网络成都 编辑:程序博客网 时间:2024/06/05 07:19

子类是由父类成员叠加子类新成员得到的。

继承中的构造析构函数调用原则

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

2.父类构造函数执行结束后,执行子类的构造函数

3.当父类的构造函数有参数时,需要再子类的初始化列表中显式调用

4.析构函数调用的先后吮吸与构造函数相反

// 继承中构造和析构.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>using namespace std;class Parent{public:Parent(int a,int b){this->a = a;this->b = b;cout << "调用父类构造函数" << endl;}~Parent(){cout << "调用父类析构函数" << endl;}protected:private:int a;int b;};class Child :public Parent{public:Child(int a,int b,int c) :Parent(a,b){this->c = c;cout << "调用子类构造函数" << endl;}~Child(){cout << "调用子类析构函数" << endl;}protected:private:int c;};void playObj(){Child c1(1,2,3);}int _tmain(int argc, _TCHAR* argv[]){playObj();return 0;}



0 0
原创粉丝点击