编写一个学生类,学生信息包含姓名、学号、年龄、性别和成绩;(1)输出每个学生的姓名、学号和成绩。(2)统计并输出学生的总人数、总成绩、平均成绩、最高成绩和最低成绩。

#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.

静态成员函数与类相联系,而不与类的对象相联系,所以,在类的外部调用类中的公有静态成员 函数,必须在其左面加上“类名::”,而不是通过对象名调用公有静态成员函数。在类的外 部不能调用类中的私有静态成员函数

  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

m0_69119927

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

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

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

打赏作者

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

抵扣说明:

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

余额充值