C++ 学习(2)

每天可能看的东西并不是很多,因为还要做别的事情,但是我相信积少成多这样的道理:

对象数组的一个样例,也是书上的;

 1 // 对象数组的使用
 2 //计算一个班学生某门功课的总评成绩 ,期末成绩占60%
 3 #include <iostream>
 4 using namespace std;
 5 
 6 const int MaxN=100;
 7 const double Rate=0.6;
 8 
 9 class Score{
10       private:
11                 long No;
12                 char *Name;
13                 int Peace;
14                 int Final;
15                 int Total;
16       public:
17                Score(long no=0,char *name=NULL,int peace=0,int final=0,int total=0);
18                void Count();
19                void ShowScore();
20 };
21 
22 Score::Score(long no,char *name, int peace, int final, int total)
23 {
24      No=no;
25      Name=name;
26      Peace=peace;
27      Final=final;
28      Total=total;
29 }
30 
31 void Score::Count()
32 {
33       Total=Peace*Rate+Final*(1-Rate)+0.5;
34 }
35 
36 void Score::ShowScore()
37 {
38       cout<<No<<"\t"<<Name<<"\t"<<Peace<<"\t"<<Final<<"\t"<<Total<<endl;
39 }
40 
41 int main()
42 {
43      int i;
44      Score ClassScore1[3];            
45      Score ClassScore2[3]={Score(200607001,"Tom",80,79),        //对象数组 
46                              Score(200607002,"John",90,85),
47                         Score(200607003,"Wilson",70,55)    };
48 
49     for(i=0;i<3;i++)
50             ClassScore2[i].Count();
51     for(i=0;i<3;i++)
52             ClassScore2[i].ShowScore();
53     system("pause");
54     return 0;
55 }
原文地址:https://www.cnblogs.com/shenshuyang/p/2639373.html