首页 > 编程语言 > 详细

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

时间:2015-04-15 14:54:55      阅读:248      评论:0      收藏:0      [点我收藏+]
#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;
}
技术分享

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

原文:http://blog.csdn.net/cherry_ermao/article/details/45058255

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!