C++第五章

  1. #include "iostream"  
  2. #include "string"  
  3.   
  4. using std::cin;  
  5. using std::cout;  
  6. using std::endl;  
  7. using std::string;  
  8.   
  9. class Student  
  10. {  
  11. protected:  
  12.   int num;  
  13.   string name;  
  14.   char sex;  
  15.   
  16. public:  
  17.   void prit();  
  18.   void display();  
  19.   
  20. };  
  21.   
  22. void Student::prit()  
  23. {  
  24.   cin >> num >> name >> sex;  
  25. }  
  26.   
  27. void Student::display()  
  28. {  
  29.   cout << num << " " << name << " " << sex;  
  30. }  
  31.   
  32. class Studet1 : public Student  
  33. {  
  34. protected:  
  35.   int age;  
  36.   string addr;  
  37.   
  38. public:  
  39.   void prit();  
  40.   void display();  
  41. };  
  42.   
  43. void Studet1::prit()  
  44. {  
  45.   Student::prit();  
  46.   cin >> age >> addr;  
  47. }  
  48.   
  49. void Studet1::display()  
  50. {  
  51.   Student::display();  
  52.   cout << " ";  
  53.   cout << age << " " << addr;  
  54. }  
  55.   
  56. int main(int argc, char const *argv[])  
  57. {  
  58.   Studet1 A;  
  59.   A.prit();  
  60.   A.display();  
  61.   cout << endl;  
  62.   return 0;  
  63. }  


第二题

[cpp]  view plain copy
  1. #include "iostream"  
  2. #include "string"  
  3.   
  4. using std::cin;  
  5. using std::cout;  
  6. using std::endl;  
  7. using std::string;  
  8.   
  9. class Student  
  10. {  
  11. protected:  
  12.   int num;  
  13.   string name;  
  14.   char sex;  
  15.   
  16. public:  
  17.   void prit();  
  18.   void display();  
  19.   
  20. };  
  21.   
  22. void Student::prit()  
  23. {  
  24.   cin >> num >> name >> sex;  
  25. }  
  26.   
  27. void Student::display()  
  28. {  
  29.   cout << num << " " << name << " " << sex;  
  30. }  
  31.   
  32. class Studet1 : private Student  
  33. {  
  34. protected:  
  35.   int age;  
  36.   string addr;  
  37.   
  38. public:  
  39.   void prit();  
  40.   void display();  
  41. };  
  42.   
  43. void Studet1::prit()  
  44. {  
  45.   cin >> num >> name >> sex;  
  46.   cin >> age >> addr;  
  47. }  
  48.   
  49. void Studet1::display()  
  50. {  
  51.   cout << num << " " << name << " " << sex;  
  52.   cout << " ";  
  53.   cout << age << " " << addr;  
  54. }  
  55.   
  56. int main(int argc, char const *argv[])  
  57. {  
  58.   Studet1 A;  
  59.   A.prit();  
  60.   A.display();  
  61.   cout << endl;  
  62.   return 0;  
  63. }  



第三题

[cpp]  view plain copy
  1. #include "iostream"  
  2. #include "string"  
  3.   
  4. using std::cin;  
  5. using std::cout;  
  6. using std::endl;  
  7. using std::string;  
  8.   
  9. class Student  
  10. {  
  11. protected:  
  12.   int num;  
  13.   string name;  
  14.   char sex;  
  15.   
  16. public:  
  17.   void prit();  
  18.   void display();  
  19.   
  20. };  
  21.   
  22. void Student::prit()  
  23. {  
  24.   cin >> num >> name >> sex;  
  25. }  
  26.   
  27. void Student::display()  
  28. {  
  29.   cout << num << " " << name << " " << sex;  
  30. }  
  31.   
  32. class Studet1 : protected Student  
  33. {  
  34. protected:  
  35.   int age;  
  36.   string addr;  
  37.   
  38. public:  
  39.   void prit();  
  40.   void display();  
  41. };  
  42.   
  43. void Studet1::prit()  
  44. {  
  45.   cin >> num >> name >> sex;  
  46.   cin >> age >> addr;  
  47. }  
  48.   
  49. void Studet1::display()  
  50. {  
  51.   cout << num << " " << name << " " << sex;  
  52.   cout << " ";  
  53.   cout << age << " " << addr;  
  54. }  
  55.   
  56. int main(int argc, char const *argv[])  
  57. {  
  58.   Studet1 A;  
  59.   A.prit();  
  60.   A.display();  
  61.   cout << endl;  
  62.   return 0;  
  63. }  



第9题

