下面提供了类Stu的数据成员定义,和用于测试的main函数,参考如图的运行结果,完成类的定义,并用多文件形式组织程序
Student.cpp
#include"Student.h"
#include<iostream>
using namespace std;
void Stu::setStudent(string name,float chinese,float math)
{
m_name=name;
m_chinese=chinese;
m_math=math;
}
void Stu::show()
{
cout<<m_name<<" "<<m_chinese<<" "<<m_math<<endl;
}
string Stu::getName()
{
return m_name;
}
void Stu::setName(string n)
{
m_name=n;
}
double Stu::average()
{
return (m_chinese+m_math)/2;
}
Student.h
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include<string>
using namespace std;
class Stu
{
private:
string m_name; //学生姓名
float m_chinese; //语文成绩
float m_math; //数学成绩
//接下去写
public:
void setStudent(string,float,float);
void show();
string getName();
void setName(string n);
double average();
};
#endif // STUDENT_H_INCLUDED
main.cpp
#include<iostream>
#include<string>
#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");//重新置p1对象的名字
s1.show();
cout<<"s1.Name: "<<s1.getName()<<endl;//打印对象的名字
cout<<"s1.average: "<<s1.average()<<endl;//打印对象的成绩
return 0;
}
在这里插入代码片