定义一个学生类,学生包含的特征信息有学号、姓名、性别和年龄等;功能(函数)包括获取学生姓名、性别和年龄等。在主函数中定义学生对象数组(长度不少于5),分别统计男生的平均年龄和女生的平均年龄(借助指针),并输出。请示范一下分3部分 头函数功能函数主函数
2 回答
慕标5832272
TA贡献1966条经验 获得超4个赞
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阳
TA贡献1859条经验 获得超6个赞
#include<iostream> #include<string> using namespace std; const int length=3; class Student { int age; int Math; int Eng; int Chn; string name; string sex; public : Student( int age, int Math, int Eng, const string sex) { this ->age=age; this ->Math=Math; this ->Eng=Eng; this ->sex=sex; } Student() { } void SetInfo( int age ,string sex,string name, int math, int eng, int chn) { this ->age=age; this ->sex=sex; this ->name=name; this ->Math=math; this ->Chn=chn; this ->Eng=eng; } string GetName(){ return name;} string GetSex(){ return sex;} int GetAge(){ return age;} int GetMath(){ return Math;} int GetEng(){ return Eng;} int GetChn(){ return Chn;} |
}; class Compute { Student s[length]; double sum_math; double sum_eng; double sum_chn; public : Compute( double a=0, int b=0, int c=0):sum_math(a),sum_eng(b),sum_chn(c) { Set(); GetAverage(); } void Set() { |
s[0].SetInfo(10, "F" , "ABC" ,87,88,89); s[1].SetInfo(20, "M" , "CDE" ,78,89,99); s[2].SetInfo(25, "F" , "QWE" ,69,89,94); } void GetAverage() { //double sum_math=0,sum_eng=0,sum_chn=0, for ( int i=0;i<length;i++) { sum_math+=s[i].GetMath(); sum_eng+=s[i].GetEng(); sum_chn+=s[i].GetChn(); } } double MathAve(){ return (sum_math/length);} double ChnAve(){ return (sum_chn/length);} double EngAve(){ return (sum_eng/length);} void Show(); |
}; void Compute::Show() { cout<< "name\t" << "sex\t" << "age\t" << "chinese\t" << "math\t" << "Eng" <<endl; for ( int i=0;i<length;i++) { cout<<s[i].GetName()<< "\t" <<s[i].GetSex()<< "\t" <<s[i].GetAge()<< "\t" <<s[i].GetChn()<< "\t" ; cout<<s[i].GetMath()<< "\t" <<s[i].GetEng()<<endl; } } int main() { Compute obj; obj.Set(); obj.Show(); cout.precision(5); cout<< "chn average is:" <<obj.ChnAve()<<endl; cout<< "eng average is:" <<obj.EngAve()<<endl; cout<< "math average is:" <<obj.MathAve()<<endl; return 0; |
} |
- 2 回答
- 0 关注
- 265 浏览
添加回答
举报
0/150
提交
取消