VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 580|回复: 1

[已解决]这道oj题怎么补充代码

[复制链接]
65_avatar_middle
最佳答案
0 
在线会员 发表于 2022-4-13 14:22:34 | 显示全部楼层 |阅读模式
根据所给代码和输入输出将代码补充完整。

1)定义一个大学生类Student,函数私有数据成员:姓名(name)、学号(StuNum)、校名(universty_name),并为它定义带参数的构造函数、参数带缺省值的构造函数

和输出数据成员值的print()公有成员函数;

2)另定义研究生类(GraStudent),它以公有继承方式派生于类Student,新增加“研究方向(special)、导师名(directorname)”两个私有数据成员,并定义带参数的构造函数和输出研究生数据的print()公有成员函数。



  1. #include<iostream>
  2. using namespace std;
  3. #include<string.h>
  4. class Student
  5. {
  6. protected:
  7.         char *name;
  8.         char *stunum;
  9.         char *uniname;
  10. public:
  11.         Student();
  12.         Student(char *pn, char* ps, char* pu);
  13.         void print();
  14. };
  15. // 在此处补充你的代码
  16. int main()
  17. {
  18.         Student stu1("Li","1600141","CUC");
  19.         stu1.print();
  20.         GraStudent gstu("Wang","1600240","CUC","Computer","Zhang");
  21.     gstu.print();
  22.     return 0;
  23. }
复制代码

输入

输出
第一行:大学生的姓名
第二行:大学生的学号
第三行:大学生的校名
第四行:研究生的姓名
第五行:研究生的学号
第六行:研究生的校名
第七行:研究生的研究方向
第八行:研究生的导师姓名
样例输入
NULL
样例输出
name=Li
StuNum=1600141
universty_name=CUC
name=Wang
StuNum=1600240
universty_name=CUC
special is Computer
director is Zhang
最佳答案
57_avatar_small
2022-4-14 17:38:15
本帖最后由 JLDawson 于 2022-4-14 17:42 编辑

私有数据成员,在给出的代码中为保护数据成员,对此进行调整。
它定义带参数的构造函数、参数带缺省值的构造函数。这句话我不李姐
  1. #include<iostream>
  2. #include<string.h>

  3. using namespace std;

  4. class Student
  5. {

  6. public:
  7.         Student(string name, string StuNum, string universty_name);
  8.         Student();

  9.         void print();

  10. private:
  11.         string name;
  12.         string StuNum;
  13.         string universty_name;
  14. };

  15. Student::Student(string name, string StuNum, string universty_name):name(name), StuNum(StuNum), universty_name(universty_name){
  16. }

  17. Student::Student() : name(""), StuNum(""), universty_name("") {
  18. }

  19. void Student::print() {
  20.         cout << "name=" << name.c_str() << endl;
  21.         cout << "StuNum=" << StuNum.c_str() << endl;
  22.         cout << "universty_name=" << universty_name.c_str() << endl;
  23. }

  24. class GraStudent:public Student
  25. {
  26. public:
  27.         GraStudent(string name, string StuNum, string universty_name, string special, string directorname);

  28.         void print();

  29. private:
  30.         string special;
  31.         string directorname;
  32. };

  33. GraStudent::GraStudent(string name, string StuNum, string universty_name, string special, string directorname) :Student(name, StuNum, universty_name), special(special), directorname(directorname){
  34. }

  35. void GraStudent::print() {
  36.         Student::print();
  37.         cout << "special is " << special.c_str() << endl;
  38.         cout << "director is " << directorname.c_str() << endl;
  39. }

  40. int main()
  41. {
  42.         Student stu1("Li", "1600141", "CUC");
  43.         stu1.print();
  44.         GraStudent gstu("Wang", "1600240", "CUC", "Computer", "Zhang");
  45.         gstu.print();
  46.         return 0;
  47. }
复制代码




上一篇:一个算法题, 规定机器人在一个区间内行动一定步数来到某个位置,使用递归算法解决
下一篇:关于多个Png如何制作成bmp格式
57_avatar_middle
最佳答案
25 
在线会员 发表于 2022-4-14 17:38:15 | 显示全部楼层    本楼为最佳答案   
bestAnswer
本帖最后由 JLDawson 于 2022-4-14 17:42 编辑

私有数据成员,在给出的代码中为保护数据成员,对此进行调整。
它定义带参数的构造函数、参数带缺省值的构造函数。这句话我不李姐
  1. #include<iostream>
  2. #include<string.h>

  3. using namespace std;

  4. class Student
  5. {

  6. public:
  7.         Student(string name, string StuNum, string universty_name);
  8.         Student();

  9.         void print();

  10. private:
  11.         string name;
  12.         string StuNum;
  13.         string universty_name;
  14. };

  15. Student::Student(string name, string StuNum, string universty_name):name(name), StuNum(StuNum), universty_name(universty_name){
  16. }

  17. Student::Student() : name(""), StuNum(""), universty_name("") {
  18. }

  19. void Student::print() {
  20.         cout << "name=" << name.c_str() << endl;
  21.         cout << "StuNum=" << StuNum.c_str() << endl;
  22.         cout << "universty_name=" << universty_name.c_str() << endl;
  23. }

  24. class GraStudent:public Student
  25. {
  26. public:
  27.         GraStudent(string name, string StuNum, string universty_name, string special, string directorname);

  28.         void print();

  29. private:
  30.         string special;
  31.         string directorname;
  32. };

  33. GraStudent::GraStudent(string name, string StuNum, string universty_name, string special, string directorname) :Student(name, StuNum, universty_name), special(special), directorname(directorname){
  34. }

  35. void GraStudent::print() {
  36.         Student::print();
  37.         cout << "special is " << special.c_str() << endl;
  38.         cout << "director is " << directorname.c_str() << endl;
  39. }

  40. int main()
  41. {
  42.         Student stu1("Li", "1600141", "CUC");
  43.         stu1.print();
  44.         GraStudent gstu("Wang", "1600240", "CUC", "Computer", "Zhang");
  45.         gstu.print();
  46.         return 0;
  47. }
复制代码
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-10-1 04:35

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表