面向对象C++ 打印最大值成绩和单科成绩

23 篇文章 1 订阅

一共三个文件    

首先是声明文件 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;


}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值