回复:(魔城侠客)创建对象错误
#include"school.h"
#ifndef PERSON_H
#define PERSON_H
class person
{
private:
char *id;
char *name;
float score[3];
school sch;
public:
person(char *,char *,float,float,float);
person(const person &init);
person &operator=(const person &right);
~person();
void modify(char *,char *,char *);
void display();
};
#endif
#include<iostream.h>
#include<string.h>
#include"school.h"
#include"Person.h"
person::person(char *i="00000",char *n="vigour",float s0=0,float s1=0,float s2=0)
{
id=new char[strlen(i)+1];
name=new char[strlen(n)+1];
strcpy(id,i);
strcpy(name,n);
score[0]=s0;
score[1]=s1;
score[2]=s2;
}
person::person(const person &init)
{
//delete []name;
//delete []id;
name=new char[strlen(init.name)+1];
id=new char[strlen(init.id)+1];
strcpy(name,init.name);
strcpy(id,init.id);
score[0]=init.score[0];
score[1]=init.score[1];
score[2]=init.score[2];
}
/*person &person::operator =(const person &right)
{
if(&right!=this)
{
delete []name;
delete []id;
name=new char[strlen(right.name)+1];
id=new char[strlen(right.id)+1];
strcpy(name,right.name);
strcpy(id,right.id);
}
return *this;
}*/
void person::modify(char *n,char *c,char *s)
{
delete []name;
name=new char[strlen(n)+1];
strcpy(name,n);
sch.modify(c,s);
}
void person::display()
{
cout<<"学号:"<<id<<endl
<<"姓名:"<<name<<endl
<<"成绩:"<<score[0]<<","
<<score[1]<<","<<score[2]<<endl;
sch.display();
}
person::~person()
{
delete []id;
delete []name;
//delete []score;
}