当前位置:   article > 正文

C语言如何实现封装继承和多态_c语言结构体 继承

c语言结构体 继承

1. 封装

成员变量比较好说,关键是成员函数怎么办?C中的结构体如何放一个成员函数呢?
答:可以使用函数指针。

typedef struct Base
{
	int a;
	void (*print)(struct Base* this);
}Base;
//模仿基类构造
Base* baseCtor(int a) {
	Base* this = (Base*)malloc(sizeof(Base));
	if(this){
		this->a = a;
		this->print = printBase;//基类的打印函数
	}
	return this;
}
//模仿基类析构
void baseDtor(Base** p){
	if(*p){
		free(*p);
		*p = NULL;
	}
}

void printBase(Base* this){
	printf("Base contains: %d\n", this->a);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

2. 继承

继承只好通过内嵌一个其他结构体的实例实现。

typedef struct Derived
{
	Base mbase;
	int b;
}Derived;
//模仿派生类构造
Derived* derivedCtor(int a, int b){
	Derived* this = (Derived*)malloc(sizeof(Derived));
	if(this){
		this->mbase.a = a;
		this->b = b;
		this->mbase.print = printDerived;//派生类的打印函数
	}
	return this;
}
//模仿派生类析构
void derivedDtor(Derived** p){
	if(*p){
		free(*p);
		*p = NULL;
	}
}

void printDerived(Base* this){
	Derived* pderived = (Derived*)this;
	printf("Derived contains: %d %d\n", pderived->mbase.a, pderived->b);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

3. 多态

怎么实现多态?需要利用结构体中的函数指针。
如何利用?构造时将其指向不同的函数。
以下是全部代码:

#include <stdio.h>
#include <malloc.h>

typedef struct Base
{
	int a;
	void (*print)(struct Base* this);
}Base;

typedef struct Derived
{
	Base mbase;
	int b;
}Derived;

void printBase(Base* this){
	printf("Base contains: %d\n", this->a);
}

void printDerived(Base* this){
	Derived* pderived = (Derived*)this;
	printf("Derived contains: %d %d\n", pderived->mbase.a, pderived->b);
}

Base* baseCtor(int a) {
	Base* this = (Base*)malloc(sizeof(Base));
	if(this){
		this->a = a;
		this->print = printBase;
	}
	return this;
}
void baseDtor(Base** p){
	if(*p){
		free(*p);
		*p = NULL;
	}
}

Derived* derivedCtor(int a, int b){
	Derived* this = (Derived*)malloc(sizeof(Derived));
	if(this){
		this->mbase.a = a;
		this->b = b;
		this->mbase.print = printDerived;
	}
	return this;
}
void derivedDtor(Derived** p){
	if(*p){
		free(*p);
		*p = NULL;
	}
}

int main(int argc, char* argv[]) 
{
	Derived* pd = derivedCtor(1,2);
	Base* pb = (Base*)pd;//基类指针指向派生类对象
	pb->print(pb);//调用的是派生类的打印函数
	derivedDtor(&pd);
	return 0; 
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

输出:
在这里插入图片描述

4. 总结

  • 封装:结构体内放入函数指针;
  • 继承:结构体内嵌别的结构体;
  • 多态:函数指针指向不同的函数。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/43375
推荐阅读
相关标签
  

闽ICP备14008679号