[cpp]  view plain copy
  1. #include "iostream"  
  2. #include "string"  
  3.   
  4. using std::cin;  
  5. using std::cout;  
  6. using std::endl;  
  7. using std::string;  
  8.   
  9.   
  10. class Teacher  
  11. {  
  12. protected:  
  13.   string name;  
  14.   int age;  
  15.   char sex;  
  16.   string addr;  
  17.   int tel;  
  18.   string title;  
  19.   
  20. public:  
  21.   Teacher(string, intchar, string, int, string);  
  22.   void display();  
  23. };  
  24.   
  25. Teacher::Teacher(string na, int ag, char se, string ad, int te, string ti)  
  26. :name(na), age(ag), sex(se), addr(ad), tel(te), title(ti)  
  27. {}  
  28.   
  29. void Teacher::display()  
  30. {  
  31.   cout << name << " " << age << " " << sex << " " << addr << " " << tel << " " << title;  
  32. }  
  33.   
  34. class Cadre  
  35. {  
  36. protected:  
  37.   string name;  
  38.   int age;  
  39.   char sex;  
  40.   string addr;  
  41.   int tel;  
  42.     
  43.   
  44. public:  
  45.   string post;  
  46.   Cadre(string na, int ag, char se, string ad, int te,string po);  
  47. };  
  48.   
  49. Cadre::Cadre(string na, int ag, char se, string ad, int te, string po)  
  50. :name(na), age(ag), sex(se), addr(ad), tel(te), post(po)  
  51. {}  
  52.   
  53. class Teacher_Cadre : public Teacher, public Cadre  
  54. {  
  55. protected:  
  56.     
  57.   
  58. public:  
  59.   int wages;  
  60.   Teacher_Cadre(string na, int ag, char se, string ad, int te, string ti, string po, int wa);  
  61. };  
  62.   
  63. Teacher_Cadre::Teacher_Cadre(string na, int ag, char se, string ad, int te, string ti, string po, int wa)  
  64. :Teacher(na, ag, se, ad, te, ti), Cadre(na, ag, se, ad, te, po), wages(wa)  
  65. {}  
  66.   
  67. int main(int argc, char const *argv[])  
  68. {  
  69.   Teacher_Cadre T("nihao", 18, 'M'"江西财经大学", 12345, "zhicheng""zhiwu", 123456);  
  70.   T.display();  
  71.   cout << " ";  
  72.   cout << T.post << " " << T.wages << endl;  
  73.   
  74.   return 0;  
  75. }  




第10题

[cpp]  view plain copy
  1. #include "iostream"  
  2. #include "string"  
  3.   
  4.   
  5. using std::cin;  
  6. using std::cout;  
  7. using std::endl;  
  8. using std::string;  
  9.   
  10. class Teacher  
  11. {  
  12. protected:  
  13.   int num;  
  14.   string name;  
  15.   char sex;  
  16.   
  17.   
  18. public:  
  19.   Teacher(int, string, char);  
  20.   void display();  
  21. };  
  22.   
  23. Teacher::Teacher(int n, string na, char s)  
  24. :num(n), name(na), sex(s)  
  25. {}  
  26.   
  27. void Teacher::display()  
  28. {  
  29.   cout << num << " " << name << " " << sex;  
  30. }  
  31.   
  32. class BirthDate  
  33. {  
  34. protected:  
  35.   int year;  
  36.   int month;  
  37.   int day;  
  38.   
  39. public:  
  40.   BirthDate(intintint);  
  41.   void change();  
  42.   void display();  
  43. };  
  44.   
  45. BirthDate::BirthDate(int y, int m, int d)  
  46. :year(y), month(m), day(d)  
  47. {}  
  48.   
  49. void BirthDate::display()  
  50. {  
  51.   cout << year << " " << month << " " << day;  
  52. }  
  53.   
  54. void BirthDate::change()  
  55. {  
  56.   cin >> year >> month >> day;  
  57. }  
  58.   
  59. class Professor : public Teacher  
  60. {  
  61. protected:  
  62.   BirthDate birthday;  
  63.   
  64.   
  65. public:  
  66.   void change();  
  67.   Professor(int, string, char,intintint);  
  68.   void display();  
  69. };  
  70.   
  71. void Professor::change()  
  72. {  
  73.   birthday.change();  
  74. }  
  75.   
  76. Professor::Professor(int n, string na, char s, int y, int m, int d)  
  77. :Teacher(n, na, s), birthday(y, m, d)  
  78. {  
  79.   
  80. }  
  81.   
  82. void Professor::display()  
  83. {  
  84.   Teacher::display();  
  85.   cout << " ";  
  86.   birthday.display();  
  87.   cout << endl;  
  88.   
  89. }  
  90.   
  91. int main(int argc, char const *argv[])  
  92. {  
  93.   Professor profl(123, "nihao"'M', 1995, 11, 12);  
  94.   profl.display();  
  95.   profl.change();  
  96.   profl.display();  
  97.   return 0;  
  98. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值