c++多态性及对象链表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cfqcfqcfqcfqcfq/article/details/53184970

    新手一枚,编写了一个简单的例子来说明c++多态性和链表对象的使用

   例子描述:

         基类 person(抽象类)

         派生类:teacher 、worker

         纯虚函数:  工资和身份

        对象链表来管理一组对象(保存对象、输出信息、删除对象)


 基类和派生类:

class Person
{
public:
    virtual ~Person(){}//必须提供定义 否则找不到析构函数入口

    virtual void setWages(const int)=0;
    virtual int getWages() const=0;
    virtual void identity() =0;

    void setName(const std::string name){this->name=name;}
    std::string getName()const{return name;}

    Person *next;

private:
    std::string name;
};

class Teacher : public Person
{
public:
    Teacher(){}
    ~Teacher(){}

    void setWages(const int wage) override{teacherWage=wage;}
    int getWages() const override{return teacherWage;}
    void identity(){std::cout<<"I am Teacher!"<<std::endl;}


private:
    int teacherWage;
};

class Worker : public Person
{
public:
    Worker(){}
    ~Worker(){}

    void setWages(const int wage) override{workerWage=wage;}
    int getWages() const override { return workerWage;}
    void identity(){std::cout<<"I am worker!"<<std::endl;}

private:
    int workerWage;
};

链表维护一组对象:

const int TEACHER=0;
const int WORKER=1;
Person* creatList(int n,int type);
void printList(Person *h);
void deleteList(Person *h);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int n,type;
    Person *head=nullptr;
    std::cout<<"person Type:"<<std::endl;
    std::cout<<"[0] Teacher"<<std::endl;
    std::cout<<"[1] Worker"<<std::endl;

    std::cout<<"input person type:"<<std::endl;
    std::cin>>type;
    std::cout<<"input person number:"<<std::endl;
    std::cin>>n;

    head=creatList(n,type);
    printList(head);
    deleteList(head);

    return a.exec();
}

Person* creatList(int n, int type)
{
    Person *h=nullptr;
    Person *p1=nullptr;
    Person *p2=nullptr;
    std::string name;
    int wage;

    int i=0;
    switch (type) {
    case TEACHER:
        p1=new Teacher();
        h=p1;
        while(i<n)
        {
            std::cout<<"Input"<<i+1<<
                       "person Information(name,wage):"<<std::endl;
            std::cin>>name>>wage;
            p1->setName(name);
            p1->setWages(wage);
            if(i+1<n)
            {
                p2=new Teacher();
                p1->next=p2;
                p1=p2;
                p2=nullptr;
            }
            i++;
        }
        p1->next=nullptr;
        break;
    case WORKER:
        p1=new Worker();
        h=p1;
        while(i<n)
        {
            std::cout<<"Input"<<i+1<<
                       "person Information(name,wage):"<<std::endl;
            std::cin>>name>>wage;
            p1->setName(name);
            p1->setWages(wage);
            if(i+1<n)
            {
                p2=new Worker();
                p1->next=p2;
                p1=p2;
                p2=nullptr;
            }
            i++;
        }
        p1->next=nullptr;
        break;
    default:
        break;
    }
    if(!h)
        qDebug()<<"head is null";
    return h;
}

void printList(Person *h)
{
    Person *p1=nullptr;
    Person *p2=nullptr;
    std::string name;
    int wage;

    p1=h;

    while(p1!=nullptr)
    {
        name=p1->getName();
        wage=p1->getWages();
        p1->identity();
        std::cout<<"name:"<<name<<" "<<"wage:"<<wage<<std::endl;
        p2=p1->next;
        p1=p2;
    }
    p1=p2=nullptr;
}


void deleteList(Person *h)
{
    Person *p1=nullptr;
    Person *p2=nullptr;

    p1=h;
    while(p1!=nullptr)
    {
        p2=p1->next;
        delete p1;
        p1=p2;
    }
    p1=p2=nullptr;
}


展开阅读全文

没有更多推荐了,返回首页