c + + - pure virtual functions and abstract classes

Pure virtual functions and abstract classes

  • C Oriented Interface Programming and C polymorphism
    • Function type syntax Foundation
    • Analysis of the idea of function pointer as function parameter (callback function)
    • Two ways to use function pointer as function parameter (forward call and reverse call)
  • Pure virtual function abstract class
    • Basic concepts of abstract classes
    • The application of abstract class in multi inheritance
    • Case reinforcement of abstract class oriented programming
    • C Oriented Interface Programming and C polymorphism
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;


//Graphic class
//If a class has a pure virtual function 
//We call this class an abstract class.
//No matter whether there are member properties in this class or not, as long as this class has pure virtual functions, it is an abstract class, and the abstract class cannot be instantiated.
class Shape
{
public:
    //The method of finding the area of figure
    //The graph class declares a method getArea(), which is a pure virtual function without function implementation.
    virtual double getArea() = 0;
};

//triangle
class Tri :public Shape
{
public:
    Tri(int a, int h)
    {
        this->a = a;
        this->h = h;
    }
    virtual double getArea() {
        cout << "Face value of triangle" << endl;
        return 0.5*a*h;
    }

private:
    int a;
    int h;
};

//square:
//If we say a common class inherits the class with pure virtual function, if we don't rewrite pure virtual function, it is still an abstract class.
//It cannot be instantiated. If you want to instantiate it, you must rewrite all pure virtual functions in the parent class
class Rect : public Shape
{
public:
    Rect(int a) {
        this->a = a;
    }
    virtual double getArea() {
        cout << "Square area" << endl;
        return a*a;
    }
private:
    int a;//Square side length
};

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

    virtual double getArea()
    {
        cout << "Circular area" << endl;

        return 3.14*r * 4;
    }


private:
    int  r;
};


//Writing an architecture function for abstract class
void printArea(Shape *sp)
{
    sp->getArea();
}

//Business layer oriented abstract class programming
int main(void)
{
    //All variable types used in main are the types of abstract class 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;
}

A little practice of the previous knowledge

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

//Big brother's empty title
class BigBrother
{
public:
    //Can hit people.
    virtual void fightPeople() = 0;
};

//Invincible Eastern
class EastNeverLose :public BigBrother
{
public:
    virtual void fightPeople()  {
        cout << "Hit people with sunflower Scripture" << endl;
    }
};

//Wuyazi
class Wuyazi :public BigBrother
{
public:
    virtual void fightPeople()  {
        cout << "Hit people with northern hell skill" << endl;
    }
};



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

    //Elder brother, you beat people for me.
    bigbrother->fightPeople();

    delete bigbrother;
    
    return 0;
}

Pure virtual function and multiple inheritance

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

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


//Interface 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;
}

Computer assembly -- small exercises

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>


using namespace std;

//--------Abstraction layer---------
//Abstract CPU class
class CPU
{
public:
    CPU();
    virtual void caculate() = 0;
};

//Abstract card class
class Card
{
public:
    virtual void display() = 0;
};

//Abstract memory class
class Memory
{
public:
    virtual void storage() = 0;
};

//Architecture class
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;
};
// --------------------------

//-----------Implementation layer----------
//Specific Intel CPU
class IntelCPU :public CPU
{
public:
    virtual void caculate() {
        cout << "Intel CPU It's counting" << endl;
    }
};

class IntelCard :public Card
{
public:
    virtual void display() {
        cout << "Intel Card Start showing" << endl;

    }
};

class IntelMem :public Memory {
public:
    virtual void storage() {
        cout << "Intel mem It's starting to store" << endl;

    }
};

class NvidiaCard :public Card
{
public:
    virtual void display() {
        cout << "Nvidia The video card starts to show" << endl;
    }
};

class KingstonMem :public Memory {
public:
    virtual void storage() {
        cout << "KingstonMem It's starting to store" << endl;
    }
};

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



//--------Business layer-------------------
int main(void)
{
    //1 assemble the first intel series computer
#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;
}

Keywords: C++ Programming

Added by davitz38 on Sun, 22 Dec 2019 16:06:24 +0200