阅读背景:

[c++]派生类与容器类

来源:互联网 
#include<iostream>
using namespace std;
class Base
{
    int x;
public:
    Base(int a)
    {
        x = a;//记得给私有成员赋初值,没有的话会是随机值
        cout<<"constructing Base "<<x<<endl;
    }
    ~Base()
    {
        cout<<"destructing Base "<<x<<endl;
    }
};
class Derived:public Base//基类
{
    int y;
    Base B1,B2;//容器类
public:
    Derived(int a,int b,int c,int d):Base(a),y(b),B1(c),B2(d)//并不按参数列表的顺序定义,而是按照先基类,再容器类,最后子类的顺序定义
    {cout<<"constructing Derived "<<y<<endl;}
    ~Derived()
    {cout<<"destructing Dervied "<<y<<endl;}
};
int main()
{
    Derived D(1,2,3,4);
    return 0;
}#include<iostream>
using namespace std;
class B



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: