关于类得继承,运行时出了点小问题,不知道为什么,望指点
//2. 首先完成下列person的定义,然后从person类派生一个教师类,新增的属性有:专业,职称,主讲课程(一门),并为这些属性定义相应的方法。(本题不需要设计构造函数)
#include<iostream.h>
#include<string.h>
class person{
char *Name;
int Age;
char Sex;
public:
void Init(char *name, int age, char sex) //初始化对象
{
Name=new char[strlen(name)+1];
strcpy(Name,name);
Age=age;
Sex=sex;
}
~person()
{
delete[]Name;
}
char *GetName() {return Name;}
int GetAge() {return Age;}
char GetSex() {return Sex;}
void show()
{
cout<<"姓名: "<<GetName()<<" "<<"年龄: "<<GetAge()<<" "<<"性别: "<<GetSex()<<" ";
}
};
class teacher: public person
{
protected:
char *professional;
char *title;
char *course;
public:
void init(char *name,int age,char sex,char *pr,char *tit,char *cs)
{
Init(name,age,sex);
professional=new char[strlen(pr)+1];
strcpy(professional,pr);
title=new char[strlen(tit)+1];
strcpy(title,tit);
course=new char[strlen(cs)+1];
strcpy(course,cs);
}
char *get_professional() {return professional;}
char *get_title() {return title;}
char *get_course() {return course;}
void show()
{
person::show ();
cout<<"专业: "<<get_professional()<<" "<<"职称: "<<get_title()<<" "<<"主讲课程: "<<get_course()<<endl;
}
~teacher()
{
delete[]professional;
delete[]title;
delete[]course;
}
};
void main()
{
teacher t[10];
int i;
char ch='y';
char na[7],pr[10],tit[10],cs[9];
int ag;
char se;
while(1)
{
for(i=0;i<2&&ch=='y';i++)
{
cout<<"------输入老师的:名字,年龄,性别,专业,职称,主讲课程------"<<endl;
cin>>na>>ag>>se>>pr>>tit>>cs;
t[i].init(na,ag,se,pr,tit,cs);
t[i].show();
}
cout<<"是否还有老师y/n"<<endl;
cin>>ch;
if(ch=='n')
break;
}
}