c_cpp C中的多态,虚函数和继承

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C中的多态,虚函数和继承相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>

/* Convenience, C does not have bool */
enum Bool { FALSE, TRUE }; 

/* Typedefs so we don't have to have "struct Gun" everywhere */
typedef struct Gun Gun;
typedef struct Shotgun Shotgun;
typedef enum Bool Bool;

/* Our base class */
struct Gun
{
    int damage;
    int currentMagazine;
    int magazineCapacity;
    float reloadTime;
    void (*Fire)(const Gun *);
};

/* Derived class */
struct Shotgun
{
    Gun base; /* Contains pointer to base class as first member so we can (* Gun) cast */
    Bool splashDamage;
    Bool sawedOff;
    void (*Cock)(const Shotgun *);
};

/* Convenience macro to always safely pass instance of pointer to its own function */
#define Gun_Fire(this) \
    ((Gun *)this)->Fire((Gun *)this)

/* Constructor */
Shotgun * Shotgun_new();
/* Overridden Fire function */
void ShotgunFire(const Gun *);
/* Derived class specific function */
void ShotgunCock(const Shotgun *);
#define Shotgun_Cock(this) \
    (this)->Cock(this)

Shotgun * Shotgun_new(Bool splashDamage, Bool sawedOff)
{
    /* Allocate object */
    Shotgun *shotgun = (Shotgun *)malloc(sizeof(Shotgun));
    
    if (shotgun != 0)
    {
        /* Setup overridden functions for child */
        ((Gun *)shotgun)->Fire = ShotgunFire;
        shotgun->Cock = ShotgunCock;

        /* Initialize fields */
        shotgun->splashDamage = splashDamage;
        shotgun->sawedOff = sawedOff;
        
        if (splashDamage == 1)
        {
            ((Gun *)shotgun)->damage = 10;
            shotgun->splashDamage = 3;
        }
        else if (splashDamage == 0)
        {
            ((Gun *)shotgun)->damage = 15;
            shotgun->splashDamage = 0;
        }
    }

    return shotgun;
}

void Shotgun_delete(void *shotgun)
{
    free((Shotgun *)shotgun);
}

void ShotgunFire(const Gun *base)
{
    Shotgun *this = (Shotgun *)base;
    printf("Dealt %d damage to a demon!\n", ((Gun *)this)->damage);
    if (this->splashDamage > 0)
    {
        printf("...And %d damage to a random skeleton\n", this->splashDamage);
        printf("...And %d damage to a random zombie\n", this->splashDamage);
        printf("...And %d damage to a random zombie\n", this->splashDamage);
    }
}

void ShotgunCock(const Shotgun *this)
{
    if (this->sawedOff)
        printf("Shotgun cocked. Made a COOLER sound!\n");
    else
        printf("Shotgun cocked. Made a cool sound!\n");
}
 
int main()
{
    /* Allocate object using constructor, initializes and sets up virtuals */
    Gun *myShotgun = (Gun *)Shotgun_new(TRUE, TRUE);
    
    /* Call a virtual function on our object! */
    Gun_Fire(myShotgun);
    /* Call a function unique to our derived object */
    Shotgun_Cock((Shotgun *)myShotgun);

    Shotgun_delete(myShotgun);

    return 0;
}

以上是关于c_cpp C中的多态,虚函数和继承的主要内容,如果未能解决你的问题,请参考以下文章

C++--多态

C++---多态

C++多态

C++之多态总结(多态的定义及实现,抽象类,多态原理,单继承,多继承中的虚函数表)

C++多态

C++——多态