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

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

2.Code

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include<cstdlib>
#define TECH const string name, const int age, const  string sex, const string dept, const double salary, const int rank
//using SA = Sale[];
//typedef Technology; *TE;
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(const string name, const int age,const string sex);
    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;
    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 lp.GetAge() == rp.GetAge()&&
        lp.GetName() == rp.GetName()&&
        lp.GetSex() == rp.GetSex();
}
inline bool
operator!=(const Person& lp, const Person& rp) {
    return !(lp == rp);   //!= defined in terms of operator
}
/*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(const string name,const int age,const  string sex) : 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‘ << 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(const string name, const int age,const  string sex, const string dept, const double salary);
    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;
    void ShowMe() const;
};
Employee& Employee:: operator=(const Person&rp) {
    SetAge(rp.GetAge());
    SetName(rp.GetName());
    SetSex(rp.GetSex());
    return *this;
}
Employee& Employee:: operator=(const Employee&rp) {
    SetAge(rp.GetAge());
    SetName(rp.GetName());
    SetSex(rp.GetSex());
    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 lp.GetDept() == rp.GetDept() &&
        lp.GetName() == rp.GetName() &&
        lp.GetSex() == rp.GetSex() &&
        lp.GetAge() == rp.GetAge() &&
        lp.GetSalary() == rp.GetSalary();
}
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(const string name, const int age, const  string sex, const string dept, const double salary)
    : 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
{
    Person::ShowMe();
    cout << "Dept" << ‘\t‘ << "Salary" << endl;
    cout << strDept << ‘\t‘ << douSalary << 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();
    Technology(const string name, const int age, const  string sex, const string dept, const double salary, const int rank);
    Technology(const Technology &e);
    ~Technology(){
        cout << "Now destroying the instance of Technology" << endl;
    }
    void SetRank(const int rank);
    int GetRank()const;
    void ShowMe() const;
};
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 lp.GetDept() == rp.GetDept() &&
        lp.GetName() == rp.GetName() &&
        lp.GetSex() == rp.GetSex() &&
        lp.GetAge() == rp.GetAge() &&
        lp.GetSalary() == rp.GetSalary() &&
        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(const string name, const int age, const  string sex, const string dept, const double salary, const int rank)
    : Person(name, age, sex), Employee(name, age, sex, dept, salary),IntRank(rank)
{

}
Technology::Technology(const Technology &e) : Person(e.GetName(), e.GetAge(), e.GetSex()), Employee(e.GetName(), e.GetAge(),e.GetSex(), e.GetDept(), e.GetSalary()),IntRank(e.IntRank) {

}
void Technology::SetRank(const int rank) {
    IntRank = rank;
}
int Technology::GetRank()const {
    return IntRank;
}
void Technology::ShowMe()const {
    Employee::ShowMe();
    cout << "Rank" << ‘\t‘ << endl;
    cout << IntRank << endl;
}
class Sale :virtual public Employee{
protected:
    int IntOver;
public:
    Sale();
    Sale(const string name, const int age, const  string sex, const string dept, const double salary,const int over);
    Sale(const Sale &e);
    ~Sale() {
        cout << "Now destroying the instance of Sale" << endl;
    }
    void SetOver(const int overturn);
    int GetOver()const;
    void ShowMe() const;
};
Sale::Sale():IntOver(0) {

}
Sale::Sale(const string name, const int age, const  string sex, const string dept, const double salary, const int over)
    : Person(name, age, sex), Employee(name, age, sex, dept, salary), IntOver(over){

}
Sale::Sale(const Sale &e) : Person(e.GetName(), e.GetAge(), e.GetSex()), Employee(e.GetName(), e.GetAge(), e.GetSex(), e.GetDept(), e.GetSalary()) ,IntOver(e.IntOver){

}
void Sale :: SetOver(const int overturn) {
    IntOver = overturn;
}
int Sale::GetOver()const {
    return IntOver;
}
void Sale::ShowMe()const {
    Employee::ShowMe();
    cout << "Over" << ‘\t‘ << endl;
    cout << IntOver << endl;
}
class Manager :virtual public Employee {
protected:
    int SumEmployee;
    //SA SaleEmployeen;
    //TE ITEmployee;
public:
    Manager();
    Manager(const string name, const int age, const  string sex, const string dept, const double salary, const int sum);//,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;
    void ShowMe()const;
};
Manager::Manager() :SumEmployee(0)/* SaleEmployeen(), ITEmployee()*/{

}
Manager::Manager(const string name, const int age, const  string sex, const string dept, const double salary, const int sum)//, const SA SaleMan, const TE ITMan)
    : Person(name, age, sex), Employee(name, age, sex, dept, salary),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):Person(e.GetName(), e.GetAge(), e.GetSex()), Employee(e.GetName(), e.GetAge(), e.GetSex(), e.GetDept(), e.GetSalary()) {
    /*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 {
    Person::ShowMe();
    cout << "The Sum of Employee" << ‘\t‘ << endl;
    cout << SumEmployee << endl;
}
class SaleManager :virtual public Sale, virtual public Manager {
protected:
    string strRight;
public:
    SaleManager();
    SaleManager(const string name, const int age, const  string sex, const string dept, const double salary, const int sum, /*const SA SaleMan, const TE ITMan,*/ const int over, const int right);
    ~SaleManager() {
        cout << "Now destroying the instance of SaleManager" << endl;
    }
    void SetRight(const string right);
    string GetRight()const;
    void ShowMe()const;
};
SaleManager::SaleManager() :strRight("0"){

}
SaleManager::SaleManager(const string name, const int age, const  string sex, const string dept, const double salary, const int sum,/* const SA SaleMan, const TE ITMan,*/ const int over, const int right)
    : Person(name, age, sex), Employee(name, age, sex, dept, salary), 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 {
    Employee::ShowMe();
    cout << "The Manager:" << endl;
    cout << "Right:" << endl;
}
int main()
{

    Person p1[10];
    cout << "Please enter ten 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 (int i = 0; i < 3; i++)
        cin >> p1[i];
    for (int i = 0; i < 3; i++)
        cout << p1[i];
    cout << endl;
    Employee emp1[10];
    cout << "Now enter the salary and department for each employee!" << endl
        << "Like:123456 Sale" << endl;
    for (int i = 0; i < 3; i++)
    {
        emp1[i] = p1[i];
        cin >> emp1[i];
    }
    for (int i = 0; i < 3; i++)
        cout << emp1[i] << endl;
    for (int i = 0; i < 3; i++)
        emp1[i].ShowMe();

    return 0;
}
  • 3.测试截图

  • 4.先Mark一下,有时间再改

