您的位置:首页 > 其它

简单的派生类的构造函数

2012-08-03 00:00 197 查看
#include<iostream>

#include<string>

using namespace std;

class stu

{

public:

stu(int num,int age,string str)

{

n=num;

a=age;

s=str;

cout<<"基类构造函数执行!\n";

}

~stu() {cout<<"基类析构函数执行!\n" ;};

protected:

int n;

int a;

string s;

};

class stu1:protected stu

{

public:

stu1(int num,int age,string str,char ch):stu(num,age,str)

{

c=ch;

cout<<"派生类函数构造函数执行!\n";

}

~stu1() {cout<<"派生类析构函数执行!\n";};

void display()

{

cout<<"num:"<<n<<endl;

cout<<"age:"<<a<<endl;

cout<<"str:"<<s<<endl;

cout<<"ch:"<<c<<endl;

}

private:

char c;

};

int main()

{

stu1 ss(5,6,"hello",'m');

ss.display();

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  派生类 构造函数