综合运用类、继承、多态,完成一个公司人员管理类层次结构

  • 1.Target
/*综合运用类、继承、多态等技术,完成一个公司人员管理类层次结构,用来描述人员信息等,
重载各种运算符,完成数据库内容的赋值、添加、工资增长等。*/

2.Code

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cstdlib>
#define PE const string name, const int age,const string sex
#define EM const string dept, const double salary
#define TECH const int rank
#define SA const int over
#define MA const int sum
#define SM  const string right
#define PEEM Person(name, age, sex), Employee(name, age, sex, dept, salary)
#define SUPEEM Person(e.GetName(), e.GetAge(), e.GetSex()), Employee(e.GetName(), e.GetAge(),e.GetSex(), e.GetDept(), e.GetSalary())
#define SUB SetAge(rp.GetAge()); SetName(rp.GetName()); SetSex(rp.GetSex());
#define COMPE lp.GetAge() == rp.GetAge()&&lp.GetName() == rp.GetName()&&lp.GetSex() == rp.GetSex()
#define COMEM lp.GetSalary() == rp.GetSalary()&&lp.GetDept() == rp.GetDept()
#define FOR for(int i=0;i<n;i++)
using namespace std;
class Person
{
private:
    string strName; 
    int intAge;
    string strSex;
public:
    friend std::istream& operator >> (std::istream&, Person&);
    friend std::ostream& operator<<(std::ostream&, const Person&);
    friend bool operator==(const Person&, const Person&);
    friend bool operator!=(const Person&, const Person&);
    /*
    friend Person operator+(const Person& lp, const Person& rp);
    Person& operator+=(const Person&);
    */
    Person();
    Person(PE);
    Person(const Person &p); 
    ~Person() {
        cout << "Now destroying the instance of Person" << endl;
    }
    void SetName(const string name);
    void SetAge(const int age);
    void SetSex(const string sex);
    string GetName() const;
    int GetAge() const;
    string GetSex() const;
    virtual void ShowMe() const;
};
std::istream& 
operator >> (std::istream& in, Person& s) {
    in >>  s.strName>>s.intAge>> s.strSex;
    //check that he inputs succeeded
    if (!in)
        s = Person();   //input failed:reset object to default state
    return in;
}
std::ostream& 
operator<<(std::ostream& out, const Person& s) {
    out << s.GetName() << " " << s.GetAge() << " "
        << s.GetSex() << " " << endl;
    return out;
}
inline bool
operator==(const Person& lp, const Person& rp) {
    //must be made a friend of Person
    return COMPE;
}
inline bool
operator!=(const Person& lp, const Person& rp) {
    return !(lp == rp);   //!= defined in terms of operator
}
/*Person& Person::operator+=(const Person&) {
}

Person operator+(const Person& lp, const Person& rp) {
    Person res(lp);// copy (|lp|) into a local object that we'll return
    res += rp;      // add in the contents of (|rp|)
    return res;      // return (|res|) by value
}
*/
Person::Person() 
{
    strName = "XXX";
    intAge = 0;
    strSex = "female";
}
Person::Person(PE) : strName(name),intAge(age), strSex(sex){

}
Person::Person(const Person &p) : strName(p.strName), intAge(p.intAge), strSex(p.strSex){

}
void Person::SetName(const string name) {
    strName = name;
}
void Person::SetAge(const int age){
    intAge = age;
}
void Person::SetSex(const string sex){
    strSex = sex;
}
string Person::GetName() const {
    return strName;
}
int Person::GetAge() const{
    return intAge;
}
string Person::GetSex() const{
    return strSex;
}
void Person::ShowMe() const{
    cout << "Name" << '\t' << "Age" << '\t' <<"Gender"<< endl;
    cout << GetName() << '\t' << GetAge() << '\t' << GetSex() << '\t' << endl;
}
class Employee : virtual public Person      
{
protected:
    string strDept;
    double douSalary;
public:
    friend std::istream& operator >> (std::istream&, Employee&);
    friend std::ostream& operator<<(std::ostream&, const Employee&);
    friend bool operator==(const Employee&, const Employee&);
    friend bool operator!=(const Employee&, const Employee&);
    Employee& operator+=(const Employee&);
    Employee& operator=(const Person&);
    Employee& operator=(const Employee&);
    friend Employee operator+(const Employee&, const Employee&);
    Employee();
    Employee(PE,EM);
    Employee(const Employee &e);
    ~Employee()
    {
        cout << "Now destroying the instance of Employee" << endl;
    }
    void SetDept(const string dept);
    void SetSalary(double  salary);
    string GetDept() const;
    double GetSalary() const;
    virtual void ShowMe() const;                
};
Employee& Employee:: operator=(const Person&rp) {
    SUB
    return *this;
}
Employee& Employee:: operator=(const Employee&rp) {
    SUB SetDept(rp.GetDept()); SetSalary(rp.GetSalary());
    return *this;
}
Employee& Employee:: operator+=(const Employee&rp) {
    douSalary += rp.GetSalary();
    return *this;
}
Employee operator+(const Employee&lp, const Employee&rp) {
    Employee res(lp); 
    res += rp;
    return res;

}
std::istream&
operator >> (std::istream& in, Employee& s) {
    in >> s.douSalary >> s.strDept;
    //check that he inputs succeeded
    if (!in)
        s = Employee(); //input failed:reset object to default state
    return in;
}
std::ostream&
operator<<(std::ostream& out, const Employee& s) {
    out << s.GetDept() << " " << s.GetSalary() << " "<<endl;
    return out;
}
inline bool
operator==(const Employee& lp, const Employee& rp) {
    //must be made a friend of Employee
    return COMPE&&COMEM;
}
inline bool
operator!=(const Employee& lp, const Employee& rp) {
    return !(lp == rp);   //!= defined in terms of operator
}
Employee::Employee() : douSalary(0.0), strDept("xxxx")
{

}
Employee::Employee(PE,EM)
    : Person(name, age, sex), douSalary(salary),strDept(dept)
{

}
Employee::Employee(const Employee &e) : Person(e.GetName(), e.GetAge(), e.GetSex()),
douSalary(e.douSalary)
{
    strDept = e.strDept;
}
void Employee::SetDept(const string dept)
{
    strDept = dept;
}
void Employee::SetSalary(double salary)
{
    douSalary = salary;
}
string Employee::GetDept() const
{
    return strDept;
}
double Employee::GetSalary() const
{
    return douSalary;
}

