请选择 进入手机版 | 继续访问电脑版
查看: 481|回复: 0

[软件工程] 多态练习,顺便练习一下常量成员函数

725

主题

725

帖子

2188

积分

猿er

Rank: 1

积分
2188
发表于 2016-8-14 17:54:14
这是练习c++多态的例子,基于VS2013编译的,里面包含大量的常量成员函数,既可以用来练习多态之虚函数,虚基类,又可以用来理解常量成员函数。
  1. #include<iostream>
  2. using namespace std;
  3. #include<iomanip>
  4. using std::setw;
  5. #include<string>
  6. using std::string;
  7. #include"Employee.h"
  8. #include"SalariedEmployee.h"
  9. #include"CommissionEmployee.h"
  10. #include"HourlyEmployee.h"
  11. #include"BasePlusCommissionEmployee.h"
  12. //double Add(double a, double b)//可以接收常量
  13. //{
  14. // double c;
  15. // c = a + b;
  16. // return c;
  17. //}
  18. //double Add(const double a, const double b)//函数可以接收常量
  19. //const修饰函数返回值,一般函数返回值是指针或者引用时用const修饰返回值有意义,
  20. //但是,若函数以按值传递方式返回函数值,则加const修饰返回值没什么意义,因为返回值会被复制到
  21. //临时开辟的内存空间。
  22. //{
  23. // double c;
  24. // return (c = a + b);
  25. //}
  26. //class A
  27. //{
  28. //public:
  29. // A()
  30. // {
  31. // cout << "This is an empty string!" << endl;
  32. // }
  33. //
  34. // A(const string &ms)
  35. // {
  36. // SetMyString(ms);
  37. // }
  38. // double Add(const double a, const double b) const
  39. // //常量成员函数只能修改类的常量成员!
  40. // //这句话表达了两个意思:
  41. // //(1)常量成员函数只能访问类的常量成员函数!
  42. // //(2)常量成员函数不能修改类的数据成员!
  43. // //由(1)(2)可知,常量成员函数只是限定类的成员的修改权限!
  44. // //因为a,b,c不是类的数据成员,所以可以修改!
  45. // {
  46. // double c;
  47. // return (c = a + b);
  48. // }
  49. //
  50. // void SetMyString(const string &ms)
  51. // {
  52. // MyString = ms;
  53. // }
  54. //
  55. // string GetMyString() const
  56. // {
  57. // return MyString;
  58. // }
  59. //
  60. //private:
  61. // string MyString;
  62. //};
  63. int main()
  64. {
  65. /*A aa;
  66. cout << aa.GetMyString();*/
  67. //SalariedEmployee mySalariedEmployee("马云","计算机");
  68. //CommissionEmployee myCommissionEmployee("马化腾","网络",10000, 0.7);
  69. //const CommissionEmployee cCommissionEmployee("习近平","国家主席",30000,1);
  70. //CommissionEmployee rCommissionEmployee(cCommissionEmployee);
  71. //const SalariedEmployee cSalariedEmployee("马赛克","日本",1000);
  72. //SalariedEmployee rSalariedEmployee(cSalariedEmployee);
  73. //Employee* EmployeePtr = &mySalariedEmployee;
  74. //EmployeePtr->print();//调用派生类成员函数
  75. //cout << EmployeePtr->GetName() << endl;
  76. //cCommissionEmployee.print();
  77. //rCommissionEmployee.print();
  78. //cSalariedEmployee.print();
  79. //rSalariedEmployee.print();
  80. //mySalariedEmployee.print();
  81. //cout << myCommissionEmployee.GetRever() << ' ' << myCommissionEmployee.GetRatio() << endl;
  82. //cout << myCommissionEmployee.GetSalary() << endl;
  83. //myCommissionEmployee.print();
  84. //double a = 1, b = 1;
  85. //cout << "1 + 1 = " << Add(a, b) << endl;
  86. /*cout << Add(1, 1) << " " << Add(a, b) << endl;
  87. cout << aa.Add(1, 1) << endl;*/
  88. BasePlusCommissionEmployee baseplus("温家宝", "总理",20000,1);
  89. baseplus.print();
  90. return 0;
  91. }
复制代码
  1. #ifndef BASEPLUECOMMISSIONEMPLOYEE_H
  2. #define BASEPLUECOMMISSIONEMPLOYEE_H
  3. #include<string>
  4. using std::string;
  5. #include"Employee.h"
  6. #include"CommissionEmployee.h"
  7. class BasePlusCommissionEmployee : public CommissionEmployee
  8. {
  9. public:
  10. BasePlusCommissionEmployee(const string &, const string &, double commission = 0, double ratio = 0);
  11. BasePlusCommissionEmployee(const BasePlusCommissionEmployee &);
  12. void print() const;
  13. double earnings() const;
  14. private:
  15. const double _BaseSalary = 10000;
  16. };
  17. #endif
