constructor initializer list

使用constructor initializer list  初始化 基类的成员变量、子对象的成员变量、和成员变量。


// wave_.cpp : Defines the entry point for the console application.

//
#include "stdafx.h"
#include "wave_process.h"
#include <string>
#include<iostream>
using namespace std;


char WriteFilePath[50] = "d:\\my_path\\output.csv";
char ReadFilePath[50] = "d:\\my_path\\input.csv";
char WaveNameStr[3][20] = {"Small", "Middle", "Big"};


class Sub{
private:
int sub_id;


public:
Sub(){cout << "Construct the sub object" << endl;};
Sub(int id){sub_id = id; cout << "Construct the sub object" << endl;};
~Sub(){cout << "Destruct the sub object" << endl;};


int GetSubID(){return sub_id;}
};


class Base{
protected:
int base_id;


public:
Base() { cout << "Construct base object " << endl;  }
Base(int id) {base_id = id; cout << "Construct base object" << endl;}
~Base() { cout << "Destruct the Base object" << endl; }


void Disp(){ cout << "the base_id is:" << base_id << endl; }
};


class Derived: public Base
{
private:
int local_id;


private:
Sub sub_obj0;
Sub sub_obj1;


public:
Derived(){ cout << "Construct the Derived with no arguments" << endl;}
Derived(int base_id, int sub0_id, int sub1_id, int local_id);
~Derived(){ cout << "Destruct the Derived object" << endl; }
void Disp(){ cout << sub_obj0.GetSubID() << endl << sub_obj1.GetSubID() << endl << base_id << endl << local_id << endl;}


};


Derived::Derived(int base_id, int sub0_id, int sub1_id, int local_id): Base(base_id), sub_obj0(sub0_id), sub_obj1(sub1_id), local_id(local_id)
{
cout << "Construct the Derived with arguments" << endl;
}

int _tmain(int argc, _TCHAR* argv[])
{


cout << "===with no arguments===" << endl;
{
Derived c_obj;
}
cout << endl;
cout << "===with arguments===" << endl;
{
Derived d_obj_arg(10, 1, 2, 100);
d_obj_arg.Disp();
}
getchar();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值