void Employee::ShowMe() const
{
    cout << "Name" << '\t' << "Age" << '\t' << "Gender" << '\t' << "Dept" << '\t' << "Salary" << endl;
    cout << GetName() << '\t' << GetAge() << '\t' << GetSex() << '\t' << GetDept() << '\t' << GetSalary() << endl;
}
class Technology :virtual public Employee{
protected:
    int IntRank;
public:
    friend std::istream& operator >> (std::istream&, Technology&);
    friend std::ostream& operator<<(std::ostream&, const Technology&);
    friend bool operator==(const Technology&, const Technology&);
    friend bool operator!=(const Technology&, const Technology&);
    Technology& operator=(const Employee&);
    Technology& operator=(const Technology&);
    Technology();
    Technology(PE,EM,TECH);
    Technology(const Technology &e);
    ~Technology(){
        cout << "Now destroying the instance of Technology" << endl;
    }
    void SetRank(const int rank);
    int GetRank()const;
    virtual void ShowMe() const;
};
Technology& Technology:: operator=(const Employee&rp) {
    SUB
    SetDept("IT");
    SetSalary(rp.GetSalary());
    return *this;
}
Technology& Technology:: operator=(const Technology&rp) {
    SUB
    SetDept("IT");
    SetSalary(rp.GetSalary());
    SetRank(rp.GetRank());
    return *this;
}
std::istream&
operator >> (std::istream& in, Technology& s) {
    in >> s.IntRank;
    //check that he inputs succeeded
    if (!in)
        s = Technology();   //input failed:reset object to default state
    return in;
}
std::ostream&
operator<<(std::ostream& out, const Technology& s) {
    out << s.GetRank() << " " << endl;
    return out;
}
inline bool
operator==(const Technology& lp, const Technology& rp) {
    //must be made a friend of Technology
    return COMPE&&COMEM&&lp.GetRank() == rp.GetRank();
}
inline bool
operator!=(const Technology& lp, const Technology& rp) {
    return !(lp == rp);   //!= defined in terms of operator
}
Technology::Technology():IntRank(0) {

}
Technology::Technology(PE, EM,TECH)
    :PEEM,IntRank(rank)
{

}
Technology::Technology(const Technology &e) : SUPEEM,IntRank(e.IntRank) {

}
void Technology::SetRank(TECH) {
    IntRank = rank;
}
int Technology::GetRank()const {
    return IntRank;
}
void Technology::ShowMe()const {
    cout << "Name" << '\t' << "Age" << '\t' << "Gender" << '\t' << "Dept" << '\t' << "Salary"<<'\t'<< "Rank" << '\t' << endl;
    cout << GetName() << '\t' << GetAge() << '\t' << GetSex() << '\t' << GetDept() << '\t' << GetSalary() << '\t' << GetRank() << endl;
}
class Sale :virtual public Employee{
protected:
    int IntOver;
public:
    Sale& operator=(const Employee&);
    Sale& operator=(const Sale&);
    friend std::istream& operator >> (std::istream&, Sale&);
    friend std::ostream& operator<<(std::ostream&, const Sale&);
    friend bool operator==(const Sale&, const Sale&);
    friend bool operator!=(const Sale&, const Sale&);
    Sale();
    Sale(PE,EM,SA);
    Sale(const Sale &e);
    ~Sale() {
        cout << "Now destroying the instance of Sale" << endl;
    }
    void SetOver(const int over);
    int GetOver()const;
    virtual void ShowMe() const;
};
Sale& Sale:: operator=(const Employee&rp) {
    SUB
    SetDept("Sale");
    SetSalary(rp.GetSalary());
    return *this;
}
Sale& Sale:: operator=(const Sale&rp) {
    SUB
    SetDept("Sale");
    SetSalary(rp.GetSalary());
    SetOver(rp.GetOver());
    return *this;
}
std::istream&
operator >> (std::istream& in, Sale& s) {
    in >> s.IntOver;
    //check that he inputs succeeded
    if (!in)
        s = Sale(); //input failed:reset object to default state
    return in;
}
std::ostream&
operator<<(std::ostream& out, const Sale& s) {
    out << s.GetOver() << '\t' << endl;
    return out;
}
inline bool
operator==(const Sale& lp, const Sale& rp) {
    //must be made a friend of Sale
    return COMEM&&COMPE&&lp.GetOver() == rp.GetOver();
}
inline bool
operator!=(const Sale& lp, const Sale& rp) {
    return !(lp == rp);   //!= defined in terms of operator
}
Sale::Sale():IntOver(0) {

}
Sale::Sale(PE,EM, SA)
    : PEEM, IntOver(over){

}
Sale::Sale(const Sale &e) : SUPEEM,IntOver(e.IntOver){

}
void Sale :: SetOver(SA) {
    IntOver = over;
}
int Sale::GetOver()const {
    return IntOver;
}
void Sale::ShowMe()const {
    cout << "Name" << '\t' << "Age" << '\t' << "Gender" << '\t' << "Dept" << '\t' << "Salary" << '\t'<< "Over" << '\t' << endl;
    cout << GetName() << '\t' << GetAge() << '\t' << GetSex() << '\t' << GetDept() << '\t' << GetSalary() << '\t'<<GetOver() << endl;
}
class Manager :virtual public Employee {
protected:
    int SumEmployee;
    //SA SaleEmployeen;
    //TE ITEmployee;
public:
    Manager& operator=(const Employee&);
    Manager& operator=(const Manager&);
    friend std::istream& operator >> (std::istream&, Manager&);
    friend std::ostream& operator<<(std::ostream&, const Manager&);
    friend bool operator==(const Manager&, const Manager&);
    friend bool operator!=(const Manager&, const Manager&);
    Manager();
    Manager(PE, EM,MA);//,const SA SaleMan,const TE ITMan);
    Manager(const Manager &e);
    ~Manager(){
            cout << "Now destroying the instance of Manager" << endl;
    }
    void SetSum(const int sum);
    int GetSum()const;
    virtual void ShowMe()const;
};
Manager& Manager:: operator=(const Employee&rp) {
    SUB
    SetDept("Manager");
    SetSalary(rp.GetSalary());
    return *this;
}
Manager& Manager:: operator=(const Manager&rp) {
    SUB
    SetDept("Manager");
    SetSalary(rp.GetSalary());
    SetSum(rp.GetSum());
    return *this;
}
std::istream&
operator >> (std::istream& in, Manager& s) {
    in >> s.SumEmployee;
    //check that he inputs succeeded
    if (!in)
        s = Manager();  //input failed:reset object to default state
    return in;
}
std::ostream&
operator<<(std::ostream& out, const Manager& s) {
    out << s.GetSum() << '\t' << endl;
    return out;
}
inline bool
operator==(const Manager& lp, const Manager& rp) {
    //must be made a friend of Manager
    return COMPE&&COMEM&&lp.GetSum() == rp.GetSum();
}
inline bool
operator!=(const Manager& lp, const Manager& rp) {
    return !(lp == rp);   //!= defined in terms of operator
}
Manager::Manager() :SumEmployee(0)/* SaleEmployeen(), ITEmployee()*/{

}
Manager::Manager(PE, EM, MA)//, const SA SaleMan, const TE ITMan) 
    :PEEM,SumEmployee(sum)
{
    /*Sale *p=new Sale[sum];
    p = SaleMan;
    while (p++ != NULL) {
        *SaleEmployeen++ = *p;
    }
    Technology *q = new Technology[sum];
    q = ITMan;
    while (q++ != NULL) {
        *ITEmployee++ = *q++;
    }*/
}
Manager::Manager(const Manager &e):SUPEEM{
    /*Sale *p = new Sale;
    p = e.SaleEmployeen;
    while (p++ != NULL) {
        *SaleEmployeen++ = *p;
    }
    Technology *q = new Technology;
    q = e.ITEmployee;
    while (q++ != NULL) { 
        *ITEmployee++ = *q;
    }
    */
}
void Manager::SetSum(const int sum) {
    SumEmployee = sum;
}
int Manager::GetSum()const {
    return SumEmployee;
}
void Manager::ShowMe()const {
    cout << "Name" << '\t' << "Age" << '\t' << "Gender" << '\t' << "Dept" << '\t' << "Salary" << '\t' << "Sum" << '\t' << endl;
    cout << GetName() << '\t' << GetAge() << '\t' << GetSex() << '\t' << GetDept() << '\t' << GetSalary() << '\t' << GetSum() << endl;
}
class SaleManager :virtual public Sale, virtual public Manager {
protected:
    string strRight;
public:
    SaleManager& operator=(const Employee&);
    SaleManager& operator=(const SaleManager&);
    friend std::istream& operator >> (std::istream&, SaleManager&);
    friend std::ostream& operator<<(std::ostream&, const SaleManager&);
    friend bool operator==(const SaleManager&, const SaleManager&);
    friend bool operator!=(const SaleManager&, const SaleManager&);
    SaleManager();
    SaleManager(PE, EM,MA, /*const SA SaleMan, const TE ITMan,*/ SA, SM);
    ~SaleManager() {
        cout << "Now destroying the instance of SaleManager" << endl;
    }
    void SetRight(SM);
    string GetRight()const;
    virtual void ShowMe()const;
};
SaleManager& SaleManager:: operator=(const Employee&rp) {
    SUB
    SetDept("SaleM");
    SetSalary(rp.GetSalary());
    return *this;
}
SaleManager& SaleManager:: operator=(const SaleManager&rp) {
    SUB
    SetDept("SaleM");
    SetSalary(rp.GetSalary());
    SetSum(rp.GetSum());
    SetOver(rp.GetOver());
    SetRight(rp.GetRight());
    return *this;
}
std::istream&
operator >> (std::istream& in, SaleManager& s) {
    in >> s.strRight;
    //check that he inputs succeeded
    if (!in)
        s = SaleManager();  //input failed:reset object to default state
    return in;
}
std::ostream&
operator<<(std::ostream& out, const SaleManager& s) {
    out << s.GetRight() << '\t' << endl;
    return out;
}
inline bool
operator==(const SaleManager& lp, const SaleManager& rp) {
    //must be made a friend of SaleManager
    return COMPE&&COMEM&&
        lp.GetRight() == rp.GetRight() 
        && lp.GetOver() == rp.GetOver() 
        && lp.GetSum() == rp.GetSum();
}
inline bool
operator!=(const SaleManager& lp, const SaleManager& rp) {
    return !(lp == rp);   //!= defined in terms of operator
}
SaleManager::SaleManager() :strRight("0"){

}
SaleManager::SaleManager(PE,EM, MA,/* const SA SaleMan, const TE ITMan,*/SA, SM)
    : PEEM, Sale(name, age, sex, dept, salary, over),Manager( name, age,sex, dept,salary, sum/*, SaleMan, ITMan*/)
{
    strRight = right;
}
void SaleManager::SetRight(const string right) {
    strRight = right;
}
string SaleManager::GetRight()const {
    return strRight;
}
void SaleManager::ShowMe()const {
    cout << "Name" << '\t' << "Age" << '\t' << "Gender" << '\t' << "Dept" << '\t' << "Salary" << '\t' << "Right" << '\t' << endl;
    cout << GetName() << '\t' << GetAge() << '\t' << GetSex() << '\t' << GetDept() << '\t' << GetSalary() << '\t' << GetRight() << endl;
}
int main()
{
    Person p1[5];
    Employee emp1[5];
    Technology te[5];
    Manager man[5];
    Sale sa[5];
    SaleManager sama[5];
    Person *p = p1;
    int n = 3;
    cout << "Please enter "<<n<<" persons in details"
        << endl << "Input format is " << "(Name Age Sex) " << endl
        << "Warnning:Separate each data with a space!!!" << endl
        << "For example:LZH 19 male" << endl;
    FOR  cin >> p1[i];
    cout << endl;
    cout << "Now enter the salary and department for each employee!" << endl
        << "Like:12n456 Sale" << endl;
    FOR {
        emp1[i] = p1[i];
        cin >> emp1[i];
    }
    FOR {
        te[i]= emp1[i];
        sa[i] = emp1[i];
        sama[i] = emp1[i];
        man[i] = emp1[i];
    }
    cout << "Now enter the turnover for each Sale!" << endl
        << "Like:12n456 " << endl;
    FOR cin >> sa[i];
    cout << "Now enter the sum of Employee for each Manager!" << endl
        << "Like:12n " << endl;
    FOR cin >> man[i];
    cout << "Now enter the Rank for each Technology!" << endl
        << "Like:12n " << endl;
    FOR  cin >> te[i];
    cout << "Now enter the Right for each SaleManager!" << endl
        << "Like:NO1 " << endl;
    FOR  cin >> sama[i];
    FOR  {
        cout << p1[i];
        cout << emp1[i];
        cout<< sa[i];
        cout << man[i];
        cout<< te[i];
        cout<< sama[i] << endl;
    }
    FOR {
        p = &emp1[i];
        p->ShowMe();
        p = &sa[i];
        p->ShowMe();
        p = &te[i];
        p->ShowMe();
        p = &sama[i];
        p->ShowMe();
    }
    for (auto s : sa) {
        s.ShowMe();
    }
    return 0;
}

  • 3.测试截图



  • 4.测试数据
    dasd 156 dad
    das 45 dsad
    da 135 das
    4252 das
    24 da
    4 d
    156
    1561
    616
    12 1 2
    1 2 3
    NO1 NO2 NO3
posted @ 2018-05-14 22:54  Chasssser  阅读(318)  评论(0编辑  收藏  举报