#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; }
原文地址:http://blog.csdn.net/cherry_ermao/article/details/45058255