首先寻找对象:
运行结果:
.h文件
1 #pragma once 2 #include<string> 3 #include<iostream> 4 using namespace std; 5 //注意面向对象的三大特征:封装,继承与多态 6 7 class Employee //提取经理和员工公有属性和行为进行封装 8 { 9 public: 10 //构造函数 11 Employee(string strName, int nYears) 12 :m_strName(strName), 13 m_nYears(nYears) 14 {}; 15 16 virtual int GetSalary() = 0; //提供一个纯虚函数,多样化 17 18 string GetName() 19 { 20 return m_strName; 21 } 22 23 //员工类的属性,遗传,设置为prote 24 protected: 25 int m_nYears; 26 string m_strName; 27 28 29 }; 30 31 class Manager :public Employee //继承 32 { 33 public: 34 Manager(string strName, int nYears) 35 :Employee(strName, nYears) 36 {}; 37 38 virtual int GetSalary() //多态 39 { 40 return 5000 * m_nYears + 10000; 41 } 42 }; 43 44 class Worker :public Employee 45 { 46 public: 47 Worker(string strName, int nYears) 48 :Employee(strName, nYears) 49 {}; 50 51 virtual int GetSalary() 52 { 53 return 200 * m_nYears + 2000; 54 } 55 }; 56 57 const int MAX_COUNT = 1000; 58 59 class SalarySystem 60 { 61 private: 62 int m_nCount; //当前员工数 63 Employee* m_arrEmployee[MAX_COUNT]; 64 65 public: 66 SalarySystem(void);//构造函数和析构函数 67 ~SalarySystem(void); 68 69 public: 70 void InputEmployee(void); 71 void DisplaySalary(void); 72 double GetAverSalary(void); 73 };
.cpp文件:
1 #include"SalarySystem.h" 2 3 4 SalarySystem::SalarySystem(void) 5 { 6 m_nCount = 0; //构造函数,对类的属性进行初始化,将员工总数初始化为0 7 } 8 9 //析构函数,清理资源,释放内存 10 SalarySystem::~SalarySystem(void) 11 { 12 //循环遍历保存员工对象指针的数组,清理资源 13 for (int i = 0; i < m_nCount; ++i) 14 { 15 Employee*pEmployee = m_arrEmployee[i]; 16 delete pEmployee; 17 m_arrEmployee[i] = NULL; 18 } 19 } 20 21 void SalarySystem::InputEmployee(void) 22 { 23 cout << "请输入员工信息\n" << 24 "格式:员工姓名 入职时间 是否为经理级别\n" << 25 "例如: chenxi 4 0\n" << 26 "输入end表示结束" << endl; 27 28 //局部变量,用于接用户的输入 29 string strName = ""; 30 int nYears = 0; 31 bool bManager = false; 32 int nIndex = 0; 33 34 //开始循环,接收用户输入的用户数据 35 while (nIndex < MAX_COUNT) 36 { 37 //清空输入流 38 cin.clear(); 39 cin >> strName; 40 if ("end" == strName) 41 break; 42 43 cin>> nYears >> bManager; 44 45 46 47 Employee*pEmployee = NULL; 48 if (bManager) 49 { 50 pEmployee = new Manager(strName, nYears); 51 } 52 else 53 { 54 pEmployee = new Worker(strName, nYears); 55 } 56 57 m_arrEmployee[nIndex] = pEmployee; 58 59 ++nIndex; 60 61 } 62 63 m_nCount = nIndex;//保存输入的员工总数 64 } 65 66 void SalarySystem::DisplaySalary(void) 67 { 68 cout << "工资管理系统" << endl; 69 cout << "当前员工总数" << m_nCount << "\n 平均工资是: " << GetAverSalary() << endl; 70 cout << "员工具体信息如下:" << endl; 71 72 for (int i = 0; i < m_nCount; ++i) 73 { 74 Employee*pEmployee = m_arrEmployee[i]; 75 cout << pEmployee->GetName() << "\t" << 76 pEmployee->GetSalary() << endl; 77 } 78 } 79 80 81 double SalarySystem::GetAverSalary() 82 { 83 int nTotal = 0; 84 85 for (int i = 0; i < m_nCount; ++i) 86 { 87 Employee*pEmployee = m_arrEmployee[i]; 88 nTotal += pEmployee->GetSalary(); 89 } 90 91 return (double)nTotal / (m_nCount); 92 93 }
主函数:
1 #include"SalarySystem.h" 2 //#include<cmath> .h头文件已经包含,可以略去 3 4 using namespace std; 5 6 int main() 7 { 8 9 10 SalarySystem nSalarySys; 11 12 nSalarySys.InputEmployee(); 13 nSalarySys.DisplaySalary(); 14 15 16 17 cin.get(); 18 cin.get(); 19 cin.get(); 20 return 0; 21 22 }