欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c++-纯虚函数和抽象类

程序员文章站 2022-05-07 09:27:46
纯虚函数和抽象类 + C面向接口编程和C多态 函数类型语法基础 函数指针做函数参数(回调函数)思想剖析 函数指针做函数参数两种用法(正向调用、反向调用) + 纯虚函数 抽象类 抽象类基本概念 抽象类在多继承中的应用 面向抽象类编程案例强化 C面向接口编程和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;
}