当前位置: 首页 > 工具软件 > stud > 使用案例 >

统计学生平均成绩。使用静态成员函数。如果想在average函数中引用stud[1]的非静态数据成员score,应该怎样处理?

汲利
2023-12-01

统计学生平均成绩。使用静态成员函数。如果想在average函数中引用stud[1]的非静态数据成员score,应该怎样处理?

方法一:

#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;
};

Student stud[3]=//定义stud对象数组为全局变量 
{
	Student(1001,18,70),Student(1002,19,78),Student(1005,20,98)
};

void Student::total()
{
	sum+=score;
	count++;
}

float Student::average()
{	
	cout<<stud[1].score<<endl;//输出对象stud[1]的数据成员score 
	return (sum/count);
}

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

int main()
{
	int n;
	cout<<"please input the number of students:";
	cin>>n;
	for(int i=0;i<n;i++)
	{
		stud[i].total();
	}
	cout<<"the average score of "<<n<<" students is "<<Student::average()<<endl;
	return 0;
}


方法二:

#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(Student stu);
	private:
		int num;
		int age;
		float score;
		static float sum;
		static int count;
};

void Student::total()
{
	sum+=score;
	count++;
}

float Student::average(Student stu)//定义形参stu为对象,用来接受实参 
{	
	cout<<stu.score<<endl;//输出对象stu的数据成员score,即输出对象stud[1]的数据成员score,即输出stud[1].score 
	return (sum/count);
}

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

int main()
{
	Student stud[3]=//定义stud对象数组为全局变量 
	{
		Student(1001,18,70),Student(1002,19,78),Student(1005,20,98)
	};
	int n;
	cout<<"please input the number of students:";
	cin>>n;
	for(int i=0;i<n;i++)
	{
		stud[i].total();
	}
	cout<<Student::average(stud[1])<<endl;//调用静态成员函数average,把实参对象stud[1]传给形参对象stu 
	return 0;
}


方法三:

#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(Student &stu);
	private:
		int num;
		int age;
		float score;
		static float sum;
		static int count;
};

void Student::total()
{
	sum+=score;
	count++;
}

float Student::average(Student &stu)//函数参数stu是对对象stud[1]的引用 
{	
	stu.score=6;
	cout<<stu.score<<endl;//输出对象stu的数据成员score,即输出对象stud[1]的数据成员score,即输出stud[1].score 
	return (sum/count);
}

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

int main()
{
	Student stud[3]=//定义stud对象数组为全局变量 
	{
		Student(1001,18,70),Student(1002,19,78),Student(1005,20,98)
	};
	int n;
	cout<<"please input the number of students:";
	cin>>n;
	for(int i=0;i<n;i++)
	{
		stud[i].total();
	}
	cout<<Student::average(stud[1])<<endl;//调用静态成员函数average,可以通过引用来修改实参stud[1]的值 
	return 0;
}


方法四:

#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(const Student stu);
	private:
		int num;
		int age;
		float score;
		static float sum;
		static int count;
};

void Student::total()
{
	sum+=score;
	count++;
}

float Student::average(const Student stu)//函数参数stu为对象的引用
{	
	cout<<stu.score<<endl;//输出对象stu的数据成员score,即输出对象stud[1]的数据成员score,即输出stud[1].score 
	return (sum/count);
}

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

int main()
{
	Student stud[3]=//定义stud对象数组为全局变量 
	{
		Student(1001,18,70),Student(1002,19,78),Student(1005,20,98)
	};
	int n;
	cout<<"please input the number of students:";
	cin>>n;
	for(int i=0;i<n;i++)
	{
		stud[i].total();
	}
	cout<<Student::average(stud[1])<<endl;//调用静态成员函数average,把实参对象stud[1]传给形参对象stu 
	return 0;
}


方法五:

#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(const Student &stu);
	private:
		int num;
		int age;
		float score;
		static float sum;
		static int count;
};

void Student::total()
{
	sum+=score;
	count++;
}

float Student::average(const Student &stu)//函数参数stu为常对象的引用,不能修改实参stud[1]的值,接受stud[1]的初始化之后stud和stud[1]的值便不能再改变 
{	
	cout<<stu.score<<endl;//输出对象stu的数据成员score,即输出对象stud[1]的数据成员score,即输出stud[1].score 
	return (sum/count);
}

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

int main()
{
	Student stud[3]=//定义stud对象数组为全局变量 
	{
		Student(1001,18,70),Student(1002,19,78),Student(1005,20,98)
	};
	int n;
	cout<<"please input the number of students:";
	cin>>n;
	for(int i=0;i<n;i++)
	{
		stud[i].total();
	}
	cout<<Student::average(stud[1])<<endl;//调用静态成员函数average,可以通过引用来修改实参stud[1]的值 
	return 0;
}


 类似资料: