C++第七次实验---作业报告

来源:互联网 发布:如何让牙齿整齐 知乎 编辑:程序博客网 时间:2024/06/05 02:04

【项目1 - 龙三】

一、问题及代码

[cpp] view plain copy
  1. /* 文件名称:Ex7-1.cpp   
  2. * 作    者:程家琦   
  3. * 完成日期:2017 年 5 月 21 日   
  4. * 版 本 号:v1.0   
  5. * 对任务及求解方法的描述部分:请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:
  6. Name: 龙三
  7. Grade: 19
  8. * 输入描述:无   
  9. * 问题描述:   
  10. * 算法设计:略   
  11. */   
  1. #include <iostream>    
  2. #include <string>    
  3. using namespace std;    
  4. class Person{    
  5. public:    
  6.     Person(string s){    
  7.         name=s;    
  8.     }    
  9.     void display( ){    
  10.         cout<<"Name: "<<name<<endl;    
  11.     }    
  12. private:    
  13.     string name;    
  14. };    
  15. class Student:public Person  
  16. {    
  17. public:    
  18.     Student(string s, int g):Person(s) // (2)参考教材P169加底纹部分    
  19.     {grade=g;}    
  20.     void display1( ) {    
  21.         Person::display();   //  (3)    
  22.         cout<<"Grade: "<<grade<<endl;    
  23.     }    
  24. private:    
  25.     int grade;    
  26. };    
  27. int main( )    
  28. {    
  29.     Student s("龙三",19);    
  30.     s.display1();   
  31.     return 0;    
  32. }    

二、运行结果





【项目3 - 职员薪水】

一、问题及代码

[cpp] view plain copy
  1. /* 文件名称:项目2.cpp   
  2. * 作    者:严明远   
  3. * 完成日期:2017 年 5 月 19 日   
  4. * 版 本 号:v1.0   
  5. * 对任务及求解方法的描述部分:   
  6. * 输入描述:无   
  7. * 问题描述:定义一个名为CPerson的类,有以下私有成员:姓名、身份证号、性别和年龄,成员函数:构造函数、输出信息的函数。并在此基础上派生出CEmployee类,派生类CEmployee增加了两个新的数据成员,分别用于表示部门和薪水。要求派生类CEmployee的构造函数显示调用基类CPerson的构造函数,并为派生类CEmployee定义输出信息的函数。 
  8. * 算法设计:略   
  9. */   
  10. #include<iostream>  
  11. #include<string>  
  12. using namespace std;  
  13. class CPerson    
  14. {    
  15. protected:    
  16.     string m_szName;    
  17.     string m_szId;    
  18.     int m_nSex;    //0:女,1:男    
  19.     int m_nAge;    
  20. public:    
  21.     CPerson(string name,string id,int sex,int age);    
  22.     void Show1();    
  23. };    
  24. CPerson::CPerson(string name,string id,int sex,int age)  
  25. {  
  26.     m_szName=name;  
  27.     m_szId=id;  
  28.     m_nSex=sex;  
  29.     m_nAge=age;  
  30. }  
  31. void CPerson::Show1()  
  32. {  
  33.     cout<<"姓名\t"<<"ID\t"<<"性别\t"<<"年龄\t";  
  34.     //cout<<m_szName<<'\t'<<m_szId<<'\t'<<m_szId<<'\t'<<m_nAge<<'\t';  
  35. }  
  36.   
  37. class CEmployee:public CPerson    
  38. {    
  39. private:    
  40.     string m_szDepartment;    
  41.     double m_Salary;    
  42. public:    
  43.     CEmployee(string name,string id,int sex,int age,string department,double salary):  
  44.       CPerson(name,id,sex,age),m_szDepartment(department),m_Salary(salary){}    
  45.     void Show2();    
  46. };    
  47. void CEmployee::Show2()  
  48. {  
  49.     CPerson::Show1();  
  50.     cout<<"部门"<<'\t'<<"薪水"<<'\t'<<endl;  
  51.     cout<<m_szName<<'\t'<<m_szId<<'\t'<<m_szId<<'\t'<<m_nAge<<'\t';  
  52.     cout<<m_szDepartment<<'\t'<<m_Salary<<endl;  
  53. }  
  54. int main()    
  55. {    
  56.     string name,id,department;    
  57.     int sex,age;    
  58.     double salary;    
  59.     cout<<"请输入雇员的姓名,ID,性别(0:女,1:男),年龄,部门,薪水:\n";    
  60.     cin>>name>>id>>sex>>age>>department>>salary;    
  61.     CEmployee employee1(name,id,sex,age,department,salary);    
  62.     employee1.Show2();    
  63.     return 0;    
  64. }    

二、运行结果


原创粉丝点击