复制代码
  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. using std::string;
  5. #include<iomanip>
  6. using std::setw;
  7. #include"BasePlusCommissionEmployee.h"
  8. BasePlusCommissionEmployee::BasePlusCommissionEmployee(const string &name, const string &pro,
  9. double commission, double ratio)
  10. : CommissionEmployee(name, pro, commission, ratio)
  11. {}
  12. BasePlusCommissionEmployee::BasePlusCommissionEmployee(const BasePlusCommissionEmployee &BPCE)
  13. : CommissionEmployee(BPCE.GetName(),BPCE.GetPro(),BPCE.GetRever(),BPCE.GetRatio())
  14. {}
  15. void BasePlusCommissionEmployee::print() const
  16. {
  17. cout << endl;
  18. cout.fill(' ');
  19. cout << left;
  20. cout << setw(15) << "姓名:" << setw(15) << "工种:" << setw(15) << "基本工资:" << setw(15) << "销售额:"
  21. << setw(15) << "佣金比:" << setw(15) << "工资:" << endl;
  22. cout << setw(15) << GetName() << setw(15) << GetPro() << setw(15) << _BaseSalary << setw(15) << GetRever()
  23. << setw(15) << GetRatio() << setw(15) << earnings() << endl;
  24. cout << endl;
  25. }
  26. double BasePlusCommissionEmployee::earnings() const
  27. {
  28. return _BaseSalary + GetRever() * GetRatio();
  29. }
复制代码
  1. #ifndef COMMISSIONEMPLOYEE_H
  2. #define COMMISSIONEMPLOYEE_H
  3. #include<string>
  4. using std::string;
  5. #include"Employee.h"
  6. class CommissionEmployee : public Employee
  7. {
  8. public:
  9. CommissionEmployee(const string &, const string &, double revernue = 0, double ratio = 0);
  10. CommissionEmployee(const CommissionEmployee &);
  11. void SetRever(double rever = 0);
  12. double GetRever() const;
  13. void SetRatio(double ratio = 0);
  14. double GetRatio() const;
  15. virtual double earnings() const;
  16. virtual void print() const;
  17. private:
  18. double _Revernue;//ÏúÊÛ¶î
  19. double _Ratio;//Ó¶½ð±È
  20. };
  21. #endif
复制代码
  1. #include<iostream>
  2. using namespace std;
  3. #include<iomanip>
  4. using std::setw;
  5. #include<string>
  6. using std::string;
  7. #include"CommissionEmployee.h"
  8. CommissionEmployee::CommissionEmployee(const string &name, const string &pro, double revernue, double ratio)
  9. : Employee(name, pro)
  10. {
  11. SetRever(revernue);
  12. SetRatio(ratio);
  13. }
  14. CommissionEmployee::CommissionEmployee(const CommissionEmployee &object)
  15. : Employee(object.GetName(), object.GetPro())
  16. {
  17. SetRever(object.GetRever());
  18. SetRatio(object.GetRatio());
  19. }
  20. void CommissionEmployee::SetRever(double rever)
  21. {
  22. _Revernue = (rever < 0) ? 0 : rever;
  23. }
  24. double CommissionEmployee::GetRever() const
  25. {
  26. return _Revernue;
  27. }
  28. void CommissionEmployee::SetRatio(double ratio)
  29. {
  30. _Ratio = ( ratio < 0 ) ? 0 : ratio;
  31. }
  32. double CommissionEmployee::GetRatio() const
  33. {
  34. return _Ratio;
  35. }
  36. void CommissionEmployee::print() const
  37. {
  38. cout << endl;
  39. cout << left;
  40. cout.fill(' ');
  41. cout << setw(15) << "姓名:" << setw(15) << "工种:" << setw(15) << "销售额:"
  42. << setw(15) << "用金比:" << setw(15) << "工资:" << endl;
  43. cout << setw(15) << GetName() << setw(15) << GetPro() << setw(15) << GetRever()
  44. << setw(15) << GetRatio() << setw(15) << earnings() << endl;
  45. cout << endl;
  46. }
  47. double CommissionEmployee::earnings() const
  48. {
  49. return GetRever() * GetRatio();
  50. }
复制代码
  1. #ifndef EMPLOYEE_H
  2. #define EMPLOYEE_H
  3. #include<string>
  4. using std::string;
  5. class Employee
  6. {
  7. public:
  8. Employee(const string &, const string &);//构造函数
  9. Employee(const Employee &);//复制构造函数
  10. void SetName(string const &);//设置姓名
  11. string GetName() const;
  12. void SetPro(string const &);//设置工种
  13. string GetPro() const;
  14. virtual void print()const = 0;//打印员工信息
  15. virtual double earnings()const = 0;//员工工资
  16. private:
  17. //类数据成员,以下划线开头
  18. string _Name;//姓名
  19. string _Profession;//工种
  20. };
  21. #endif
复制代码
  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. using std::string;
  5. #include"Employee.h"
  6. Employee::Employee(string const &name, string const &pro)
  7. {
  8. SetName(name);
  9. SetPro(pro);
  10. }
  11. Employee::Employee(const Employee &myEmployee)
  12. {
  13. SetName(myEmployee._Name);
  14. SetPro(myEmployee._Profession);
  15. }
  16. void Employee::SetName(string const &name)
  17. {
  18. _Name = name;
  19. }
  20. string Employee::GetName() const
  21. {
  22. return _Name;
  23. }
  24. void Employee::SetPro(const string &pro)
  25. {
  26. _Profession = pro;
  27. }
  28. string Employee::GetPro() const
  29. {
  30. return _Profession;
  31. }
