用对象数组实现每个同学的成绩的平均值
#include<iostream>
#include<string>
using namespace std;
class stu
{
int num;
string name;
int grade[3];
public:
stu(int m,string n,int *p):num(m),name(n)//指针作为形参
{
for(int i=0;i<3;i++)
{
grade[i]=*(p)++;//赋值
}
}
float aver()//求平均值
{
int i;
int sum=0;
float average;
for(i=0;i<3;i++)
{
sum=sum+grade[i];
}
average=sum/3.0;
return average;
}
void print()
{
cout<<name<<"³É¼¨Îª"<<aver()<<endl;
}
};
int main()
{
int i;
int a[3]={90,95,94};
int b[3]={98,85,94};
int c[3]={84,91,98};
stu students[3]={stu(101,"zhao",a),//三科成绩传入必须为初始化后的数组,不可直接传入成绩,因为构造函数为指针
stu(102,"qian",b),
stu(103,"sun",c)
};
for(i=0;i<3;i++)
students[i].print();
}