c++-純虛函數和抽象類

純虛函數和抽象類

  • C面向接口編程和C多態
    • 函數類型語法基礎
    • 函數指針作函數參數(回調函數)思想剖析
    • 函數指針作函數參數兩種用法(正向調用、反向調用)
  • 純虛函數 抽象類
    • 抽象類基本概念
    • 抽象類在多繼承中的應用
    • 面向抽象類編程案例強化
    • C面向接口編程和C多態
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;


//圖形類
//若是說一個類 擁有一個純虛函數 
//就稱這個類是一個抽象類。
//無論這個類中有沒有成員屬性,只要這個類有純虛函數,就是一個抽象類, 抽象類就是不可以實例化的。
class Shape
{
public:
    //求圖形面積的方法
    //表示圖形類聲明一個方法getArea(),它是一個純虛函數,沒有函數的實現。
    virtual double getArea() = 0;
};

//三角形
class Tri :public Shape
{
public:
    Tri(int a, int h)
    {
        this->a = a;
        this->h = h;
    }
    virtual double getArea() {
        cout << "三角形求麪價" << endl;
        return 0.5*a*h;
    }

private:
    int a;
    int h;
};

//正方形:
//若是說一個普通類,繼承擁有純虛函數的類,若是說不重寫純虛函數,依然是一個抽象類。
//依然不能被實例化, 若是想實例化, 必需要重寫這個父類中全部純虛函數
class Rect : public Shape
{
public:
    Rect(int a) {
        this->a = a;
    }
    virtual double getArea() {
        cout << "正方形求面積" << endl;
        return a*a;
    }
private:
    int a;//正方形邊長
};

class Circle :public Shape
{
public:
    Circle(int r)
    {
        this->r = r;
    }

    virtual double getArea()
    {
        cout << "圓形求面積" << endl;

        return 3.14*r * 4;
    }


private:
    int  r;
};


//面向抽象類寫一個架構函數
void printArea(Shape *sp)
{
    sp->getArea();
}

//業務層  面向的抽象類編程
int main(void)
{
    //main 中全部使用的變量類型 都是 抽象類Shape的類型。
    Shape *sp1 = new Rect(10);
    //sp1->getArea();

    Shape *sp2 = new Circle(20);
    //sp2->getArea();

    Shape *sp3 = new Tri(10, 20);
    //sp3->getArea();


    printArea(sp1);
    printArea(sp2);
    printArea(sp3);

    return 0;
}

上一個知識的小練習

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

//大哥的虛銜
class BigBrother
{
public:
    //會打人。
    virtual void fightPeople() = 0;
};

//東方不敗
class EastNeverLose :public BigBrother
{
public:
    virtual void fightPeople()  {
        cout << "使用了葵花寶典區打人" << endl;
    }
};

//無崖子
class Wuyazi :public BigBrother
{
public:
    virtual void fightPeople()  {
        cout << "使用北冥神功打人" << endl;
    }
};



//boss
int main(void)
{
    BigBrother *bigbrother = new Wuyazi;

    //大哥你給我去打人。
    bigbrother->fightPeople();

    delete bigbrother;
    
    return 0;
}

純虛函數和多繼承

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

//接口1
class Interface1
{
public:
    virtual void func1(int a, int b) = 0;
    virtual void func3(int a, int b) = 0;
};


//接口2
class Interface2
{
public:
    virtual void func2(int a) = 0;
};

class Child :public Interface1, public Interface2
{
public:
    virtual void func1(int a, int b)
    {
        cout << "func1" << endl;
    }
    virtual void func3(int a, int b) {
        cout << "func3" << endl;

    }


    virtual void func2(int a)
    {
        cout << "func2 " << endl;
    }
};

int main(void)
{
    Interface1 *if1 = new Child;

    if1->func1(10, 20);
    if1->func3(100, 200);


    Interface2 *if2 = new Child;

    if2->func2(10);

    return 0;
}

電腦組裝--小練習

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

//--------  抽象層---------
//抽象CPU類
class CPU
{
public:
    CPU();
    virtual void caculate() = 0;
};

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

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

//架構類
class Computer
{
public:
    Computer(CPU *cpu, Card *card, Memory *mem)
    {
        this->cpu = cpu;
        this->card = card;
        this->mem = mem;
    }

    void work()
    {
        this->cpu->caculate();
        this->card->display();
        this->mem->storage();
    }

    ~Computer() {
        if (this->cpu != NULL) {
            delete this->cpu;
        }
        if (this->card != NULL) {
            delete this->card;
        }
        if (this->mem != NULL) {
            delete this->mem;
        }
    }
private:
    CPU* cpu;
    Card*card;
    Memory *mem;
};
// --------------------------

//-----------實現層----------
//具體的IntelCPU
class IntelCPU :public CPU
{
public:
    virtual void caculate() {
        cout << "Intel CPU開始計算了" << endl;
    }
};

class IntelCard :public Card
{
public:
    virtual void display() {
        cout << "Intel Card開始顯示了" << endl;

    }
};

class IntelMem :public Memory {
public:
    virtual void storage() {
        cout << "Intel mem開始存儲了" << endl;

    }
};

class NvidiaCard :public Card
{
public:
    virtual void display() {
        cout << "Nvidia 顯卡開始顯示了" << endl;
    }
};

class KingstonMem :public Memory {
public:
    virtual void storage() {
        cout << "KingstonMem 開始存儲了" << endl;
    }
};

//--------------------------



//--------業務層-------------------
int main(void)
{
    //1 組裝第一臺intel系列的電腦
#if 0
    CPU *intelCpu = new IntelCPU;
    Card *intelCard = new IntelCard;
    Memory *intelMem = new IntelMem;

    Computer *com1 = new Computer(intelCpu, intelCard, intelMem);

    com1->work();

    Card *nCard = new NvidiaCard;
    Memory* kMem = new KingstonMem;

    Computer *com2 = new Computer(intelCpu, nCard, kMem);

    com2->work();

    delete intelCpu;
#endif
    Computer *com1 = new Computer(new IntelCPU, new IntelCard, new IntelMem);
    com1->work();
    delete com1;


    return 0;
}
相關文章
相關標籤/搜索