C语言的继承与多态

1 篇文章 0 订阅

这篇文章旨在记录自己对C语言的封装、继承与多态的学习过程,参考了CSDN上的一篇博客,本来要贴出来地址,无奈无痕浏览模式下找不到在哪里了。不过这并不影响我的分享。

对于继承和多态,我这里不做解释,看到这篇文章自然有一定的基础,解释自然是多余了。既然有基础了,那我直接上代码,供伙伴们查看和觉悟即可。

为方便无基础的伙伴,还是要提点一下关键,至少看不懂的时候,可快速自检自己的知识盲区。阅读源码的基础如下:

1. 熟悉C/C++语言,至少其中一种。

2. 了解内存模型,学习过C++的,自然看一下就明白了。


源码来了:

//Animal.h

#ifndef _ANIMAL_H_
#define __ANIMAL_H_

//add virtual tables
struct AnimalVTable;

//def parent struct
typedef struct {
    struct AnimalVTable *vptr;//pointer to vritual table
    int Age;
    int Weight;
}Animal;

//virtual table
struct AnimalVTable{
    void (*say)(Animal* this);// virtual functions pointer
    void (*action)(Animal* this);

};

//parent virtual fun realization
void Animal_Say(Animal* this);
void Animal_Action(Animal* this);


//declare constructor
void Animal_Ctor(Animal* this,int age,int weight);

//functions for getting parent's members
int Animal_GetAge(Animal* this);
int Animal_GetWeight(Animal* this);

//functions for setting parents' members
void Animal_SetAge(Animal* this,int age);
void Animal_SetWeight(Animal* this,int weight);

#endif
//Animal.c

#include"Animal.h"
#include<assert.h>
#include<stdio.h>

//parent virtual fun realization
static void _Animal_Say(Animal* this)
{
    printf("Animal say\n");
    // assert(0);
}
//parent virtual fun realization
static void _Animal_Action(Animal* this)
{
    printf("Animal action\n");
    // assert(0);
}


void Animal_Ctor(Animal* this,int age,int weight)
{
    static struct AnimalVTable animal_vptr={_Animal_Say,_Animal_Action};
    this->vptr=&animal_vptr;
    this->Age=age;
    this->Weight=weight;
}

int Animal_GetAge(Animal* this)
{
    return this->Age;
}
int Animal_GetWeight(Animal* this)
{
    return this->Weight;
}

void Animal_SetAge(Animal* this,int age)
{
    this->Age=age;
}
void Animal_SetWeight(Animal* this,int weight)
{
    this->Weight=weight;
}

void Animal_Say(Animal* this)
{
    this->vptr->say(this);
}
void Animal_Action(Animal* this)
{
    this->vptr->action(this);
}
//Dog.h
#ifndef _DOG_H_
#define _DOG_H_

// #include"Animal.h"
struct Animal;

//def child stuct
typedef struct{
    Animal parent;
    int Legs;
}Dog;

//constructor
void Dog_Ctor(Dog* this,int age,int weight,int legs);

int Dog_GetAge(Dog* this);
int Dog_GetWeight(Dog* this);
int Dog_GetLegs(Dog* this);


#endif
//Dog.c
#include"Animal.h"
#include"Dog.h"
#include"stdio.h"

static void _Dog_Say(Dog *this)
{
    printf("today dog say\n");
}
static void _Dog_Action(Dog *this)
{
    printf("today dog action\n");
}
//constructor
void Dog_Ctor(Dog* this,int age,int weight,int legs)
{
    Animal_Ctor(&this->parent,age,weight);
    static struct AnimalVTable dog_vtbl={_Dog_Say,_Dog_Action};
    this->parent.vptr=&dog_vtbl;
    this->Legs=legs;
}


int Dog_GetAge(Dog* this)
{
    return Animal_GetAge(&this->parent);
}
int Dog_GetWeight(Dog* this)
{
    return Animal_GetWeight(&this->parent);
}
int Dog_GetLegs(Dog* this)
{
    return this->Legs;
}
//Test code main.c
#include<stdio.h>

#include"Animal.h"
#include"Dog.h"

int main(int argc,char** argv)
{
    Dog an;
    Dog_Ctor(&an,1,6,4);

    Animal* pa=(Animal*)&an;
    Animal_Say(pa);
    Animal_Action(pa);

    Animal aa;
    Animal_Ctor(&aa,1,6);
    Animal_Say(&aa);
    Animal_Action(&aa);
    //printf("age=%d,weight=%d,legs=%d\n",Dog_GetAge(&an),Dog_GetWeight(&an),Dog_GetLegs(&an));
    return 0;
}

执行测试输出

today dog say
today dog action
Animal say
Animal action

编译的话用cmake,编译文件如下:

cmake_minimum_required(VERSION 3.16)
project(c_opp)

file(GLOB SRCS ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
message(${SRCS})
add_executable(${PROJECT_NAME} ${SRCS})

至于编译指令就自行搜索了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jeff_ROS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值