第三周项目:考了语文数学的学生

来源:互联网 发布:淘宝第一层级什么意思 编辑:程序博客网 时间:2024/04/28 13:18

代码:

main.cpp

#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");//重新置p1对象的名字    s1.show();    cout<<"s1.Name: "<<s1.getName()<<endl;//打印对象的名字    cout<<"s1.average: "<<s1.average()<<endl;//打印对象的成绩    return 0;}


student.h

#ifndef STUDENT_H_INCLUDED#define STUDENT_H_INCLUDED#include <iostream>using namespace std;class Stu{private:    string name;//学生姓名    float Chinese;//语文成绩    float math;//数学成绩public:    void setStudent(string x,float y,float z);    void show();    string setName(string a);    string getName();    double average();};#endif // STUDENT_H_INCLUDED


student.cpp

#include <iostream>#include "student.h"using namespace std;void Stu::setStudent(string x,float y,float z){    name=x;    Chinese=y;    math=z;}void Stu::show(){    cout<<"Name:"<<name<<endl;    cout<<"Score:"<<Chinese<<'\t'<<math<<endl;    cout<<"average:"<<(Chinese+math)/2<<'\t'<<"Sum:"<<Chinese+math<<endl;}string Stu::setName(string a){    return name=a;}string Stu::getName(){    return name;}double Stu::average(){    return (Chinese+math)/2;}


运行结果:

0 0
原创粉丝点击