#include "stdafx.h" #include <iostream> #include <list> using namespace std; class A { public: //A():m_a(0) //{ // cout<<"Default constructor of class A is called."<<endl; // cout<<"m_a = "<<m_a<<endl; //} A(int a):m_a(0) { cout<<"Class A is called."<<endl; m_a = a; cout<<"m_a = "<<m_a<<endl; } private: int m_a; }; class B : public A { public: B(int b):A(b) // 如果基类没有定义默认构造函数,则必须显示地调用基类的构造函数对基类进行初始化 //B(int b):m_b(0) // 如果基类定义了默认构造函数,则可以不显示地调用基类构造函数,默认调用基类默认的构造函数 { cout<<"Class B is called."<<endl; m_b = b; cout<<"m_b = "<<m_b<<endl; } private: int m_b; }; void main() { B b(12); system("pause"); }
派生类的构造函数对基类显示初始化
最新推荐文章于 2023-02-23 21:54:44 发布