c++实现计算课程总成绩和平均成绩

// main.cpp
// 2021_2
// Created by spurs on 2021/1/2.
// Copyright © 2021 spurs. All rights reserved.
// 计算课程总成绩和平均成绩
#include
using namespace std;
class Student{//创建student类
int score; //定义类的对象
static int total; //定义静态数据成员
public: //类为公有类型
Student(int s=0):score(s){
total+=s;
}
static void display(); //将函数声明为静态,返回值为空
};
int Student::total=0;//静态数据成员的初始化,赋值为0
void Student::display()//调用静态函数,实现数据的共享
{
cout<<total<<endl<<total/5;//第一个是总数,第二个是平均数
}
int main(int argc, const char * argv[]) {
int score;
cout<<“依次输入五次成绩:”<<endl;
for (int i=0; i<5; i++) {
cin>>score;
Student s(score);//该对象的作用域为for循环体,相当于创建五次,销毁五次
}Student s;
s.display();//函数调用
return 0;
}
输出:
依次输入五次成绩:
99 98 99 66 55
417
83Program ended with exit code: 0

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GOD FOR JAVA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值