2013-7-17学习作业练习
学习 练习 17 作业 2013
2023-09-27 14:29:23 时间
//1.有这样一个学生系统,用于学校存储学生信息,当有新生的时候存储新生信息,有学生毕业的时候删除学生信息,还可以修改学生信息,比如学生突然更改了姓名或者学生换了专业。学生信息至少要有姓名,学号,年龄,性别,专业,手机号码。
//条件:使用类Student,完成。
#include iostream
#define NUM 50
using namespace std;
class Student { private: int num;//学号 string name;//姓名 int age; //年龄 char sex; //性别 string profession;//专业 string telphone; public: Student() { } Student(int nu,string na,int ag,char sx,string pr,string te) { num = nu; name = na; age = ag; sex = sx; profession = pr; telphone = te; } void SetNum(int nu) { num = nu; } void SetName(string na) { name = na; } void SetAge(int ag) { age = ag; } void SetSex(char sx) { sex = sx; } void SetProfession(string pr) { profession = pr; } void SetTelphone(string te) { telphone = te; } Student* GetStudent() { Student *stu; return stu; } int GetNum() { return num; } string GetName() { return name; } int GetAge() { return age; } char GetSex() { return sex; } string GetProfession() { return profession; } string GetTelphone() { return telphone; } //还要有整体的赋值,可以用单目运算符重写= Student operator = (const Student other) { if (this == other) { return *this; } this- num = other.num; this- name = other.name; this- age = other.age; this- sex = other.sex; this- profession = other.profession; this- telphone = other.telphone; } };
//输入学生,返回输入的个数 int setStudent(Student stu[],int n) { int i=0; int j; int iTemp; string sTemp; char cTemp; int f=0; do { cout "请输入学生学号" endl; cin iTemp; stu[i].SetNum(iTemp); cout "请输入学生姓名" endl; cin sTemp; stu[i].SetName(sTemp); cout "请输入学生年龄" endl; cin iTemp; stu[i].SetAge(iTemp); cout "请输入学生性别" endl; cin cTemp; stu[i].SetSex(cTemp); cout "请输入学生专业" endl; cin sTemp; stu[i].SetProfession(sTemp); cout "请输入学生手机号" endl; cin sTemp; stu[i].SetTelphone(sTemp); i++; cout "请问要输入学生信息吗?(1.要 2.不要)"; cin f; } while (1==f); // for (i=0; i i++) { // printf("请问要输入学生信息吗?(1.要 2.不要)"); // cin j; // if (2==j) { // break; // } // else // { // cout "请输入学生学号" endl; // cin iTemp; // stu[i].SetNum(iTemp); // cout "请输入学生姓名" endl; // cin sTemp; // stu[i].SetName(sTemp); // cout "请输入学生年龄" endl; // cin iTemp; // stu[i].SetAge(iTemp); // cout "请输入学生性别" endl; // cin cTemp; // stu[i].SetSex(cTemp); // cout "请输入学生专业" endl; // cin sTemp; // stu[i].SetProfession(sTemp); // cout "请输入学生手机号" endl; // cin sTemp; // stu[i].SetTelphone(sTemp); // } // } return i;//返回输入的学生数 }
//返回查找学号为什么的学生的下标 int findStudent(Student *stu,int n,int findNum)//要查询的Student数组,实际查询的条数,要查询的学号 { int i; int m=-1; for (i=0; i i++) { if(stu[i].GetNum()==findNum) { m=i; break; } } return m; //返回Stu的下标 }
//删除一条学生信息 int deleteStudent(Student *stu,int n,int deleteStudentNum) //在学生数组中删除学号为deleteStudentNum的学生,并且个数减少1 { int i; int j=0; for (i=0; i i++) { if (stu[i].GetNum()==deleteStudentNum) { for (j=i; j j++) { stu[j]=stu[j+1]; } n--; i--; } } return n; //返回n-1 } Student addStudent() { Student stu; int iTemp; char cTemp; string sTemp; cout "请输入学生学号" endl; cin iTemp; stu.SetNum(iTemp); cout "请输入学生姓名" endl; cin sTemp; stu.SetName(sTemp); cout "请输入学生年龄" endl; cin iTemp; stu.SetAge(iTemp); cout "请输入学生性别" endl; cin cTemp; stu.SetSex(cTemp); cout "请输入学生专业" endl; cin sTemp; stu.SetProfession(sTemp); cout "请输入学生手机号" endl; cin sTemp; stu.SetTelphone(sTemp); return stu; } //添加一个学生(可能有点问题) int insertStudent(Student *stu,int n) { Student s=addStudent(); stu[n] = s; return ++n; }
//修改学生信息 void alterStudent() { }
//打印标题行 void printTitle() { cout "学号 " "姓名 " "年龄 " "性别 " "专业 " "手机号" endl; }
//输出打印学生信息 void getStudent(Student *stu,int n) { int i; printTitle();//打印标题 for (i=0; i i++) { cout stu[i].GetNum() " " stu[i].GetName() " " stu[i].GetAge() " " stu[i].GetSex() " " stu[i].GetProfession() " " stu[i].GetTelphone() " " endl; } } //显示菜单 void showMenu() { cout " 欢迎使用学生信息管理系统 " endl; cout "***********************************************" endl; cout "* 1.初始化学生 2.添加学生 *" endl; cout "* 3.修改学生 4.删除学生 *" endl; cout "* 5.显示所有学生 6.退出 *" endl; cout "***********************************************" endl; } //菜单选择 int get_menu_choice() { int menu_ch; do { cout "选择菜单选项:"; cin menu_ch; if ((menu_ch 0)||(menu_ch 6)) { cout "error!" endl; } } while ((menu_ch 0)||(menu_ch return menu_ch; } int main(int argc, const char * argv[]) {
Student* stu = new Student[NUM]; int r1=0; int r2;//保存查找的学号 int choose; while (1) { showMenu(); switch (get_menu_choice()) { case 1: r1=setStudent(stu,NUM);//返回输入的个数 break; case 2: r1=insertStudent(stu,r1); getStudent(stu, r1); break; case 3: cout "请输入要修改的学生的学号:"; int nn; cin nn; r2=findStudent(stu, r1, nn); if (r2==-1) { cout "没有该学生!"; } else { int iTemp; char cTemp; string sTemp; cout "请输入学生学号" endl; cin iTemp; stu[r2].SetNum(iTemp); cout "请输入学生姓名" endl; cin sTemp; stu[r2].SetName(sTemp); cout "请输入学生年龄" endl; cin iTemp; stu[r2].SetAge(iTemp); cout "请输入学生性别" endl; cin cTemp; stu[r2].SetSex(cTemp); cout "请输入学生专业" endl; cin sTemp; stu[r2].SetProfession(sTemp); cout "请输入学生手机号" endl; cin sTemp; stu[r2].SetTelphone(sTemp); } break; case 4: cout "请输入要删除学生的学号:"; int n; cin n; r2=findStudent(stu, r1, n); if (r2==-1) { cout "没有该学生!"; } else { r1=deleteStudent(stu, r1, n); cout "删除成功!" "\n" "显示所有学生" endl; getStudent(stu, r1); } break; case 5: getStudent(stu, r1); break; case 6: cout "您已经成功退出系统,欢迎再次使用!谢谢!" endl; break; default: break; } } return 0; } 结果:
//题目2.在一个程序中,各写出重载覆盖隐藏的成员函数 #include iostream using namespace std;
class A { public: void func() { cout "A的func()" endl; } void func1(int i) { cout "A的func1()" " 参数是:" i endl; } virtual void func2() { cout "A的func2()" endl; } virtual void func2(int i) { cout "A的func2()" " 参数是:" i endl; } };
class B:public A { public: void func() //基类没有virtual关键字就是表示基类被隐藏 { cout "B的func()" endl; } void func2() //基类有virtual关键字就表示基类方法被覆盖 { cout "B的func2()" endl; } void func2(int i) { cout "B的func2()" " 参数:" i endl; } }; int main(int argc, const char * argv[]) { A a1; A *a2= B b1; A *a3= B *b2= a1.func(); //A的func() a1.func1(1); //A的func1() 1 a2- func1(2); //A的func1() 2 a2- func2(3); //A的func2() 3 a2- func(); //A的func() a3- func(); //A的func() a3- func1(4); //A的func1() 4 a3- func2(); //B的func2() a3- func2(5); //B的func2() 5 b1.func(); //B的func() b1.func2(); //B的func2() b2- func(); //B的func() b2- func2(); //B的func2() b2- func1(6); //A的func1() 6 b2- A::func2(7); //A的func2() 7 return 0; } 结果: A的func() A的func1() 参数是:1 A的func1() 参数是:2 A的func2() 参数是:3 A的func() A的func() A的func1() 参数是:4 B的func2() B的func2() 参数:5 B的func() B的func2() B的func() B的func2() A的func1() 参数是:6 A的func2() 参数是:7
A *a = new B(); //先创建B对象,又因为B是继承的A,所以在构造B之前要先构造A,所以输出A,然后构造B,输出B,然后调用func(),输出A::func\n a- func(); //由于a中的func()方法虽然定义的是虚函数,但是由于B类中没有该重名函数,所以直接调用A中的func方法,输出A::func a- func1(); //由于A中的func1方法是虚函数,且B中也有该方法,所以就调用B中的func1方法,输出B::func\n B b; //实例化一个B对象,先实例化他的基类,调用A的构造方法,打印输出A,然后在调用B的构造方法,打印输出B,在调用A中的func方法,打印输出A::func\n b.func(); //调用继承自A中的func方法 } 结果: A B A::func A::func B::func A B A::func A::func
蓬莱仙羽 麦子学院讲师,游戏蛮牛专栏作家,CSDN博客专家,热爱游戏开发,热爱Coding!
class Student { private: int num;//学号 string name;//姓名 int age; //年龄 char sex; //性别 string profession;//专业 string telphone; public: Student() { } Student(int nu,string na,int ag,char sx,string pr,string te) { num = nu; name = na; age = ag; sex = sx; profession = pr; telphone = te; } void SetNum(int nu) { num = nu; } void SetName(string na) { name = na; } void SetAge(int ag) { age = ag; } void SetSex(char sx) { sex = sx; } void SetProfession(string pr) { profession = pr; } void SetTelphone(string te) { telphone = te; } Student* GetStudent() { Student *stu; return stu; } int GetNum() { return num; } string GetName() { return name; } int GetAge() { return age; } char GetSex() { return sex; } string GetProfession() { return profession; } string GetTelphone() { return telphone; } //还要有整体的赋值,可以用单目运算符重写= Student operator = (const Student other) { if (this == other) { return *this; } this- num = other.num; this- name = other.name; this- age = other.age; this- sex = other.sex; this- profession = other.profession; this- telphone = other.telphone; } };
//输入学生,返回输入的个数 int setStudent(Student stu[],int n) { int i=0; int j; int iTemp; string sTemp; char cTemp; int f=0; do { cout "请输入学生学号" endl; cin iTemp; stu[i].SetNum(iTemp); cout "请输入学生姓名" endl; cin sTemp; stu[i].SetName(sTemp); cout "请输入学生年龄" endl; cin iTemp; stu[i].SetAge(iTemp); cout "请输入学生性别" endl; cin cTemp; stu[i].SetSex(cTemp); cout "请输入学生专业" endl; cin sTemp; stu[i].SetProfession(sTemp); cout "请输入学生手机号" endl; cin sTemp; stu[i].SetTelphone(sTemp); i++; cout "请问要输入学生信息吗?(1.要 2.不要)"; cin f; } while (1==f); // for (i=0; i i++) { // printf("请问要输入学生信息吗?(1.要 2.不要)"); // cin j; // if (2==j) { // break; // } // else // { // cout "请输入学生学号" endl; // cin iTemp; // stu[i].SetNum(iTemp); // cout "请输入学生姓名" endl; // cin sTemp; // stu[i].SetName(sTemp); // cout "请输入学生年龄" endl; // cin iTemp; // stu[i].SetAge(iTemp); // cout "请输入学生性别" endl; // cin cTemp; // stu[i].SetSex(cTemp); // cout "请输入学生专业" endl; // cin sTemp; // stu[i].SetProfession(sTemp); // cout "请输入学生手机号" endl; // cin sTemp; // stu[i].SetTelphone(sTemp); // } // } return i;//返回输入的学生数 }
//返回查找学号为什么的学生的下标 int findStudent(Student *stu,int n,int findNum)//要查询的Student数组,实际查询的条数,要查询的学号 { int i; int m=-1; for (i=0; i i++) { if(stu[i].GetNum()==findNum) { m=i; break; } } return m; //返回Stu的下标 }
//删除一条学生信息 int deleteStudent(Student *stu,int n,int deleteStudentNum) //在学生数组中删除学号为deleteStudentNum的学生,并且个数减少1 { int i; int j=0; for (i=0; i i++) { if (stu[i].GetNum()==deleteStudentNum) { for (j=i; j j++) { stu[j]=stu[j+1]; } n--; i--; } } return n; //返回n-1 } Student addStudent() { Student stu; int iTemp; char cTemp; string sTemp; cout "请输入学生学号" endl; cin iTemp; stu.SetNum(iTemp); cout "请输入学生姓名" endl; cin sTemp; stu.SetName(sTemp); cout "请输入学生年龄" endl; cin iTemp; stu.SetAge(iTemp); cout "请输入学生性别" endl; cin cTemp; stu.SetSex(cTemp); cout "请输入学生专业" endl; cin sTemp; stu.SetProfession(sTemp); cout "请输入学生手机号" endl; cin sTemp; stu.SetTelphone(sTemp); return stu; } //添加一个学生(可能有点问题) int insertStudent(Student *stu,int n) { Student s=addStudent(); stu[n] = s; return ++n; }
//修改学生信息 void alterStudent() { }
//打印标题行 void printTitle() { cout "学号 " "姓名 " "年龄 " "性别 " "专业 " "手机号" endl; }
//输出打印学生信息 void getStudent(Student *stu,int n) { int i; printTitle();//打印标题 for (i=0; i i++) { cout stu[i].GetNum() " " stu[i].GetName() " " stu[i].GetAge() " " stu[i].GetSex() " " stu[i].GetProfession() " " stu[i].GetTelphone() " " endl; } } //显示菜单 void showMenu() { cout " 欢迎使用学生信息管理系统 " endl; cout "***********************************************" endl; cout "* 1.初始化学生 2.添加学生 *" endl; cout "* 3.修改学生 4.删除学生 *" endl; cout "* 5.显示所有学生 6.退出 *" endl; cout "***********************************************" endl; } //菜单选择 int get_menu_choice() { int menu_ch; do { cout "选择菜单选项:"; cin menu_ch; if ((menu_ch 0)||(menu_ch 6)) { cout "error!" endl; } } while ((menu_ch 0)||(menu_ch return menu_ch; } int main(int argc, const char * argv[]) {
Student* stu = new Student[NUM]; int r1=0; int r2;//保存查找的学号 int choose; while (1) { showMenu(); switch (get_menu_choice()) { case 1: r1=setStudent(stu,NUM);//返回输入的个数 break; case 2: r1=insertStudent(stu,r1); getStudent(stu, r1); break; case 3: cout "请输入要修改的学生的学号:"; int nn; cin nn; r2=findStudent(stu, r1, nn); if (r2==-1) { cout "没有该学生!"; } else { int iTemp; char cTemp; string sTemp; cout "请输入学生学号" endl; cin iTemp; stu[r2].SetNum(iTemp); cout "请输入学生姓名" endl; cin sTemp; stu[r2].SetName(sTemp); cout "请输入学生年龄" endl; cin iTemp; stu[r2].SetAge(iTemp); cout "请输入学生性别" endl; cin cTemp; stu[r2].SetSex(cTemp); cout "请输入学生专业" endl; cin sTemp; stu[r2].SetProfession(sTemp); cout "请输入学生手机号" endl; cin sTemp; stu[r2].SetTelphone(sTemp); } break; case 4: cout "请输入要删除学生的学号:"; int n; cin n; r2=findStudent(stu, r1, n); if (r2==-1) { cout "没有该学生!"; } else { r1=deleteStudent(stu, r1, n); cout "删除成功!" "\n" "显示所有学生" endl; getStudent(stu, r1); } break; case 5: getStudent(stu, r1); break; case 6: cout "您已经成功退出系统,欢迎再次使用!谢谢!" endl; break; default: break; } } return 0; } 结果:
//题目2.在一个程序中,各写出重载覆盖隐藏的成员函数 #include iostream using namespace std;
class A { public: void func() { cout "A的func()" endl; } void func1(int i) { cout "A的func1()" " 参数是:" i endl; } virtual void func2() { cout "A的func2()" endl; } virtual void func2(int i) { cout "A的func2()" " 参数是:" i endl; } };
class B:public A { public: void func() //基类没有virtual关键字就是表示基类被隐藏 { cout "B的func()" endl; } void func2() //基类有virtual关键字就表示基类方法被覆盖 { cout "B的func2()" endl; } void func2(int i) { cout "B的func2()" " 参数:" i endl; } }; int main(int argc, const char * argv[]) { A a1; A *a2= B b1; A *a3= B *b2= a1.func(); //A的func() a1.func1(1); //A的func1() 1 a2- func1(2); //A的func1() 2 a2- func2(3); //A的func2() 3 a2- func(); //A的func() a3- func(); //A的func() a3- func1(4); //A的func1() 4 a3- func2(); //B的func2() a3- func2(5); //B的func2() 5 b1.func(); //B的func() b1.func2(); //B的func2() b2- func(); //B的func() b2- func2(); //B的func2() b2- func1(6); //A的func1() 6 b2- A::func2(7); //A的func2() 7 return 0; } 结果: A的func() A的func1() 参数是:1 A的func1() 参数是:2 A的func2() 参数是:3 A的func() A的func() A的func1() 参数是:4 B的func2() B的func2() 参数:5 B的func() B的func2() B的func() B的func2() A的func1() 参数是:6 A的func2() 参数是:7
A *a = new B(); //先创建B对象,又因为B是继承的A,所以在构造B之前要先构造A,所以输出A,然后构造B,输出B,然后调用func(),输出A::func\n a- func(); //由于a中的func()方法虽然定义的是虚函数,但是由于B类中没有该重名函数,所以直接调用A中的func方法,输出A::func a- func1(); //由于A中的func1方法是虚函数,且B中也有该方法,所以就调用B中的func1方法,输出B::func\n B b; //实例化一个B对象,先实例化他的基类,调用A的构造方法,打印输出A,然后在调用B的构造方法,打印输出B,在调用A中的func方法,打印输出A::func\n b.func(); //调用继承自A中的func方法 } 结果: A B A::func A::func B::func A B A::func A::func
蓬莱仙羽 麦子学院讲师,游戏蛮牛专栏作家,CSDN博客专家,热爱游戏开发,热爱Coding!
相关文章
- openssl 学习之从证书中提取RSA公钥N 和 E
- java基础语法——断点调试与数据加密(基础语法练习学习)
- Andrew Ng机器学习课程9-补充
- halcon脚本-深度学习【目标检测】
- LWIP应用指南学习。
- Java学习笔记之字节缓冲流&字符流&IO流练习
- K8S学习笔记之二进制部署Kubernetes v1.13.4 高可用集群
- Spring学习12-Spring利用mock进行单元测试
- 2023.1.24,周二【图神经网络 学习记录7】GraphSAGE 练习,PyG学习(部署在之前已经介绍过)
- 【人工智能 & 机器学习 & 深度学习】基础选择题 31~60题 练习(题目+答案),亦含 判断题
- 【人工智能 & 机器学习 & 深度学习】基础选择题1~30题 练习
- 七月算法机器学习4 凸优化初步
- Rasa 3.x 学习系列-Rasa [3.2.9] - 2022-09-09新版本发布
- PyQt(Python+Qt)学习随笔:model/view架构中QTableView视图的标题显示不正常问题
- 2021-10-23 《编译原理》学习笔记:第7周
- 8-20学习练习[用两个tableview实现类似省市联动选择效果]
- 2013-8-6学习练习
- Linux 学习笔记(七)增加硬盘
- python学习之爬虫(六)--selenium: 无头浏览器、selenium的基本使用、selenium元素定位的方法、selenium爬虫案例练习