/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:呼亚萍
* 完成日期:2015年 3月 25日
* 版 本 号:v1.0
*
* 问题描述:下面提供了类Stu的数据成员定义,和用于测试的main函数,参考如图的运行结果,完成类的定义,并用多文件形式组织程序
* 程序输入:相应的程序
* 程序输出:学生的成绩信息
*/
student.h:
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include <cstring>
using namespace std;
class Stu
{
public:
void setStudent(string ,float ,float );
void show();
void setName(string);
string getName();
float average();
float sum();
private:
string name;
float chinese;
float math;
};
#endif // STUDENT_H_INCLUDED
student.cpp:
#include <iostream>
#include <cstring>
#include "student.h"
using namespace std;//用于string的输出
void Stu::setStudent(string p,float q,float r)
{
name=p;
chinese=q;
math=r;
}
float Stu::average()
{
return ((chinese+math)/2);
}
float Stu::sum()
{
return (chinese+math);
}
void Stu::show()
{
cout<<"Name:"<<name<<endl;
cout<<"score:"<<'\t'<<chinese<<'\t'<<math<<endl;
cout<<"average:"<<average()<<'\t'<<"sum:"<<sum();
}
void Stu::setName(string na)
{
name=na;
}
string Stu::getName()
{
return name;
}
main.cpp
#include <iostream>
#include <cstring>
#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();
cout<<endl;
s2.show();
cout<<endl;
s1.setName("xue baochai");
s1.show();
cout<<endl;
cout<<"s1.Name:"<<s1.getName()<<endl;
cout<<"s1.aveage:"<<s1.average()<<endl;
return 0;
}
运算结果:
知识点总结:
程序的多文件组合,类的定义!
学习心得:
对多文件组合的再次应用,使自己更加熟练了!