C++ const成员数据、静态成员、特殊成员初始化、构造函数初始化列表应用实例

更多资料请点击:我的目录
本篇仅用于记录自己所学知识及应用,代码仍可优化,仅供参考,如果发现有错误的地方,尽管留言于我,谢谢。

要求:设计一个类(Student),用来表征一个学生。在初始化一个学生对象时必须提供姓名和学号,并且一旦确定无法修改。
成员数据包括:
学号(ID),一旦确定无法修改
姓名(name),一旦确定无法修改
成绩(score)

成员方法包括:
构造函数
获取学号
获取姓名
设置和获取成绩
求所有学生的平均分

student.h部分:

#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>

using namespace std;

class student
{
    
    

private:                                        //私有变量
    const int _ID;                              //学号
    const string _name;                         //姓名
    float _score;                               //成绩分数
    static int totalnum;                        //学生总人数
    static float totalscore;                    //所有学生的成绩分数总和

public:                                         //公有变量
    student(int id, string names);              //录入新学生信息(学号与姓名)
    int ID(){
    
    return _ID;}                       //返回学生学号
    string name(){
    
    return _name;}                //返回学生姓名
    float score(){
    
    return _score;}               //返回学生成绩分数
    void in_score(float newscore);              //录入学生成绩分数
    static float averagescore();                //静态函数,返回平均分
    void info();                                //输出学生所有信息
};

#endif // STUDENT_H

studen.c部分:

#include "student.h"

int student::totalnum = 0;                      //静态变量定义
float student::totalscore = 0.0;                //静态变量定义

student::student(int id, string names, float s)
    :_ID(id),_name(names),_score(s)             //初始化列表(只能出现在构造函数中)
{
    
    
//    _ID = id;
//    _name = names;
    totalnum += 1;                              //学生总数++
    totalscore += score();                      //所有学生总成绩++
}

student::~student()
{
    
    
    totalscore -= score();
    totalnum --;
}

void student::in_score(float newscore)
{
    
    
    totalscore -= _score;                       //减去原来的成绩
    totalscore += newscore;                     //加入新录入的成绩
    _score = newscore;
}

float student::averagescore()
{
    
    
    return totalscore/totalnum;                 //返回总成绩/总人数的值
}
void student::info()
{
    
    
    cout << "ID == " << _ID << endl;
    cout << "name == " << _name << endl;
    cout << "score == " << _score<< endl << endl;
}

main.c部分:

#include <iostream>
#include "student.h"

using namespace std;

int main()
{
    
    
    student stud1(16,"jiajia");                             //录入新学生信息(学号与姓名)
    stud1.in_score(100);                                    //录入学生成绩
    student stud2(17,"hehe");                               //录入新学生信息(学号与姓名)
    stud2.in_score(80);                                     //录入学生成绩
    stud1.info();                                           //输出学生信息(学号、姓名、成绩)
    stud2.info();                                           //输出学生信息(学号、姓名、成绩)
    cout << "平均分:" << stud1.averagescore() << endl;      //输出所有学生平均分

    student *stud3 = new student(18,"lala",60);             //录入新学生信息(学号与姓名)
    stud3->info();                                           //输出学生信息(学号、姓名、成绩)
    cout << "平均分:" << student::averagescore() << endl;   //输出所有学生平均分
    delete stud3;                                           //删除学生信息(释放对象)
    cout << "平均分:" << student::averagescore() << endl;   //输出所有学生平均分

    return 0;
}

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43793181/article/details/108348821