⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_3_static_member_5.cpp

📁 对于一个初涉VC++的人来书
💻 CPP
字号:

//************************************************

#include <iostream.h>

class Student
{
public:
	Student(int,int,float);
	void total();
	static float average();
private:
	int num;
	int age;
	float score;
	static float sum;
	static int count;
};

Student::Student(int m,int a,float s)
{
	num=m;
	age=a;
	score=s;
}
 
void Student::total()
{
	sum+=score;
    count++;
}
   
float Student::average()
{ 
	return(sum/count);
}

float Student::sum=0;
int Student::count=0;

int main()
{
	Student stud[3]={Student(1001,18,70),
		             Student(1002,19,79),
                     Student(1005,20,98)};

	int n;
    cout<<"please input the number of students (1-3): ";
    cin>>n;

    for(int i=0;i<n;i++)
		stud[i].total();

	cout<<"The average score of "<<n<<" students is ";
	cout<<stud[0].average()<<endl;
	cout<<stud[1].average()<<endl;
	cout<<stud[2].average()<<endl;
	cout<<Student::average()<<endl;

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -