课内学习04
设计学生类Student:
设计学生类Student,成员变量包括ID(学号),Name(姓名),Age(年龄),AverageScore(平均分),成员方法包括4形参更新学生的属性方法Update(),将接口的值分别更新为学生相应成员变量的新值;计算学生的奖学金方法StudentBonus();显示学生的全部信息的方法(包括学号、姓名、年龄、平均分、奖学金)ShowInformation()。本例中奖学金计算方法:
(1)平均分不超过80分(含80分)的学生,奖学金为0,即没有奖学金;
(2)平均分在81~90之间的学生,奖学金计算公式为(平均分 - 80)*50;
(3)平均分在91~100之间的学生,奖学金计算公式为(平均分 – 80)*100;
(4)在主方法中,声明2个学生,分别调用Update()方法更新学生的全部信息,调用ShowInformation()方法显示学生的全部信息。
由于题目太过简单,直接上代码:
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(void);
~Student(void);
void ShowInformation(void);
int Num, Age, Update(int id, string name, int age, double average);
string Name;
double AverageScore;
double StudentBonus(double average);
};
Student::Student(void) : Num(0){}
Student::~Student(void){}
int Student::Update(int num, string name, int age, double average)//更新学生的信息
{
Num = num;
Name = name;
Age = age;
AverageScore = average;
return 0;
}
void Student::ShowInformation(void)//显示学生的全部信息
{
cout << "学号:" << Num << " 姓名:" << Name << " 年龄:" << Age << " 平均分:" << AverageScore << " 奖学金:" << StudentBonus (AverageScore) << endl;
}
double Student::StudentBonus(double average)//奖学金
{
if(average >= 91 && average <= 100)return (average - 80 ) * 100;
else if(average >= 81 && average <= 90)return (average - 80) * 50;
else return 0;
}
int main()
{
system("color F0");
Student student;
string name;
int num,age;
double average;
cout << "请输入学生的学号、姓名、年龄、平均分:" << endl;
cin >> num >> name >> age >> average;
cout << "更新前的学生信息是 ";
student.Update(num,name,age,average);student.ShowInformation();
cout << "更新后的学生信息是 ";
student.Update(201408,"王敏",18,89);student.ShowInformation();
system("pause");
return 0;
}