原文地址:https://www.cnblogs.com/FlyerBird/p/9038616.html

时间: 2024-10-07 18:28:41

综合运用类、继承、多态,完成一个公司人员管理类层次结构(未完待续)的相关文章

练武场之“封装、继承”攻略(未完待续)

如题: 使用继承编写Person.Student类 编写Person类: 属性有:姓名.年龄.出生日期 方法有:showInfo(); 编写Student类: 属性有:姓名.年龄.出生日期.学校 方法有:showInfo(); study(); 使用继承优化Student类. Ready Go! 第一步 Person类 1 package a; 2 //父类 3 public class Person { 4 // 为了让成员变量隐藏(封装),便于对值制定规则:否则变量会被直接赋值. 5 pri

设置一个DIV的文字超出隐藏,并用省略号表示未完待续

<div style="width:50px;height:18px;white-space: nowrap;overflow:hidden;text-overflow:ellipsis;">设置一个DIV的文字超出隐藏,并用省略号表示未完待续 设置一个DIV的文字超出隐藏,并用省略号表示未完待续 设置一个DIV的文字超出隐藏,并用省略号表示未完待续</div>

接口、继承与多态 总结(未完待续)

面向对象三个基本特征是封装 继承 多态 能自己复述出令自己明白的定义即可,定义有一千种定义,不必拘泥 封装:将客观事物抽象成类,对外部隐藏数据和操作数据的细节,只提供外部接口访问对象.(藏的是数据和对数据的操作) 好处:1.符合面向对象设计中的单一性原则,当我们需要修改一个类的时候,只需要改变它的内部代码即可,对外提供的方法不需要改变.    2.代码重用 继承:子类(个性)继承父类(共性)的全部属性和方法,并加入子类(个性)特有的属性和方法 (我喜欢个性和共性的说法)./实现现有类的全部功能,

HDU 5326 公司人员管理树问题(多校)-简单dp

题意:给出一个公司的人员管理树,求直接和间接下属共k人的人有多少. 分析:刚开始一看是树就吓到了,不敢做,后来咬着牙一想,不难啊,G哥说是dp,我用的我也不知道的方法,过了.我的方法就是用vis[i][j]记录i与j的直接隶属关系,然后通过vis[i][j]和vis[j][k]又能找到vis[i][k]的间接隶属关系,a[i]+=a[j],a[i]+=a[k]即可.相信自己. 这是G哥代码: 递归大法要学会用起来 #include<cstdio> #include<cstring>

c++ 植物类 继承多态 菱形继承

#pragma once//头文件 #include <iostream> #include<string> using namespace std; // // 1.实现以下几个类的成员函数 // 2.实现一个虚函数的覆盖及调用 // 3.处理菱形继承问题. // // 植物 class Botany { public: Botany(const string&  name); virtual ~Botany(); virtual void Display(); Bota

String类的深入理解(未完待续)

String不是基本数据类型,String和8种包装类型是不可变类.String和8种基本数据类型采用值传递. 0.不可变类的设计原则 public final class String implements java.io.Serializable, Comparable<String>, CharSequence { /** The value is used for character storage. */ private final char value[];//数组是引用传递 /*

java中的集合操作类(未完待续)

申明: 实习生的肤浅理解,如发现有错误之处,还望大牛们多多指点 废话 其实我写java的后台操作,我每次都会遇到一条语句:List<XXXXX> list = new ArrayList<XXXXX>(); 但是我仅仅只是了解,list这个类是一个可变长用来存储的对象实例的类,我甚至觉得这个List对象可以理解成数组,但是却又与java中咱们正常理解的数组很多的不同,比如说,他的长度可以随着需要自动增长,比如说,实例化一个List类就和咱们声明数组的时候是不一样的! 今天的实习生活

面向对象之元类介绍(未完待续)

标签(空格分隔): 元类介绍 元类介绍: 在正式介绍元类之前,大家储备一下知识:exec; exec命令使用 这个命令有三个参数: 1.字符串形式的命令,(把字符串提取出来) 2.全局作用域:(字典形式,如果不指定,默认使用globals()) 3.局部作用域,(字典形式,如果不指定默认locals) g={'x':1, 'y':2} l={} exec(""" global x,m x=10 m=100 z=3 """,g,l) print(g

一个开源的分布式搜索引擎---Elasticsearch(未完待续)

今天给大家介绍一个开源的分布式搜索引擎Elasticsearch. 一.ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎, 基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apache 许可条款下的开放源码发布,是第二最流行的企业搜索引擎.设计用于云计算中,能够达到实时搜索, 稳定,可靠,快速,安装使用方便. 我们建立一个网站或应用程序,并要添加搜索功能,令我们受打击的是:搜索工作是很难的.我们希望我们的