📄 l8_9.cpp
字号:
#include <iostream.h>
#include <String.h>
class CEmployee
{
private:
char *name;
int age;
public:
CEmployee(char *n, int a);
virtual ~CEmployee();
};
CEmployee::CEmployee(char *n, int a)
{
name=new char[strlen(n)+ 1 ];
strcpy(name, n);
age = a;
}
CEmployee::~CEmployee()
{
cout << "Destruct CEmployee" << endl;
if(name)
{
delete []name;
}
}
class CTeacher : public CEmployee
{
private:
char *mainCourse;
public:
CTeacher(char *n, char *course, int a);
virtual ~CTeacher();
};
CTeacher::CTeacher(char *n, char *course, int a) : CEmployee(n,a)
{
mainCourse = new char[strlen(course)+1];
strcpy(mainCourse, course);
}
CTeacher::~CTeacher()
{
cout << "Destruct CTeacher" << endl;
if(mainCourse)
delete []mainCourse;
}
void main(void)
{
CEmployee *p[3];
p[0] = new CEmployee("Name1", 20);
p[1] = new CTeacher("Name2","C for 2 years,C++ 3 years",26);
p[2] = new CTeacher("Name3","Data structure for 2 years,C++ 3 years",30);
for(int i=0; i<3; i++)
delete p[i];
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -