使用静态成员函数统计平均成绩
2013-07-28 21:17
309 查看
#include<iostream> using namespace std; class Student { public: Student(int n,int a,float s):num(n),age(a),score(s) {} void total(); static float average(); private: int num; int age; float score; static float sum; static int count; }; void Student::total() //改变静态成员的值,则在各个对象中这个数据成员的值都同时改变。可节约空间 ,提高效率。 { sum=+score; count++; } float Student::average() { return(sum/count); } float Student::sum=0; int Student::count=0; //对静态数据成员初始化。 int main() { Student stu[3]={Student(1001,18,70),Student(1002,19,78),Student(1005,20,98)}; int n; cout<<"please input the number of student: "; cin>>n; for(int i=0;i<n;i++) stu[i].total(); cout<<"the average of the score is :"<<Student::average()<<endl; return 0; }
相关文章推荐
- 第十周刷OJ题 统计程序设计基础课程学生的平均成绩(静态成员函数的使用复习)
- 使用静态成员函数 统计学生成绩
- 类的静态成员变量和静态成员函数的使用方法三
- 类的静态成员函数和静态成员变量的使用
- C++静态成员变量和静态成员函数使用总结
- C++ 静态成员函数使用不需要声明
- 面向对象的程序设计学习笔记-19-静态成员函数的使用
- 类的静态成员函数和静态成员变量的使用
- C++类中的静态成员变量与静态成员函数的使用
- 函数指针 类成员函数指针 类静态成员函数指针 使用实例
- 类的静态成员函数和静态成员变量的使用
- 静态成员函数的使用
- 类的静态成员函数和静态成员变量的使用
- 类的静态成员函数带来了什么好处,应该在什么时候使用?
- 类的静态成员函数带来了什么好处,应该在什么时候使用?
- static 静态成员变量 静态成员函数 类中使用
- Static静态成员函数的使用
- 类的静态成员变量和静态成员函数的使用方法四
- 类的静态成员函数带来了什么好处,应该在什么时候使用?
- C++ 静态成员函数可以使用类内的枚举值