设计一个学生类,其中数据成员有学号、姓名、年龄、3门课程的成绩,以及若干成员函数。并利用这个类建立一个对象数组。同时编写主函数使用这个类,实现对学生数据的赋值和输出。要求:(1)使用成员函数实现对数据的输入、输出。(2)在头文件中完成类的声明,在.cpp中完成类的实现,在主文件(.cpp)中完成主函数编写。
student.h文件
#include <stdio.h>
#include <string>
using namespace std;
class Student
{
public:
Student();
void SetXh(string id);
string GetXh();
void SetName(string name);
string GetName();
void SetAge(int a);
int GetAge();
void SetCj1(float a);
void SetCj2(float a);
void SetCj3(float a);
float GetCj1();
float GetCj2();
float GetCj3();
private:
string mId; //学号
string mName; //姓名
int mAge; //年龄
float mChji1; //科目1成绩
float mChji2; //科目1成绩
float mChji3; //科目1成绩
};
student.cpp文件
#include "student.h"
Student::Student(){}
void Student::SetXh(string id){mId = id;}
string Student::GetXh(){return mId;}
void Student::SetName(string name){mName = name;}
string Student::GetName(){return mName;}
void Student::SetAge(int a){mAge = a;}
int Student::GetAge(){return mAge;}
void Student::SetCj1(float a){mChji1 = a;}
void Student::SetCj2(float a){mChji2 = a;}
void Student::SetCj3(float a){mChji3 = a;}
float Student::GetCj1(){return mChji1;}
float Student::GetCj2(){return mChji2;}
float Student::GetCj3(){return mChji3;}
main.cpp文件
#include <stdio.h>
#include <string>
#include "student.h"
using namespace std;
int main()
{
//初始化学生数据,我只写了一个学生的,其它的自己补上就行
Student st[3];
st[0].SetXh("00001");
st[0].SetName("t1");
st[0].SetAge(22);
st[0].SetCj1(88);
st[0].SetCj2(32);
st[0].SetCj1(88);
printf("学号:%s\n",st[0].GetXh().c_str());
printf("姓名:%s\n",st[0].GetName().c_str());
printf("年龄:%d",st[0].GetAge());
getchar();
getchar();
return 0;
}