复制代码
  1. #ifndef HOURLYEMPLOYEE_H
  2. #define HOURLYEMPLOYEE_H
  3. #include<string>
  4. using std::string;
  5. #include"Employee.h"
  6. class HourlyEmployee : public Employee
  7. {
  8. public:
  9. HourlyEmployee(const string &, const string &, double, double );
  10. HourlyEmployee(const HourlyEmployee &);
  11. void SetWorkH(double);
  12. double GetWorkH() const;
  13. void SetHourlyS(double);
  14. double GetHourlyS() const;
  15. virtual void print() const;
  16. virtual double earnings() const;
  17. private:
  18. double _WorkHour;//工作时间
  19. double _HourlySalary;//小时工资
  20. };
  21. #endif
复制代码
  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. using std::string;
  5. #include<iomanip>
  6. using std::setw;
  7. #include"HourlyEmployee.h"
  8. HourlyEmployee::HourlyEmployee(const string &name, const string &pro, double wh, double hs)
  9. : Employee(name, pro)
  10. {
  11. SetHourlyS(hs);
  12. SetWorkH(wh);
  13. }
  14. HourlyEmployee::HourlyEmployee(const HourlyEmployee &HE)
  15. : Employee(HE.GetName(), HE.GetPro())
  16. {
  17. SetHourlyS(HE.GetHourlyS());
  18. SetWorkH(HE.GetWorkH());
  19. }
  20. void HourlyEmployee::SetWorkH(double wh)
  21. {
  22. _WorkHour = (wh < 0) ? 0 : wh;
  23. }
  24. double HourlyEmployee::GetWorkH() const
  25. {
  26. return _WorkHour;
  27. }
  28. void HourlyEmployee::SetHourlyS(double hs)
  29. {
  30. _HourlySalary = (hs < 0) ? 0 : hs;
  31. }
  32. double HourlyEmployee::GetHourlyS() const
  33. {
  34. return _HourlySalary;
  35. }
  36. void HourlyEmployee::print() const
  37. {
  38. cout << endl;
  39. cout.fill(' ');
  40. cout << left;
  41. cout << setw(15) << "姓名:" << setw(15) << "工种:" << setw(15) << "一周工作时间:" << setw(15) << "小时工资:"
  42. << setw(15) << "工资:" << endl;
  43. cout << setw(15) << GetName() << setw(15) << GetPro() << setw(15) << GetWorkH() << setw(15) << GetHourlyS()
  44. << setw(15) << earnings() << endl;
  45. cout << endl;
  46. }
  47. double HourlyEmployee::earnings() const
  48. {
  49. return GetWorkH() * GetHourlyS();
  50. }
复制代码
  1. #ifndef SALARIEDEMPLOYEE_H
  2. #define SALARIEDEMPLOYEE_H
  3. #include"Employee.h"
  4. #include<string>
  5. using std::string;
  6. class SalariedEmployee : public Employee
  7. {
  8. public:
  9. SalariedEmployee(const string &, const string &, double salary = 0);//构造函数
  10. SalariedEmployee(const SalariedEmployee & );//复制构造函数
  11. void SetSalary(double salary = 0);
  12. double GetSalary() const;
  13. virtual double earnings() const;
  14. virtual void print() const;
  15. private:
  16. double _WeeklySalary;
  17. };
  18. #endif
复制代码
  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. using std::string;
  5. #include<iomanip>
  6. using std::setw;
  7. #include"SalariedEmployee.h"
  8. SalariedEmployee::SalariedEmployee(string const &name, string const &pro, double salary)//构造函数
  9. : Employee(name,pro)
  10. {
  11. SetSalary(salary);
  12. }
  13. SalariedEmployee::SalariedEmployee(const SalariedEmployee &mySalariedEmployee)//复制构造函数
  14. :Employee(mySalariedEmployee.GetName(), mySalariedEmployee.GetPro())
  15. //派生类的成员函数无法访问基类私有数据成员!!!!!!!
  16. {
  17. SetSalary(mySalariedEmployee._WeeklySalary);
  18. }
  19. void SalariedEmployee::SetSalary(double salary)//设置工资
  20. {
  21. _WeeklySalary = (salary < 0) ? 0 : salary;
  22. }
  23. double SalariedEmployee::GetSalary() const//获得工资
  24. {
  25. return _WeeklySalary;
  26. }
  27. double SalariedEmployee::earnings() const//工资
  28. {
  29. return GetSalary();
  30. }
  31. void SalariedEmployee::print() const//打印员工信息
  32. {
  33. cout << endl;
  34. cout.fill(' ');
  35. cout << left;
  36. cout << setw(15) << "姓名:" << setw(15) << "工种:" << setw(15)<< "工资:" << endl;
  37. cout << setw(15) << GetName() << setw(15) << GetPro() << setw(15) << earnings() << endl;
  38. cout << endl;
  39. }
复制代码


回复

使用道具 举报