问题及代码:
/*
*Copyright (c) 2015,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cop
*作者:
*完成日期:2015年3月27日
*版本号:v1.0
*
*问题描述:
*输入描述:
*输出描述:
main.cpp
<pre name="code" class="html">#include<iostream>
#include"stu.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");//重新置p1对象的名字
s1.show();
cout<<"s1.Name: "<<s1.getName()<<endl;//打印对象的名字
cout<<"s1.average: "<<s1.average()<<endl;//打印对象的成绩
return 0;
}
stu.h
<pre name="code" class="html">#ifndef STU_H_INCLUDED
#define STU_H_INCLUDED
#include<cstring>
#include<iostream>
using namespace std;
class Stu
{
private:
string name; //学生姓名
float chinese; //语文成绩
float math; //数学成绩
//接下去写
public:
float average();
float Sum();
void setStudent(string,float,float);
void show();
void setName(string);
string getName();
};
#endif // STU_H_INCLUDED
stu.cpp
<pre name="code" class="html">#include "stu.h"
float Stu::average()
{
return (chinese+math)/2;
}
float Stu::Sum()
{
return chinese+math;
}
void Stu::setStudent(string Xname,float Xchinese,float Xmath)
{
name=Xname;
chinese=Xchinese;
math=Xmath;
}
void Stu::show()
{
cout<<"Name: "<<name<<endl;
cout<<"Score:"<<'\t'<<chinese<<'\t'<<math<<endl;
cout<<"average:"<<'\t'<<average()<<'\t'<<"Sum:"<<Sum()<<endl<<endl;
}
void Stu::setName(string Yname)
{
name=Yname;
}
string Stu::getName()
{
return name;
}
运行结果:
<img src="https://img-blog.csdn.net/20150327214542029?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaG91eGluaGFuZzEyMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
</pre><pre>