栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言的继承与多态

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言的继承与多态

这篇文章旨在记录自己对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
#include

//parent virtual fun realization
static void _Animal_Say(Animal* this)
{
    printf("Animal sayn");
    // assert(0);
}
//parent virtual fun realization
static void _Animal_Action(Animal* this)
{
    printf("Animal actionn");
    // 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 sayn");
}
static void _Dog_Action(Dog *this)
{
    printf("today dog actionn");
}
//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

#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=%dn",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})

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/395961.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号