本博客(
http://blog.csdn.net/livelylittlefish
)贴出作者(三二一、小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!
定义一个基类、两个派生类,分别定义其对象,观察其构造、析构函数的调用顺序
代码如下:
/**/
/************************************************************************
* 基类、派生类的构造、析构函数的调用顺序
************************************************************************/
#include < iostream.h >
// 基类
class CPerson
... {
char *name; //姓名
int age; //年龄
char *add; //地址
public:
CPerson()...{cout<<"constructor - CPerson! ";}
~CPerson()...{cout<<"deconstructor - CPerson! ";}
} ;
// 派生类(学生类)
class CStudent : public CPerson
... {
char *depart; //学生所在的系
int grade; //年级
public:
CStudent()...{cout<<"constructor - CStudent! ";}
~CStudent()...{cout<<"deconstructor - CStudent! ";}
} ;
// 派生类(教师类)
class CTeacher : public CPerson
... {
char *major; //教师专业
float salary; //教师的工资
public:
CTeacher()...{cout<<"constructor - CTeacher! ";}
~CTeacher()...{cout<<"deconstructor - CTeacher! ";}
} ;
// 实验主程序
void main()
... {
CPerson person;
CStudent student;
CTeacher teacher;
}
* 基类、派生类的构造、析构函数的调用顺序
************************************************************************/
#include < iostream.h >
// 基类
class CPerson
... {
char *name; //姓名
int age; //年龄
char *add; //地址
public:
CPerson()...{cout<<"constructor - CPerson! ";}
~CPerson()...{cout<<"deconstructor - CPerson! ";}
} ;
// 派生类(学生类)
class CStudent : public CPerson
... {
char *depart; //学生所在的系
int grade; //年级
public:
CStudent()...{cout<<"constructor - CStudent! ";}
~CStudent()...{cout<<"deconstructor - CStudent! ";}
} ;
// 派生类(教师类)
class CTeacher : public CPerson
... {
char *major; //教师专业
float salary; //教师的工资
public:
CTeacher()...{cout<<"constructor - CTeacher! ";}
~CTeacher()...{cout<<"deconstructor - CTeacher! ";}
} ;
// 实验主程序
void main()
... {
CPerson person;
CStudent student;
CTeacher teacher;
}
运行结果: