个人感觉这是个很平常的练习。
student.h:
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
using namespace std;
#include <cstring>
class Stu
{
private:
string name;
float chinese;
float math;
public:
float average();
float sum();
void show();
void setStudent(string,float,float);
void setName(string);
string getName();
};
#endif // STUDENT_H_INCLUDED
student.cpp:
#include<iostream>
#include<cstring>
#include "student.h"
using namespace std;
float Stu::average()
{
return (chinese+math)/2;
}
float Stu::sum()
{
return chinese+math;
}
void Stu::show()
{
cout<<"Name: "<<name<<endl;
cout<<"Score: "<<chinese<<'\t'<<math<<endl;
cout<<"average: "<<average()<<'\t'<<"Sum: "<<sum()<<endl<<endl;
}
void Stu::setStudent(string sName, float fChinese, float fMath)
{
name=sName;
chinese=fChinese;
math=fMath;
}
void Stu::setName(string sName)
{
name=sName;
}
string Stu::getName()
{
return name;
}
最后是main:
#include<iostream>
#include"student.h"
using namespace std;
int main()
{
Stu s1,s2;
s1.setStudent("Lin daiyu", 98, 96);
s2.setStudent("Jia baoyu", 90, 88);
s1.show();
s2.show();
s1.setName("xue baochai");
s1.show();
cout<<"s1.Name: "<<s1.getName()<<endl;
cout<<"s1.average: "<<s1.average()<<endl;
return 0;
}
感觉这些人名听搞笑的……总之这是个很简单的东西,算是一种巩固吧。