LT hướng đối tượng - Quản lý nhân viên

Giả sử Công ty có hai loại nhân viên: Nhân viên văn phòng và Nhân viên sản xuất. Viết chương trình quản lý và tính lương cho từng nhân viên của công ty:

Mỗi nhân viên cần quản lý các thông tin sau: Họ tên, ngày sinh, lương
Công ty cần tính lương cho nhân viên như sau:
Đối với nhân viên sản xuất:
Lương=lương căn bản + số sản phẩm * 5.000
Đối nhân viên văn phòng:
lương = số ngày làm việc * 100.000


Khai báo Lớp trong file Employee.h

#pragma once

#include<string>

using namespace std;
struct Birth{
    int Day;
    int Month;
    int Year;
};
class Employee
{
private:
    string Name;
    Birth BirthDay;
protected:
    double Salary;
public:
    Employee();
    Employee(const string name,Birth Day);
    Employee(const string name,int day,int month,int year);
    virtual ~Employee();
    double GetSalary(){return Salary;}
    virtual void SetSalary()=0;
    virtual void Print();
    virtual void SetInfo();
};

class Officer: public Employee{
    private:
        int WorksDay;
    public:
        Officer();
        Officer(const string name,Birth Day,int WorksD);
        Officer(const string name,int day,int month,int year,int WorksD);
        virtual void SetSalary();
        double GetWorksDay(){return WorksDay;}
        void SetWorksDay(int worksday){WorksDay=worksday;}
        void SetInfo();
        void Print(){cout<<"Officer: "; Employee::Print();}
};

class Worker: public Employee{
    private:
        double BaseSalary;
        int NumProducts;
    public:
        Worker();
        Worker(const string name,Birth Day,double basesalary,int numproducts);
        Worker(const string name,int day ,int month,int year,double basesalary,int numproducts);
        virtual void SetSalary();
        double GetBaseSalary(){return BaseSalary;}
        int GetNumProducts(){return NumProducts;}
        void SetBaseSalary(double basesalary){BaseSalary=basesalary;}
        void SetNumProducts(int numproducts){NumProducts=numproducts;}
        void SetInfo();
        void Print(){cout<<"Worker: "; Employee::Print();}
};

Cài đặt Lớp trong file Employee.cpp

#include<iostream>
#include<string>
#include<limits>
#include"Employee.h"
using namespace std;

//Employee definition
Employee::Employee(){
    Name="None Name";
    BirthDay={0,0,0};
    Salary=0;
}
Employee::Employee(const string name,Birth Day){
    Name=name;
    BirthDay=Day;
}
Employee::Employee(const string name,int day,int month,int year){
    Name=name;
    BirthDay={day,month,year};
}
Employee::~Employee(){
}
void Employee::Print(){
    cout<<Name<<" --- Day of Birth: "<<BirthDay.Day<<"/"<<BirthDay.Month<<"/"<<BirthDay.Year<<"--- Salary: "<<Salary<<endl;
}
void Employee::SetInfo(){
    cout<<"Input\n";
    cout<<"Name: ";
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    getline(cin,Name);
    cout<<"Birth Day(separate by space): ";
    cin>>BirthDay.Day>>BirthDay.Month>>BirthDay.Year;
}

//Officer definition
Officer::Officer():Employee(){
    WorksDay=0;
}
Officer::Officer(const string name,Birth Day,int WorksD):Employee(name,Day){
    WorksDay=WorksD;
}
Officer::Officer(const string name,int day,int month,int year,int WorksD):Employee(name,day,month,year){
    WorksDay=WorksD;
}
void Officer::SetSalary(){
    Salary=WorksDay*100000;
}
void Officer::SetInfo(){
    Employee::SetInfo();
    cout<<"Days of Work: ";
    int worksday;
    cin>>worksday;
    SetWorksDay(worksday);
    Officer::SetSalary();
}

//Worker definition
Worker::Worker():Employee(){
    BaseSalary=0;
    NumProducts=0;
}
Worker::Worker(const string name,Birth Day,double basesalary,int numproducts):Employee(name,Day){
    BaseSalary=basesalary;
    NumProducts=numproducts;
}
Worker::Worker(const string name,int day ,int month,int year,double basesalary,int numproducts):Employee(name,day,month,year){
    BaseSalary=basesalary;
    NumProducts=numproducts;
}
void Worker::SetSalary(){
    Salary=BaseSalary+NumProducts*5000;
}
void Worker::SetInfo(){
    Employee::SetInfo();
    cout<<"Base Salary: ";
    double basesalary;
    cin>>basesalary;
    SetBaseSalary(basesalary);
    cout<<"Numbers of product: ";
    int numproducts;
    cin>>numproducts;
    SetNumProducts(numproducts);
    Worker::SetSalary();
}

Code chương trình:

#include<iostream>
#include<vector>
#include"Employee.h"
using namespace std;
void Input(vector<Employee*> &List){
    int CatchKind;
    do{
    cout<<"Choose kind of employee: \n";
    cout<<"\t Press 1 for input Officer.\n";
    cout<<"\t Press 2 for input Worker.\n";
    cout<<"\t Press 3 for back to Manage Employee menu.\n";
    cout<<"Press:";
    while(cin>>CatchKind&&CatchKind!=1&&CatchKind!=2&&CatchKind!=3){
        cout<<"\nJust press 1,2 or 3!\nAgain: ";
    }
    switch(CatchKind)
    {
    case 1:
        List.push_back(new Officer());
        List[List.size()-1]->SetInfo();
        break;
    case 2:
        List.push_back(new Worker());
        List[List.size()-1]->SetInfo();
        break;
    default:
        break;
    }
    }
    while(CatchKind!=3);
}
void PrintList(vector<Employee*> & List){
    for(int i=0;i<List.size();i++){
        List[i]->Print();
    }
}
int main(){
    vector<Employee *> ListEmployee;
    ListEmployee.resize(0);
    int CatchKey;
    do{
    cout<<"Manage Employee: \n";
    cout<<"\t Press 1 for Input employee information.\n";
    cout<<"\t Press 2 for Print all list of employees.\n";
    cout<<"\t Press 3 for exit.\n";
    cout<<"Press:";
    while(cin>>CatchKey&&CatchKey!=1&&CatchKey!=2&&CatchKey!=3){
        cout<<"\nJust press 1,2 or 3!\nAgain: ";
    }
    switch (CatchKey)
    {
    case 1:
        Input(ListEmployee);
        break;
    
    case 2:
        PrintList(ListEmployee);
        break;
    default:
        break;
    }
    }
    while(CatchKey!=3);
    cout<<"Thanks!";
    return 0;
}