一共三个文件
首先是声明文件 student.h
#ifndef STUDENT_H
#define STUDENT_H
#include<iostream>
using namespace std;
const double MAXSCORE = 100;
const double MINSCORE = 60;
class student
{
public:
student();
//成员函数
double getAvgScore();
double getEnglishScore();
double getMathScore();
double getbiologyScore();
double getChemistyScore();
double getMaxScore();
void getEveryScore()const;
double english;
double math;
double biology;
double chemisty;
};
#endif // STUDENT_H
然后是student.cpp 实现函数
#include "student.h"
student::student()
{
english = 80.5;
chemisty = 70;
biology = 65;
math = 95;
}
double student::getAvgScore()
{
double result = (english+chemisty+biology+math)/4;
return result;
}
double student::getEnglishScore()
{
return english;
}
double student::getMathScore()
{
return math;
}
double student::getChemistyScore()
{
return chemisty;
}
double student::getbiologyScore()
{
return biology;
}
double student::getMaxScore()
{
double temp;
double arr[][4] = {this->english,this->chemisty,this->biology,this->math};
double max = arr[0][0];
for(int i= 0;i<4;i++)
{
for(int j =0;j<3;j++ )
{
if(arr[i][j]>max)
{
max = arr[i][j];
}
}
}
return max;
}
void student::getEveryScore()const
{
cout<<"english score: "<<english<<endl;
cout<<"chemisty score: "<<english<<endl;
cout<<"biology score: "<<english<<endl;
cout<<"math score: "<<english<<endl;
}
然后是main.cpp函数,在这里创建对象,然后调用函数
#include <QCoreApplication>
#include"student.h"
int main()
{
student yanglei;
cout<<"平均成绩: "<<yanglei.getAvgScore()<<endl;
cout<<"所有成绩为:"<<endl;
yanglei.getEveryScore();
cout<<"单科成绩如下:"<<endl;
cout<<"english score is "<<yanglei.english<<endl;
cout<<"math score is "<<yanglei.math<<endl;
cout<<"biology score is "<<yanglei.biology<<endl;
cout<<"chemisty score is "<<yanglei.chemisty<<endl;
cout<<"最大成绩是:"<<endl;
cout<<"Max score is "<<yanglei.getMaxScore()<<endl;
}