关于使用c++定义学生对象数组问题,懂的大佬速来~

定义一个学生类,学生包含的特征信息有学号、姓名、性别和年龄等;功能(函数)包括获取学生姓名、性别和年龄等。在主函数中定义学生对象数组(长度不少于5),分别统计男生的平均年龄和女生的平均年龄(借助指针),并输出。请示范一下
分3部分 头函数
功能函数
主函数

杨魅力
浏览 266回答 2
2回答

慕标5832272

class student{public:char *xuehao;char *name;char *sex;int age;public:char *getName(char *xuehao){return .......}char *getSex(char *xuehao){return .......}int getAge(char *xuehao){return .......}}

BIG阳

#include<iostream>#include<string>using&nbsp;namespace&nbsp;std;const&nbsp;int&nbsp;length=3;class&nbsp;Student{&nbsp;int&nbsp;age;&nbsp;int&nbsp;Math;&nbsp;int&nbsp;Eng;&nbsp;int&nbsp;Chn;&nbsp;string&nbsp;name;&nbsp;string&nbsp;sex;public:&nbsp;Student(int&nbsp;age,int&nbsp;Math,int&nbsp;Eng,const&nbsp;string&nbsp;sex)&nbsp;{&nbsp;&nbsp;this->age=age;&nbsp;&nbsp;this->Math=Math;&nbsp;&nbsp;this->Eng=Eng;&nbsp;&nbsp;this->sex=sex;&nbsp;}&nbsp;Student()&nbsp;{&nbsp;}&nbsp;void&nbsp;SetInfo(int&nbsp;age&nbsp;,string&nbsp;sex,string&nbsp;name,int&nbsp;math,int&nbsp;eng,int&nbsp;chn)&nbsp;{&nbsp;&nbsp;this->age=age;&nbsp;&nbsp;this->sex=sex;&nbsp;&nbsp;this->name=name;&nbsp;&nbsp;this->Math=math;&nbsp;&nbsp;this->Chn=chn;&nbsp;&nbsp;this->Eng=eng;&nbsp;}&nbsp;string&nbsp;GetName(){&nbsp;return&nbsp;name;}&nbsp;string&nbsp;GetSex(){return&nbsp;sex;}&nbsp;int&nbsp;GetAge(){return&nbsp;age;}&nbsp;int&nbsp;GetMath(){return&nbsp;Math;}&nbsp;int&nbsp;GetEng(){return&nbsp;Eng;}&nbsp;int&nbsp;GetChn(){return&nbsp;Chn;}};class&nbsp;Compute{&nbsp;Student&nbsp;s[length];&nbsp;double&nbsp;sum_math;&nbsp;double&nbsp;sum_eng;&nbsp;double&nbsp;sum_chn;public:&nbsp;Compute(double&nbsp;a=0,int&nbsp;b=0,int&nbsp;c=0):sum_math(a),sum_eng(b),sum_chn(c)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set();&nbsp;&nbsp;&nbsp;&nbsp;GetAverage();&nbsp;}&nbsp;void&nbsp;Set()&nbsp;{&nbsp;&nbsp;s[0].SetInfo(10,"F","ABC",87,88,89);&nbsp;&nbsp;s[1].SetInfo(20,"M","CDE",78,89,99);&nbsp;&nbsp;s[2].SetInfo(25,"F","QWE",69,89,94);&nbsp;}&nbsp;void&nbsp;GetAverage()&nbsp;{&nbsp;&nbsp;//double&nbsp;sum_math=0,sum_eng=0,sum_chn=0,&nbsp;&nbsp;for(int&nbsp;i=0;i<length;i++)&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;sum_math+=s[i].GetMath();&nbsp;&nbsp;&nbsp;sum_eng+=s[i].GetEng();&nbsp;&nbsp;&nbsp;sum_chn+=s[i].GetChn();&nbsp;&nbsp;}&nbsp;}&nbsp;double&nbsp;MathAve(){return&nbsp;(sum_math/length);}&nbsp;double&nbsp;ChnAve(){return&nbsp;(sum_chn/length);}&nbsp;double&nbsp;EngAve(){return&nbsp;(sum_eng/length);}&nbsp;void&nbsp;Show();};void&nbsp;Compute::Show(){&nbsp;cout<<"name\t"<<"sex\t"<<"age\t"<<"chinese\t"<<"math\t"<<"Eng"<<endl;&nbsp;for(int&nbsp;i=0;i<length;i++)&nbsp;{&nbsp;&nbsp;cout<<s[i].GetName()<<"\t"<<s[i].GetSex()<<"\t"<<s[i].GetAge()<<"\t"<<s[i].GetChn()<<"\t";&nbsp;&nbsp;cout<<s[i].GetMath()<<"\t"<<s[i].GetEng()<<endl;&nbsp;}}int&nbsp;main(){&nbsp;Compute&nbsp;obj;&nbsp;obj.Set();&nbsp;obj.Show();&nbsp;cout.precision(5);&nbsp;&nbsp;&nbsp;&nbsp;cout<<"chn&nbsp;average&nbsp;is:"<<obj.ChnAve()<<endl;&nbsp;cout<<"eng&nbsp;average&nbsp;is:"<<obj.EngAve()<<endl;&nbsp;cout<<"math&nbsp;average&nbsp;is:"<<obj.MathAve()<<endl;&nbsp;return&nbsp;0;}
打开App,查看更多内容
随时随地看视频慕课网APP