//求一個班學生某門功課的總評成績
//socre類里含有姓名,學號,平時成績,期末成績。求總成績,顯示成績
//然后定義一個物件陣列存盤一個班學生的成績,最后通過逐一呼叫陣列元素的成員函式求每個學生的總成績
#include<iostream>
#include<string>
using namespace std;
const double Rate = 0.5;
const int maxN = 100;
class Score
{
private:
long No;
string Name;
int Usual;
int Final;
int Total;
public:
Score(long=0, string * =NULL, int =0, int =0, int =0);
void Count();
void ShowScore();
};
Score::Score(long no, string name, int usual, int final, int total)
{
No = no;
Name = name;
Usual = usual;
Final = final;
Total = total;
}
void Score::Count()
{
Total = Usual * Rate + Final * (1 - Rate) + 0.5;
}
void Score::ShowScore()
{
cout << No << "\t" << Usual << "\t" << Final << "\t" << Total << endl;
}
int main()
{
Score MyScore[3];
Score Myscore2[3] = { (2014,"張三",88,77),(2015,"李四",88,99),(2016,"王五",77,99) };
for (int i = 0; i < 3; i++)
Myscore2[i].Count();
for (int i = 0; i < 3; i++)
Myscore2[i].ShowScore();
return 0;
}
uj5u.com熱心網友回復:
供參考:#include<iostream>
#include<string>
using namespace std;
const double Rate = 0.5;
const int maxN = 100;
class Score
{
private:
long No;
string Name;
int Usual;
int Final;
int Total;
public:
Score(long , string ,int ,int ,int );
void Count();
void ShowScore();
};
Score::Score(long no=0,string name="",int usual=0,int final=0,int total=0)
{
No = no;
Name = name; //Name.assign(name);
Usual = usual;
Final = final;
Total = total;
}
void Score::Count()
{
Total = Usual * Rate + Final * (1 - Rate) + 0.5;
}
void Score::ShowScore()
{
cout << No << "\t" << Name << "\t" << Usual << "\t" << Final << "\t" << Total << endl;
}
int main()
{
Score Myscore[3];
Score Myscore2[3]={Score(2014,"張三",88,77),Score(2015,"李四",88,99),Score(2016,"王五",77,99)};//初始化
for (int i = 0; i < 3; i++)
Myscore2[i].Count();
for (int i = 0; i < 3; i++)
Myscore2[i].ShowScore();
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/278595.html
標籤:C++ 語言
