类的继承
1.1 简单构造派生类
#include <iostream>
using namespace std;
struct Base
{
Base()
{
cout<<"Base constructor is called"<<endl;
}
};
class Derive : public Base
{
public:
Derive()
{
cout<<"Derive constructor is called"<<endl;
}
};
int main()
{
Derive d;
}
1.2 输出结果
Base constructor is called
Derive constructor is called
2.1 使用初始化列表构造带参数的派生类
#include <iostream>
using namespace std;
struct Base
{
Base(int)
{
cout<<"Base constructor is called"<<endl;
}
};
class Derive : public Base
{
public:
Derive(int a)
: Base(a)
{
cout<<"Derive constructor is called"<<endl;
}
};
int main()
{
Derive d(2);
}
2.2 输出结果
Base constructor is called
Derive constructor is called