如何用C++定义一个学生类?

要求可以存放五个学生的姓名以及四科的成绩。可以计算平均分,并进行比较,输出。我不会定义,不知道该怎么把这些信息进行输入。

定义一个学生类,里面包含了学生的姓名、四科成绩和平均成绩的成员变量,计算平均分的成员函数。在学生类的构造函数里对学生的姓名和成绩做初始化。然后你实例化5个学生变量就可以了。
CStudent
{
private:
char mName[20];
float mScore[4];
public:
float mAve;
CStudent(char *strName,float *score);
void calulateAve();
}
CStudent::CStudent(char *strName,float *score)
{
strcpy(mName,strName);
for(int i=0;i<4;i++)
mScore[i]=score[i];
}
void CStudent::calulateAve()
{
int i;
for(i=0;i<4;i++)
mAve+=mScore[i];
mAve/=4;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-12-12
我相信看了一下代码你就会明白,祝你好运!

#include<iostream.h>
class student
{private:
float aa,bb,cc,dd,avg;
public:
void set( float a,float b ,float c,float d)
{aa=a;bb=b;cc=c;dd=d;
}
avg=(aa+bb+cc+dd)/4.0;
cout<<"你的平均分为"<<avg<<endl;
};
void main()
{
student n1,n2,n3;
n1=张三;
n2=李四;
n3=王五;
cout<<n1<<"的平均成绩"<<n1.set(67,77,87,98)<<endl;
cout<<n2<<"的平均成绩"<<n2.set(65,76,86,97)<<endl;
cout<<n3<<"的平均成绩"<<n3.set(67,67,89,98)<<endl;
}
第2个回答  2011-12-12
#include "iostream.h"
#include "string.h"
class student
{
private:
char name[8];
int math;
int chinese;
int english;
int computer;
public:
student(char *s,int x,int y,int z,int m)
{
strcpy(name,s);
math=x;
chinese=y;
english=z;
computer=m;

};
void p()
{
cout<<name<<" "<<math<<" "<<chinese<<" "<<english<<" "<<computer<<" "<<(math+chinese+english+computer)/4.0<<endl;
}

};
void main()
{
student s1("张三",88,99,78,87);
s1.p();
student s2("李四",87,94,78,88);
s2.p();
student s3("王五",66,99,78,87);
s3.p();
student s4("小明",85,99,78,87);
s4.p();
student s5("小张",83,99,78,87);
s4.p();
}