发布网友 发布时间:2022-07-22 04:23
共1个回答
热心网友 时间:2023-11-05 02:15
#include <string.h> #include <iostream> using namespace std; class person { public: person():age(0) { memset(name,0,19); memset(sex,0,4); } char name[20]; char sex[5]; //数据“男”或“女” int age; }; class Scores { public: Scores():m_Chinese(0),m_math(0),m_English(0) { } float m_Chinese; float m_math; //数学 float m_English; }; class student : private person //私有继承基类的pulic成员,在派生类中为private私有的 { public: student() { memset(number,0,4); } private: char number[5]; // 4位学号 Scores scores; // 成绩 public: void SetNumber(char *pNum) //设置学号 { strcpy(number,pNum); } void GetNumber(char *pNum) //获取学号 { strcpy(pNum,number); } void SetSocres(float fch,float fmath,float fEnglish) { scores.m_Chinese = fch; scores.m_math = fmath; scores.m_English = fEnglish; } void GetSocres(Scores &sc) //获取所有成绩 { sc.m_Chinese = scores.m_Chinese; sc.m_math = scores.m_math; sc.m_English = scores.m_English; } void GetName(char *pName) //获取姓名 { strcpy(pName,name); } void SetName(char *pName) //设置姓名 { strcpy(name,pName); } void GetSex(char *pSex) { strcpy(pSex,sex); } void SetSex(char *pSex) { strcpy(sex,pSex); } int GetAge() { return age; } void SetAge(int nAge) { age = nAge; } }; int main() { student stu[3]; stu[0].SetName("AA"); stu[0].SetSex("男"); stu[0].SetAge(18); stu[0].SetNumber("001"); stu[0].SetSocres(89.0, 87.0, 86.0); stu[1].SetName("BB"); stu[1].SetSex("男"); stu[1].SetAge(18); stu[1].SetNumber("002"); stu[1].SetSocres(67.0, 89.0, 78.0); stu[2].SetName("CC"); stu[2].SetSex("女"); stu[2].SetAge(18); stu[2].SetNumber("003"); stu[2].SetSocres(44.0, 66.0, 88.0); Scores temp; printf("输出信息:\n"); printf("学号\t姓名\t性别\t年龄\t语文\t数学\t英语\t\n"); for (int i=0;i<3;i++) { char temp[5]; stu[i].GetNumber(temp); printf("%s\t",temp); char temp1[20]; stu[i].GetName(temp1); printf("%s\t",temp1); char temp2[5]; stu[i].GetSex(temp2); printf("%s\t",temp2); printf("%d\t",stu[i].GetAge()); Scores sc; stu[i].GetSocres(sc); printf("%3.1f\t",sc.m_Chinese); printf("%3.1f\t",sc.m_math); printf("%3.1f\t\n",sc.m_English); } system("pause"); return 0; }