简单的派生类的构造函数
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;
}
#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;
}
相关文章推荐
- 派生类的构造函数(简单的举例)
- 简单的派生类的构造函数
- 简单的派生类的构造函数
- 简单的派生类构造函数C++
- 派生类构造函数的定义和使用举例
- C++语言笔记系列之十三——派生类构造函数的调用
- C++编译器:虚基类及其派生类的构造函数
- 基类和派生类的构造函数
- c++, 派生类的构造函数和析构函数 , [ 以及operator=不能被继承 or Not的探讨]
- 派生类构造函数的一些问题
- C++中派生类的构造函数实例
- 类中简单构造函数的使用
- 5-3 多级派生类的构造函数
- 派生类的构造函数
- C++编程入门系列之三十九(继承与派生:派生类的构造函数)
- 构造函数简单示意
- 【C++】派生类构造函数
- C++继承详解之二——派生类成员函数详解(函数隐藏、构造函数与兼容覆盖规则)
- C++中基类,派生类 类成员对象的构造函数,析构函数的顺序
- 5-3 多级派生类的构造函数