大侠帮忙看看,哪错啦?
编译,连接都没问题,运行到最后才报错,咋回事?而且,要是把析构函数删了的话,就没问题!
问题该出在析构函数或是内存分配吧
大侠帮忙看看哈
#include<iostream.h>
#include<string.h>
class CPerson
{
public:
CPerson(char *name = NULL, char *id = NULL, int sex = 0)
{
Input(name, id, sex);
cout<<"%%%%%CPerson Constructor%%%"<<endl;
}
CPerson(const CPerson &person)
{
Input(, person.cID, person.bSex);
cout<<"Copy constructor has been used."<<endl;
}
virtual ~CPerson()
{
delete []cName;
delete []cID;
cout<<"########CPerson Destroyed!!!"<<endl;
}
void Input(char *name, char *id, int sex)
{
setName(name);
setID(id);
setSex(sex);
}
void Output()
{
cout<<"NAME: "<<cName<<endl;
cout<<"ID: "<<cID<<endl;
char *str = (bSex == 0 ? "man" : "woman");
cout<<"SEX: "<<str<<endl;
}
char *getName()const
{
return cName;
}
void setName(char *name)
{
cName = new char[sizeof(name)];
strcpy(cName, name);
}
char *getID()const
{
return cID;
}
void setID(char *id)
{
cID = new char[sizeof(id)];
strcpy(cID, id);
}
int getSex()
{
return bSex;
}
void setSex(int sex)
{
bSex = sex;
}
private:
char *cName;
char *cID;
int bSex;
};
class CStudent: public CPerson
{
public:
CStudent(char *name, char *id, int sex, double x = 0, double y = 0, double z = 0):CPerson(name, id, sex)
{
dbScore[0] = x;
dbScore[1] = y;
dbScore[2] = z;
cout<<"CStudent Constructor"<<endl;
}
~CStudent(){cout<<"$$$$$$$$$$$$CStudent Destroyed!!!"<<endl;}
void Print()
{
cout<<"Student resume: "<<endl;
Output();
cout<<"GRADE: "<<dbScore[0]<<"\t"<<dbScore[1]<<"\t"<<dbScore[2]<<endl;
}
private:
double dbScore[3];
};
class CTeacher: public CPerson
{
public:
CTeacher(char *name, char *id, int sex, int schoolage = 0):CPerson(name, id, sex)
{
OfSchoolAge = schoolage;
cout<<"CTeacher Constructor"<<endl;
}
~CTeacher(){cout<<"^^^^^^^^^^^^CTeacher DesTroyed!!!"<<endl;}
void Print()
{
cout<<"Teacher Resume: "<<endl;
Output();
cout<<"SCHOOLAGE: "<<OfSchoolAge<<endl;
}
private:
int OfSchoolAge;
};
void BasicResume(char *name, char *id, int &sex)
{
cout<<"Name: ";
cin>>name;
cout<<"ID: ";
cin>>id;
cout<<"Sex(man: 0, woman: 1): ";
cin>>sex;
}
void StudentResume(char *name, char *id, int &sex, double *score)
{
BasicResume(name, id, sex);
cout<<"Grade(three subject): ";
for(int i = 0; i < 3; i++)
{
cin>>score[i];
}
cout<<endl;
}
void TeacherResume(char *name, char *id, int &sex, int &schoolage)
{
BasicResume(name, id, sex);
cout<<"SchoolAge: ";
cin>>schoolage;
}
void main()
{
char name[20], id[20];
int sex;
double score[3];
int schoolage;
cout<<"Please input the student's resume: "<<endl;
StudentResume(name, id, sex, score);
CStudent student(name, id, sex, score[0], score[1], score[2]);
student.Print();
TeacherResume(name, id, sex, schoolage);
CTeacher teacher(name, id, sex, schoolage);
teacher.Print();
}