• 面向对象程序设计2--简单


    //

    //  main.cpp

    //  score

    //

    //  Created by duanqibo on 2019/7/12.

    //  Copyright © 2019年 duanqibo. All rights reserved.

    //  面向对象程序设计--简单

    #include <iostream>

    using namespace std;

    class student

    {

    private:

        double score;

        static double total;

        static double count;

    public:

        void scoretotalcount(double s)

        {

            score=s;

            total=total+score;

            count++;

        }

        static double sum()

        {

            return total;

        }

        static double average()

        {

            return total/count;

        }

    };

    double student::total=0;

    double student::count=0; //静态变量初始化

    int main(int argc, const char * argv[])

    {

        int n;

        double s;

        cout<<"请输入学生人数:";

        cin>>n;

        student stu;

        for(int i=0;i<n;i++)

        {

            cout<<"请输入第"<<i+1<<"个学生的分数:";

            cin>>s;

            stu.scoretotalcount(s);

        }

        cout<<"总分:"<<student::sum()<<endl;

        cout<<"平均分:"<<student::average()<<endl;

        return 1;

    }

    运行结果:

  • 相关阅读:
    XML HTML
    git教程
    GIT过滤
    HTTP
    golang json
    golang type 和断言 interface{}转换
    tcp参数设置
    tcp状态-TIME_WAIT与CLOSE_WAIT带来的坑
    tcp状态
    文件描述符与socket连接
  • 原文地址:https://www.cnblogs.com/duanqibo/p/11177069.html
Copyright © 2020-2023  润新知