#include
#include
#include
#include
using namespace std;
class person
{
protected:
char *name;
char *sex;
int age;
public:
person(char *a,char *b,int c):age(c)
{
name=new char[strlen(a)+1];
name=a;
sex=new char[strlen(b)+1];
strcpy(sex,b);
}
void show()
{
cout<<"name:"<
~person()
{
delete []name;
delete []sex;
name=NULL;
sex=NULL;
}
};
class employee:public person
{
protected:
int salary;
int dayoff;
public:
employee(char *a,char*b,int c,int d,int e):person(a,b,c),salary(d),dayoff(e){}
void show()
{
cout<<"basicSalary:"<
};
class manger:public employee
{
float performance;
public:
manger(char *a,char*b,int c,int d,int e,float f):employee(a,b,c,d,e),performance(f){}
void show()
{
cout<<"performance:"<
};
int main()
{
char a[20],b[20];
int c,d,e;
float f;
manger *t;
while(cin>>a>>b>>c>>d>>e>>f)
{
t= new manger(a,b,c,d,e,f);
t->person::show();
t->employee::show();
t->show();
delete (t);
}
}
我的问题是 1为什么每新建一个对象都得为name和sex申请内存,不然每个对象的name和sex都一样?
2为什么上面的指针释放方式不对?必须把delete (t)放在while循环外?