前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >综合运用类、继承、多态,完成一个公司人员管理类层次结构

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

作者头像
Enterprise_
发布2019-02-21 17:23:04
4810
发布2019-02-21 17:23:04
举报
文章被收录于专栏:小L的魔法馆小L的魔法馆
  • 1.Target
代码语言:javascript
复制
/*综合运用类、继承、多态等技术,完成一个公司人员管理类层次结构,用来描述人员信息等,
重载各种运算符,完成数据库内容的赋值、添加、工资增长等。*/
/*修改18-5-16*/

2.Code

代码语言:javascript
复制
#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.测试数据
代码语言:javascript
复制
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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年05月14日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档