C++输出到文本本档
#include<iostream>#include<string>
#include<fstream>
using namespace std;
//------------------------------------------------------------------
enum EYear { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR }; //学生年级
enum EStatus { BACHELOR, PART_TIME, TRAINING, OTHER }; //研究生来源
enum ELevel { UNDERGRADUATE, GRADUATE }; //学生类别
//------------------------------------------------------------------
class Student
{
public:
Student( char *name1, char *major1, EYear year,
long id1, float credit1 )
{
name = new char[strlen(name1)+1]; //分配姓名的空间
strcpy(name, name1); //姓名字符组赋值
strcpy(major, major1); //专业
id = id1; //学号
e_year = year; //本科生年级
e_level = UNDERGRADUATE; //学生类别
credit = credit1; //学分
cout<<"Constructing Student class for"<<name<<endl;
}
void print( )
{
cout<<"\n姓名:"<<name<<"\n学号:"<<id<<"\n专业:"<<major
<<"\n类别:"<<l_char(e_level)<<"\n年级:"<<y_char(e_year)
<<"\n学分:"<<credit<<endl;
}
~Student()
{
cout<<"Destructing Student class for"<<name<<endl;
delete name;
}
friend char *y_char (EYear); //友元函数,将枚举值转换成字符串
friend char *l_char (ELevel); //友元函数,将枚举值转换成字符串
private: //基类私有成员在派生类中不可被访问
EYear e_year; //本科生学生所处年级
char major[40]; //本科生专业
protected:
ELevel e_level; //本科生还是研究生
char *name; //姓名
float credit; //学分
long id; //学号
};
//-----------------------------------------------------------------------
class GStudent:public Student
{
private:
EStatus e_status; //研究生来源
char *supervisor; //论文
char thesis[50]; //学分
public:
GStudent(char *name1, char *supervisor1,
EStatus status1, char *thesis1, long id1,
float credit1) : Student(name1, " ", SENIOR,
id1, credit1)
{
supervisor = new char[strlen(supervisor1)+1];
strcpy(supervisor, supervisor1);
e_level = GRADUATE;
e_status = status1;
strcpy(thesis, thesis1);
cout<<"Constructing GStudent class for"<<name<<endl;
}
void print( )
{
cout<<"\n姓名:"<<name<<"\n学号:"<<id<<"\n类别:"
<<l_char(e_level)<<"\n来源:"<<s_char(e_status)<<"\n导师:"
<<supervisor<<"\n论文:"<< thesis<<"\n学分:"<<credit<<endl;
}
friend char *s_char(EStatus); //友元函数,将枚举值转换成字符串
~GStudent( )
{
cout<<"Destructing GStudent class for"<<name<<endl;
delete supervisor;
}
};
//-------------------------------------------------------------------
int main( )
{
ofstream out("a.txt")
s.print();
out<<s.print()<<endl;
gs.print();
out<<gs.print()<<endl; //将输出内容生成文本文本档
return 0;
}
//---------------------------------------------------------------------------
char *l_char(ELevel level)
{
switch (level)
{
case UNDERGRADUATE : return "本科生";
case GRADUATE : return "研究生";
}
}
//---------------------------------------------------------------------------
char *y_char(EYear year)
{
switch(year)
{
case FRESHMAN : return "新生";
case SOPHOMORE : return "二年级";
case JUNIOR : return "三年级";
case SENIOR : return "毕业生";
}
}
//---------------------------------------------------------------------------
char *s_char(EStatus status)
{
switch(status)
{
case BACHELOR : return "应届";
case PART_TIME: return "在职";
case TRAINING : return "代培";
case OTHER: return "其它";
}
}
//===========================================================================
85行 87行的错误怎么改?就是怎么把输出的东西直接输出到文本里