c++第四次作业 继承和派生

  1. [cpp] view plain copy 在CODE上查看代码片派生到我的代码片  
  2. /*    
  3. * 文件名称:hj.cpp    
  4. * 作    者:仲海亮 
  5. * 完成日期:2015 年 5月 6 日    
  6. * 版 本 号:v1.0    
  7. * 对任务及求解方法的描述部分:   
  8. * 输入描述:无    
  9. * 问题描述:  
  10. * 程序输出:   
  11. * 问题分析:略   
  12. * 算法设计:略    
  13. */        
  14.     
  15. #include <iostream>        
  16. #include <string>        
  17. using namespace std;        
  18. class Person      
  19. {        
  20. public:        
  21.     Person(string s)      
  22.     {        
  23.         name=s;        
  24.     }        
  25.    void display( )      
  26.     {        
  27.         cout<<"Name: "<<name<<endl;        
  28.     }        
  29. private:        
  30.     string name;        
  31. };        
  32. class Student:public Person//(1)        
  33. {        
  34. public:        
  35.     Student(string s, int g):Person(s)// (2)参考教材P169加底纹部分        
  36.     {grade=g;}        
  37.     void display1( )      
  38.     {        
  39.         display();   //  (3)        
  40.         cout<<"Grade: "<<grade<<endl;        
  41.     }        
  42. private:        
  43.     int grade;        
  44. };        
  45. int main( )        
  46. {        
  47.     Student s("龙三",19);        
  48.     s.display1();       //  (4)        
  49.     return 0;        
  50. }        

二.输出结果

项目三——职员薪水

一、问题及代码

  定义一个名为CPerson的类,有以下私有成员:姓名、身份证号、性别和年龄,成员函数:构造函数、输出信息的函数。并在此基础上派生出CEmployee类,派生类CEmployee增加了两个新的数据成员,分别用于表示部门和薪水。要求派生类CEmployee的构造函数显示调用基类CPerson的构造函数,并为派生类CEmployee定义输出信息的函数。

[cpp]  view plain  copy
  1. cpp] view plain copy  
  2. /*    
  3. * 文件名称:hwj.cpp    
  4. * 作    者:孙红亮 
  5. * 完成日期:2015 年 4月 23 日    
  6. * 版 本 号:v1.0    
  7. * 对任务及求解方法的描述部分:   
  8. * 输入描述:无    
  9. * 问题描述:  
  10. * 程序输出:   
  11. * 问题分析:略   
  12. * 算法设计:略    
  13. */    
  14. #include<iostream>      
  15. #include<string>      
  16. using namespace std;      
  17. class CPerson      
  18. {      
  19. protected:      
  20.     string m_szName;      
  21.     string m_szId;      
  22.     int m_nSex;//0:女,1:男      
  23.     int m_nAge;     
  24. public:      
  25.     CPerson(string name,string id,int sex,int age);      
  26.     void Show1();      
  27. };      
  28. CPerson::CPerson(string name,string id,int sex,int age)      
  29. {      
  30.     m_szName=name;      
  31.     m_szId=id;      
  32.     m_nSex=sex;      
  33.     m_nAge=age;      
  34. }      
  35. void CPerson::Show1()      
  36. {      
  37.     cout<<m_szName<<" "<<m_szId<<"  "<<m_nSex<<" "<<m_nAge<<"  ";      
  38. }      
  39.     
  40. class CEmployee:public CPerson      
  41. {      
  42. private:      
  43.     string m_szDepartment;      
  44.     double m_Salary;      
  45. public:      
  46.     CEmployee(string name,string id,int sex,int age,string department,double salary);      
  47.     void Show2();      
  48. };      
  49. CEmployee::CEmployee(string name,string id,int sex,int age,string department,double salary):CPerson(name,id,sex,age)      
  50. {      
  51.     m_szDepartment=department;      
  52.     m_Salary=salary;      
  53. }      
  54. void CEmployee::Show2()      
  55. {      
  56.     cout<<"姓名"<<" "<<"ID"<<" "<<"性别"<<" "<<"年龄"<<" "<<"部门"<<" "<<"薪水"<<endl;      
  57.     Show1();      
  58.     cout<<m_szDepartment<<"  "<<m_Salary<<endl;      
  59. }      
  60. int main()      
  61. {      
  62.     string name,id,department;      
  63.     int sex,age;      
  64.     double salary;      
  65.     cout<<"请输入雇员的姓名,ID,性别(0:女,1:男),年龄,部门,薪水:\n";      
  66.     cin>>name>>id>>sex>>age>>department>>salary;      
  67.     CEmployee employee1(name,id,sex,age,department,salary);      
  68.     employee1.Show2();      
  69.     return 0;      
  70. }      
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值