#include<iostream>
#include<string.h>
using namespace std;
class Student {
public:
Student(string name, string sex, string ID,int age, double score) {
m_name=name;
m_ID=ID;
m_sex = sex;
m_age = age;
m_score=score;
sum += m_score;
count++;
if (m_score > max)
max = m_score;
if (m_score < min)
min = m_score;
}
void display1();
void allsum();
void display2();
private:
string m_name;
string m_ID;
int m_age;
string m_sex;
double m_score;
static int count;
static double sum;
static double min;
static double max;
};
double Student::min = 100;
double Student::max = 0;
int Student::count = 0;
double Student::sum = 0;
void Student::display1()
{
cout << "name: " << m_name << endl;
cout << "ID: " << m_ID << endl;
cout << "score: " << m_score << endl;
cout << "sex: " << m_sex << endl;
}
void Student::display2() {
cout << endl;
cout << "sum: " << sum << endl;
cout << "count: " << count << endl;
cout << "avg: " << sum / count << endl;
cout << "max: " << max << endl;
cout << "min: " << min << endl;
}
void text01() {
Student p1("张三","男", "001",18, 93.5);
p1.display1();
Student p2("李四","女", "002",19, 90.5);
p2.display1();
Student p3("王五", "男", "003",20, 89.5);
p3.display1();
Student p4("底线", "女", "004",21, 78);
p4.display1();
p4.display2();
}
int main() {
text01();
system("pause");
return 0;
}
1.
类的静态成员函数只能访问类的静态数据成员,而不能访问类中的普通函数成员(非静态数据成 员),因为普通数据成员只有类的对象存在时才有意义。
2.
静态成员函数与类相联系,而不与类的对象相联系,所以,在类的外部调用类中的公有静态成员 函数,必须在其左面加上“类名::”,而不是通过对象名调用公有静态成员函数。在类的外 部不能调用类中的私有静态成员函数