项目要求
下面提供了类Stu的数据成员定义,和用于测试的main函数,参考如图的运行结果,完成类的定义,并用多文件形式组织程序
#include<iostream>
using namespace std;
class Stu
{
private:
string name; //学生姓名
float chinese; //语文成绩
float math; //数学成绩
//接下去写
};
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;
}
代码如下
main.cpp
#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
#ifndef STU_H_INCLUDED
#define STU_H_INCLUDED
#include<cstring>
using namespace std;
class Stu
{
public:
float average();
float Sum();
void setStudent(string a,float b,float c);
void show();
void setName(string a1);
string getName();
private:
string name; //学生姓名
float chinese; //语文成绩
float math; //数学成绩
//接下去写
};
#endif // STU_H_INCLUDED
Stu.cpp
#include<iostream>
#include<cstring>
#include "Stu.h"
using namespace std;
float Stu::average()
{
return (chinese+math)/2.0;
}
float Stu::Sum()
{
return (chinese+math);
}
void Stu::setStudent(string a,float b,float c)
{
name=a;
chinese=b;
math=c;
}
void Stu::show()
{
cout<<"Name: "<<name<<endl;
cout<<"Score: "<<chinese<<"\t"<<math<<endl;
cout<<"average: "<<average()<<"\t"<<"Sum: "<<Sum()<<endl<<endl;
}
void Stu::setName(string a1)
{
name=a1;
}
string Stu::getName()
{
return name;
}