十九、多态例子

1动物园

---------------------------------animal.h-----------------------------
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal
{
public:
   Animal();
   virtual ~Animal();//一定要记得
   virtual void voice() = 0;
};

//此函数时根据抽象类来封装的,这种函数就叫 架构函数
void AnimalVoice(Animal *ap);

#endif
//
ANIMAL_H

---------------------------------animal.cpp-----------------------------
//animal.cpp
#include 
#include "animal.h"
using namespace std;
Animal::Animal()
{
 cout<<"Animal::Animal()"<voice();
   cout<<"动物乱叫结束了"<
using namespace std;
Dog::Dog()
{
cout<<"Dog::Dog()"<
using namespace std;
Cat::Cat()
{
cout<<"Cat::Cat()"<voice();
  AnimalVoice(pa);
  delete pa;
  cout<<"-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐"<voice();
  AnimalVoice(pa);
  delete pa;
  return 0;
}

2电脑组装的案例

1.png
#include 
using namespace std;

//---------------------抽象层------------------------------------
//抽象的CPU
class CPU
{
public:
    //抽象的计算方法
    virtual void caculate() = 0;
}


//抽象的显卡
class Card
{
public:
    virtual void display() = 0;
}

//抽象的内存
class Memory
{
public:
    virtual void storage() = 0;
}


class Computer
{
public:
    Computer(CPU *cpu,Card *card,Memory *mem){cpu = new IntelCPU;
        this->cpu = cpu;
        this->card = card;
        this->mem = mem;
    }

    //给电脑提供一个工作的方法
    void work(){
        cpu->caculate();//发生多态
        card->display();//发生多态
        mem->storage(); //发生多态
    }

    ~Computer(){
        if (cpu != NULL)
        {
            delete cpu;
            cpu = NULL
        }
        if (card != NULL)
        {
            delete card;
            card = NULL;
        }
        if (mem != NULL)
        {
            delete mem;
            mem = NULL;
        }
    }

private:
    CPU *cpu;
    Card *card;
    Memory *mem;

}


//---------------------抽象层end------------------------------------

//------------------------实现层------------------------------------
//intel的CPU
class IntelCPU:public CPU
{
public:
    virtual void caculate(){
        cout<<"Intel的CPU开始计算了"<caculate();
    intelCard->display();
    intelMemory->storage();
#endif

    Computer *c1 = new Computer(intelCPU,intelCard,intelMemory);
    c1->work();
    delete c1;

    //组装第二台电脑
    Card *nvdiaCard = new NvdiaCard;
    Memory *kingstonMem = new KingstonMem;
    Computer *c2 = new Computer(intelCPU,nvdiaCard,kingstonMem);
    c2->work();
    delete c2;


    delete intelCPU;
    delete intelCard;
    delete intelMemory;
    delete nvdiaCard;
    delete kingstonMem;


    Computer *c3 = new Computer(new IntelCPU,new IntelCard,new IntelMemory);
    //这样写无法在main里面 delete (new IntelCPU...)
    //只能通过Computer的析构函数delete掉
    //很方便!
    delete c3;
    return 0;
}

3.企业员工信息管理系统

一个小型公司的人员信息管理系统
某小型公司,主要有四类人员:经理、技术人员、销售经理和推销员。现在,需要存储这些人员的姓名、编号、级别、当月薪水.计算月薪总额并显示全部信息。

人员编号基数为 1000,每输入一个人员信息编号顺序加 1。

程序要有对所有人员提升级别的功能。本例中为简单起见,所有人员的初始级别均为 1 级。然后进行升级,经理升为 4 级,技术人员和销售经理升为 3 级,推销员仍为1级。

月薪计算办法是: 经理拿固定月薪 8000 元;技术人员按每小时 100 元领取月薪; 推销员的月薪按该推销员当月销售额的 4%提成;销售经理既拿固定月薪也领取销售提成,固定月薪为5000 元,销售提成为所管辖部门当月销售总额的5%。

2.jpg
//-------------------------------Employee.h------------------------------------------
#ifndef __EMPLOYEE_H__
#define __EMPLOYEE_H__


#include 
#include 


class Employee
{
public:
    Employee();
    ~Employee();

    //显示员工的信息的接口
    void displayStatus();//发现都一样显示,索性写在Employee文件里

    //得到员工的薪水的方法
    virtual void getPay() = 0;

    //员工的升级方法
    virtual void upLevel(int addLevel) = 0;

    //提供一个显示的初始化接口!
    virtual void init() = 0;
    //这里是为了解决多继承自动调用父类构造函数的缺陷!
    //同时我们也不应该把业务写在构造函数里面,构造函数最好只用于初始化一些值,或者什么都别写!

protected:
    string name;  //员工的姓名
    int id;       //员工的编号
    double salary;//员工的薪水
    int level;    //员工的等级

    static int startnum;//静态的编号计数

};


#endif

//-------------------------------Employee.cpp------------------------------------------

int Employee::startnum = 1000;

//所有继承于抽象类的子类在构造的时候,都会调用父类的构造函数
Employee::Employee()
{
    level = 1;
    salary = 0;
    id = startnum++;
}

Employee::~Employee()
{

}

void Employee::displayStatus()
{
   cout<<"姓名:"<name<level<id<salary<>this->name;
   perHourMoney = 100;//技术人员每小时赚100
}

void Technican::getPay()
{
   cout<<"请输入员工一个月工作了多少小时"<>workHour;

   //计算工资
   this->salary = perHourMoney * workHour;
}

    
void Technican::upLevel(int addLevel)
{
   this->level += addLevel;
}

//-------------------------------manager.h-----------------------------------------
#ifndef __MANAGER_H__
#define __MANAGER_H__
#include "Employee.h"
class Manager:virtual public Employee//因SalesManager修改成virtual
{
public:
    Manager();
    ~Manager();

    //得到员工的薪水的方法
    virtual void getPay();

    //员工的升级方法
    virtual void upLevel(int addLevel);

    virtual void init();
protected://因SalesManager修改成protected
    double fixSalary;//固定薪水

};
#endif
//-------------------------------manager.cpp------------------------------------------
#include "Manager.h"

Manager::Manager()
{
    
}


Manager::~Manager()
{

}

void Manager::init()
{
    cout<<"请输入经理的姓名:"<>name;
    //经理的固定薪水是8000
    fixSalary = 8000;
}


    //得到员工的薪水的方法
void Manager::getPay()
{
    //计算经理的薪资方法
    this->salary = fixSalary;

}

    //员工的升级方法
void Manager::upLevel(int addLevel)
{
    this->level += addLevel;
}

//-------------------------------SaleMan.h------------------------------------------
#ifndef __SALEMAN_H__
#define __SALEMAN_H__
#include "Employee.h"
class SaleMan:virtual public Employee//因SalesManager修改成virtual
{
public:
    SaleMan();
    ~SaleMan();

    virtual void getPay();

    virtual void upLevel(int addLevel);

    virtual void init();

protected://因SalesManager修改成protected
    //当月的销售总额
    int saleAmount;
    //提成比率
    double saleRate;


}



#endif
//-------------------------------SaleMan.cpp------------------------------------------
SaleMan::SaleMan
{
    
}


SaleMan::~SaleMan
{

}

void SaleMan::init()
{
    cout<<"请输入销售人员的姓名:"<>name;

    this->saleRate = 0.04;//按4%提成
}

void SaleMan::getPay()
{
   cout<<"请输入该员工本月的销售额:"<>saleAmount;

   //计算薪水
   this->salary = this->saleAmount * this->saleRate;
}


void SaleMan::upLevel(int addLevel)
{
   this->level += addLevel;
}

//-------------------------------SalesManager.h------------------------------------------
#ifndef __SALESMANAGER_H__
#define __SALESMANAGER_H__
#include "Manager.h"
#include "SaleMan.h"

class SalesManager: public Manager, public SaleMan//菱形继承不是在这里加virtual,而是在多继承的父类那里!
{
public:
    SalesManager();
    ~SalesManager();
    virtual void getPay();
    virtual void upLevel(int addLevel);
    virtual void init();
}

#endif

//-------------------------------SalesManager.cpp------------------------------------------
SalesManager::SalesManager()
{
    //1.这里调用了SaleMan和Manager的构造方法!系统崩了,怎么解决?

}
SalesManager::~SalesManager()
{

}

void SalesManager::init()
{
    cout<<"请输入销售经理姓名:"<>name;
    fixSalary = 5000;
    saleRate = 0.05;
}

void SalesManager::getPay()
{
     cout<<"请输入销售经理当月的销售额:"<>this->saleAmount;

     //计算薪水
     this->salary = this->fixSalary + this->saleAmount * this->saleRate;
}

void SalesManager::upLevel(int addLevel)
{
    this->level += addLevel;
}





//-------------------------------main.cpp------------------------------------------


#include 
#include "Employee.h"
#include "Technican.h"
#include "Manager.h"
#include "SaleMan.h"
#include "SalesManager.h"

using namesapce std;

int main(int argc, char const *argv[])
{
    #if 0
    Employee *techp = new Technican;
    techp->init();
    techp->upLevel(1);
    techp->getPay();
    techp->displayStatus();
    delete techp;


    techp = new Manager;
    techp->init();
    techp->upLevel(1);
    techp->getPay();
    techp->displayStatus();
    delete techp;

    techp = new SaleMan;
    techp->init();
    techp->upLevel(1);
    techp->getPay();
    techp->displayStatus();
    delete techp;


    techp = new SalesManager;
    techp->init();
    techp->upLevel(1);
    techp->getPay();
    techp->displayStatus();
    delete techp;//2.多继承这里的delete报错了!怎么解决
                 //Employee的析构方法没有加virtual,导致只析构了Employee

   #endif

    Employee *person_array[] = {new Technican,new Manager,new SaleMan, new SalesManager};

    for (int i = 0; i < sizeof(person_array)/sizeof(person_array[0]); ++i)
    {//为什么sizeof(person_array)/sizeof(person_array[0])==4?
     //因为person_array[]存储的是指针变量!!!!!!所以可以整除,和类的空间大小无关!
         person_array[i]->init();
         person_array[i]->upLevel(1);
         person_array[i]->getPay();
         person_array[i]->displayStatus();
    }

    for (int i = 0; i < sizeof(person_array)/sizeof(person_array[0]); ++i)
    {
          delete person_array[i];
    }
    return 0;
}



你可能感兴趣的:(十九、多态例子)