C++中级-多态

基本概念

#include <iostream>
#include <string>
using namespace std;

//抽象类,无法实例化对象。
class Animal {
public:
    //3.通过加virtual关键字使其变虚函数。
    virtual void talk() { cout << "Animal is talking " << endl; }

};


class Dog:public Animal {
public:
    void talk() { cout << "Dog is talking" << endl; }
};

class Pig:public Animal {
public:
    void talk() { cout << "Pig is talk" << endl; }
};
//1.在编译阶段,已确定函数地址为animal了。
//3.1在运行阶段,因为有了virtual修饰,vfptr就在vfptb表中修改确定成传入的对象函数地址了。
void Mainwork(Animal& animal){ animal.talk(); } 



int main() { 
    Dog d;
    //2.所以此刻只打印Animal类中的talk方法。
    Mainwork(d);
    //3.2在加virtual后,此时就能打印Dog的talk方法了

    Pig p;
    Mainwork(p);
    return 0; 
}

纯虚函数和抽象类

#include <iostream>
using namespace std;

class Base {
public:

    virtual void func() = 0;
};


class Son:public Base {
public:
    virtual void func() {};//子类重写父类纯虚函数,才能实例化对象

};


int main() { 
    //Base b;//无法实例化对象
    Son s;//子类重写父类纯虚函数,才能实例化对象


    return 0; 
}

虚析构和纯虚析构

#include <iostream>
using namespace std;

//通过虚析构和纯虚析构才能走到子类的析构。
/*
用来解决通过父类指针释放子类对象;
如果子类没有堆区数据。就用不着它们;
拥有纯虚析构函数的类也属于抽象类;
*/

class Animal {
public:
    virtual void speak() = 0;
    //virtual ~Animal() {};//1.至为虚析构。才会走~Dog析构。
    virtual ~Animal() = 0;//2.纯虚析构声明。
};
Animal::~Animal() {};//2.1纯虚析构定义。才会走~Dog析构。


class Dog:public Animal{
public:
    Dog(string name) {
        d_name = new string(name);
    }
    virtual void speak() { cout << *d_name<<" DOG SPEAKING!\n"; }
    string *d_name;

    ~Dog() {
        if (d_name !=NULL)
        {
            cout<< *d_name<<"is gone"<<endl;
            delete d_name;
        }
    };
};


void work(Animal* aml) {
    aml->speak();
    delete aml;
};

int main() { 
    work(new Dog("LUCS"));
    return 0; 
}

多态应用案例

 

#include <iostream>
using namespace std;

//抽象类
class ABstractDrink {
public:
    virtual void Boil() = 0;
    virtual void Brew() = 0;
    virtual void Pourincup() = 0;
    virtual void PutAnother() = 0;

    void makeDrink() {
        Boil();
        Brew();
        Pourincup();
        PutAnother();
        cout << "Finished!" << endl;
    };
};


class Coffee :public ABstractDrink {
public:
    virtual void Boil() { cout << "boil coffee \n"; };
    virtual void Brew() { cout << "brew coffee \n"; };
    virtual void Pourincup() { cout << "pour in coffee \n"; };
    virtual void PutAnother() { cout << "put in coffee \n"; };

};

class Milk :public ABstractDrink {
public:
    virtual void Boil() { cout << "boil Milk \n"; };
    virtual void Brew() { cout << "brew Milk \n"; };
    virtual void Pourincup() { cout << "pour in Milk \n"; };
    virtual void PutAnother() { cout << "put in Milk \n"; };

};


void work(ABstractDrink* abs) {
    abs->makeDrink();
    delete abs; 
};

int main() { 
    work(new Coffee);
    work(new Milk);

    return 0; 
}

 

多态应用案例二-做电脑

 

#include <iostream>
#include <string>
using namespace std;

//零件
class CPU{
public:
    virtual void run() =0;
};

class GPU{
public:
    virtual void display() = 0;
};

class Memory{
public:
    virtual void storge() = 0;
};

//厂商
class AMD:public CPU{
public:
    virtual void run() {
        cout <<"Cpu running...." << endl;;
    }
};

class NIVADA :public GPU {
public:
    virtual void display() {
        cout << "Gpu displayed" << endl;
    }
};

class Kinston :public Memory {
public:
    virtual void storge() {
        cout << "memory storged" << endl;
    }
};

//实体
class PC{
public:
    PC(CPU* cpu, GPU* gpu, Memory* memory) {
        ccpu = cpu;
        ggpu = gpu;
        mmemory = memory;
    };

    void made() {
        cout << "making Computer !!!!" << endl;
        ccpu->run();
        ggpu->display();
        mmemory->storge();
        
    };

    ~PC() {
        if (ccpu!=NULL && mmemory!=NULL && ggpu!=NULL)
        {
            delete ccpu;  delete ggpu, delete mmemory;
            cout << "Computer made" << endl;
        }
    };

private:
    CPU* ccpu;
    GPU* ggpu;
    Memory* mmemory;
};





int main() { 
    CPU* amd = new AMD;
    GPU* nivada = new NIVADA;
    Memory* memory = new Kinston;

    PC* pc = new PC(amd, nivada, memory);
    pc->made();
    delete pc;

    return 0; 
}

 

posted @ 2021-01-14 11:43  y0um  阅读(76)  评论(0编辑  收藏  举报

新人优惠服务器