编写一个C++程序,定义学生类。用对象数组初始化,将其写入文本文件,并显示。
编写一个程序,首先定义一个学生类,包括学号、姓名、成绩等数据成员。在main函数中利用对象数组初始化学生信息,然后将其写入到文本文件中,最后显示出来。用C++编写。重点是...
编写一个程序,首先定义一个学生类,包括学号、姓名、成绩等数据成员。在main函数中利用对象数组初始化学生信息,然后将其写入到文本文件中,最后显示出来。
用C++编写。
重点是这个对象数组的读写,本人完全不会,求高手指点。 展开
用C++编写。
重点是这个对象数组的读写,本人完全不会,求高手指点。 展开
2个回答
展开全部
VS2010编译运行。程序主要重载流操作符。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class student
{
public:
string number, name;
int grade;
friend ofstream & operator << (ofstream & outfile, student & stu);
friend ifstream & operator >> (ifstream & infile, student & stu);
friend ostream & operator << (ostream & out, student & stu);
};
ofstream & operator << (ofstream & outfile, student & stu)
{
outfile << stu.number << ' ' << stu.name << ' '
<< stu.grade << endl;
return outfile;
}
ifstream & operator >> (ifstream & infile, student & stu)
{
infile >> stu.number >> stu.name >> stu.grade;
return infile;
}
ostream & operator << (ostream & out, student & stu)
{
out << stu.number << '\t' << stu.name << '\t' << stu.grade << endl;
return out;
}
int main()
{
student stu[2] = {{"0001", "Gray", 80}, {"0002", "Anna", 90}}, s1, s2;
ofstream outfile("stuinfo.txt");
outfile << stu[0] << stu[1];
outfile.close();
ifstream infile("stuinfo.txt");
infile >> s1 >> s2;
cout << s1 << s2;
infile.close();
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class student
{
public:
string number, name;
int grade;
friend ofstream & operator << (ofstream & outfile, student & stu);
friend ifstream & operator >> (ifstream & infile, student & stu);
friend ostream & operator << (ostream & out, student & stu);
};
ofstream & operator << (ofstream & outfile, student & stu)
{
outfile << stu.number << ' ' << stu.name << ' '
<< stu.grade << endl;
return outfile;
}
ifstream & operator >> (ifstream & infile, student & stu)
{
infile >> stu.number >> stu.name >> stu.grade;
return infile;
}
ostream & operator << (ostream & out, student & stu)
{
out << stu.number << '\t' << stu.name << '\t' << stu.grade << endl;
return out;
}
int main()
{
student stu[2] = {{"0001", "Gray", 80}, {"0002", "Anna", 90}}, s1, s2;
ofstream outfile("stuinfo.txt");
outfile << stu[0] << stu[1];
outfile.close();
ifstream infile("stuinfo.txt");
infile >> s1 >> s2;
cout << s1 << s2;
infile.close();
}
展开全部
/////////////////////////////////////////
// C-Free 4.1
// MinGW 3.4.5
// veket的小号
/////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student
{
public:
Student(int i, string n, int s);
int GetID(){return id;}
string GetName(){return name;}
int GetScore(){return score;}
private:
int id;
string name;
int score;
};
Student::Student(int i, string n, int s):id(i),name(n),score(s)
{
}
int main()
{
Student a[3] = {Student(1001, "Jack", 95), Student(1002, "Tom", 80), Student(1003, "Mike", 93)};
int i;
for(i=0;i<3;i++)
{
cout << "学号\t" << a[i].GetID()
<< "\t姓名\t" << a[i].GetName()
<< "\t成绩\t" << a[i].GetScore()
<< endl;
}
ofstream f;
f.open("C:\\student.txt", ios::out);
for(i=0;i<3;i++)
{
f << "学号\t" << a[i].GetID()
<< "\t姓名\t" << a[i].GetName()
<< "\t成绩\t" << a[i].GetScore()
<< endl;
}
f.close();
return 0;
}
// C-Free 4.1
// MinGW 3.4.5
// veket的小号
/////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student
{
public:
Student(int i, string n, int s);
int GetID(){return id;}
string GetName(){return name;}
int GetScore(){return score;}
private:
int id;
string name;
int score;
};
Student::Student(int i, string n, int s):id(i),name(n),score(s)
{
}
int main()
{
Student a[3] = {Student(1001, "Jack", 95), Student(1002, "Tom", 80), Student(1003, "Mike", 93)};
int i;
for(i=0;i<3;i++)
{
cout << "学号\t" << a[i].GetID()
<< "\t姓名\t" << a[i].GetName()
<< "\t成绩\t" << a[i].GetScore()
<< endl;
}
ofstream f;
f.open("C:\\student.txt", ios::out);
for(i=0;i<3;i++)
{
f << "学号\t" << a[i].GetID()
<< "\t姓名\t" << a[i].GetName()
<< "\t成绩\t" << a[i].GetScore()
<< endl;
}
f.close();
return 0;
}
追问
Student::Student(int i, string n, int s):id(i),name(n),score(s)
{
}
这个是什么意思啊?
主要是不明白这个部分:id(i),name(n),score(s)
追答
初始化列表 讲组合类的时候会讲到
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询