C++文件读写之对象的读写(二)

2014-11-24 12:30:27 · 作者: · 浏览: 4
nt GetNumberFromFile();
10 char Menu();
11 char SortMenu();
12
13 class Score
14 {
15 public:
16 Score(){ english = 0, math = 0; computer = 0; }
17 Score(float eng, float mat, float com) :english(eng), math(mat), computer(com){}
18 void InputScore()
19 {
20 cout << "enter english score:";
21 cin>> english;
22 cout << "enter math score:";
23 cin >> math;
24 cout << "enter computer score:";
25 cin >> computer;
26 }
27 void outputScore()
28 {
29 cout << english << "\t" << math << "\t" << computer << "\t";
30 }
31 float ScoreSum()
32 {
33 return (math + english + computer);
34 }
35 void swapScore(Score &scor)//对象交换数据
36 {
37 float ftmp = english;
38 english = scor.english;
39 scor.english = ftmp;
40
41 ftmp = math;
42 math = scor.math;
43 scor.math = ftmp;
44
45 ftmp = computer;
46 computer = scor.computer;
47 scor.computer = ftmp;
48 }
49 private:
50 float english;
51 float math;
52 float computer;
53 };
54
55 class Birthday
56 {
57 public:
58 Birthday(){ year = 0; month = 0; day = 0; }
59 Birthday(int ye, int mon, int da) :year(ye), month(mon), day(da){}
60 void inputBirthday()
61 {
62 cout << "enter birthday like \"2014 05 01\":" << endl;
63 cin >> year >> month >> day;
64 }
65 void outputBirthday()
66 {
67 cout << year << "\-" << month << "\-" << day << "\t";
68 }
69 void swapBirthday(Birthday &bir)//对象交换数据
70 {
71 int itmp = year;
72 year = bir.year;
73 bir.year = itmp;
74
75 itmp = month;
76 month = bir.month;
77 bir.month = itmp;
78
79 itmp = day;
80 day = bir.day;
81 bir.day = itmp;
82 }
83 private:
84 int year;
85 int month;
86 int day;
87 };
88
89
90 class Student
91 {
92 public:
93 Student() :score(), birthday(){}
94 ~Student(){}
95 void InputInfo();
96 void OutPutInfo();
97 void ShowInfo();
98 bool CreditManage();
99
100 char *getNum(){ return num; }
101 char *getName(){ return name; }
102 bool getSex(){ return sex; }
103 float GetCredit(){ return credit; }
104 void swapStudent(Student &stu);//对象交换数据
105 private:
106 char num[10];
107 char name[20];
108 bool sex;
109 Score score;
110 Birthday birthday;
111 float credit;
112 bool isChanged;
113 };
复制代码
复制代码
1 #include "student.h"
2 #include "iostream"
3 #include "fstream"
4 using namespace std;
5
6
7 void Student::InputInfo()
8 {
9 cout << "enter number(字符串)";
10 cin >> num;
11 cout << "enter name(字符串)";
12 cin >> name;
13 cout << "enter sex(0表示女,1表示男)";
14 cin >> sex;
15 score.InputScore();
16 birthday.inputBirthday();
17 credit = score.ScoreSum() / 10;//计算得出学分
18 }
19
20 void Student::OutPutInfo()
21 {
22 cout << num << "\t" << name << "\t" << sex << "\t";
23 //score.outputScore();
24 birthday.outputBirthday();
25 cout << credit << endl;
26 }
27
28 char Menu()
29 {
30 //system("cls");
31 cout << "******************************************" << endl;
32 cout << "***welcome to student management system***" << endl;
33 cout << "******************************************" << endl;
34 cout << "please choose the number below:" << endl;
35 cout << "1--成绩录入" << endl;
36 cout << "2--成绩显示" << endl;
37 cout << "3--排序管理" << endl;
38 cout << "4--学分管理" << endl;
39 cout << "0--退出" << endl;
40 char ch = getchar();
41 return ch;
42 }
43
44 void Student::swapStudent(Student &stu)
45 {
46 char tmp[10];
47 strcpy_s(tmp, num);
48 strcpy_s(num, stu.num);
49 strcpy_s(stu.num, tmp)