在写完这个程序后编译和链接都是对的,可为什么运行的时候就出错了啊(下面是截图),百思不得其解,请各位高手帮忙
注:在屏蔽了①②两句后就可以正常运行了,估计是复制构造函数出了问题,但又没发现什么问题,实在想不出来了
#include"iostream"
#include"string"
using namespace std;
class people
{public:
people()
{cout<<"constructing a people..."<<endl;
}
people(const people &p)
{cout<<"copy na people"<<endl;
name=new char[strlen(p.name)];
strcpy(name,p.name);
id=p.id;
sex=p.sex;
}
~people()
{ cout<<"destructing a people..."<<endl;
delete []name;
}
void getname(char *NAME)
{ int a;
a=strlen(NAME);
name=new char[a+1];
strcpy(name,NAME);
cout<<"get a name"<<endl;
cout<<"the name is "<<name<<endl;
}
void getID(int ID)
{ id=ID;
cout<<"get a ID"<<endl;
}
void getsex(int a)
{ sex=a;
cout<<"get a sex"<<endl;
}
protected:
char *name;
int id;
int sex;
};
class student:public people
{ public:
student():people()
{
cout<<"constructing a student..."<<endl;
}
student(const student &stu):people(stu)
{ cout<<"copy a student"<<endl;
course=new char[strlen(stu.course)+1];
strcpy(course,stu.course);
}
void getcourse(char *c)
{course=new char[strlen(c)+1];
strcpy(course,c);
cout<<"get a course"<<endl;
cout<<"the course is "<<course<<endl;
}
void rename()
{ strcpy(name,"jack");
}
void display()
{ cout<<"the name is"<<name<<endl;
}
~student()
{ delete []course;
cout<<"destructing a student..."<<endl;
}
protected:
char *course;
};
void main()
{ student s1;
s1.getname("winki");
s1.getcourse("computer");
student s2(s1);//①
s1.rename();
s1.display();
s2.display();②
}