- /*
- *Copyright (c)2015,烟台大学计算机与控制工程学院
- *All rights reserved.
- *文件名称:score.cpp
- *作 者:李文贺
- *完成日期:2015年3月29日
- *版 本 号:v1.0
- *
- *问题描述:给定考生的语文数学成绩,计算其平均分和总分。
- *程序输入:考生的成绩。
- *程序输出:考生的语文数学成绩、平均分和总分。
- */
- 1. 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;//打印对象的成绩 cout<<"s1.sum: "<<s1.sum()<<endl;//打印对象的成绩 return 0; }
2。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,float,float);
void show();
void setName(string);
string getName();
double average();
double sum();
};
#endif // STUDENT_H_INCLUDED
3.student.cpp
#include<iostream>
#include "student.h"
using namespace std;
void Stu::setStudent(string n,float c,float m)
{
name=n;
chinese=c;
math=m;
}
void Stu::show()
{
cout<<"Name:"<<name<<endl;
cout<<"Score:"<<chinese<<'\t'<<math<<endl;
}
void Stu::setName(string n)
{
name=n;
}
string Stu::getName()
{
return name;
}
double Stu::average()
{
return ((chinese+math)/2.0);
}
double Stu::sum()
{
return (chinese+math);
}
运行结果