#include
#include
using namespace std;
class person
{
public:
person(char name[20],char sex,int age) //<--构造函数的参数命名和成员变量名一样,你能区分是谁赋值给谁吗?改为person( char _name[20], char _sex, int _age )
{
name[20]=name[20]; // <-- memcpy( name, _name, 20 );
sex=sex; //<--sex = _sex;
age=age; // age = _age;
}
protected:
char name[20];
char sex;
int age;
};
class student:public person
{
public:
student(char name[20],char sex,int age,int num):person(name,sex,age)
// student( char _name[20], char _sex, int _age, int _num ): person( _name, _sex, _age )
{
num=num; // <-- num = _num;
}
protected:
int num;
};
int main()
{
student stu[10]=
{
student("angel one",'m',20,1),
student("angel two",'m',20,2),
student("angel three",'m',20,3),
student("angel four",'m',20,4),
student("angel five",'m',20,5),
student("angel six",'m',20,6),
student("angel seven",'m',20,7),
student("angel eight",'m',20,8),
student("angel nine",'m',20,9),
student("angel ten",'m',20,10)
};
ofstream outfile("student.txt",ios::binary);
if(!outfile)
{
cerr<<"open error!"<
}
int i;
for(i=0;i<10;i++)
outfile.write((char*)&stu[i],sizeof(stu[i]));
outfile.close();
return 0;
}