【C语言】实现类效果

【C语言】实现类的继承

C语言如何实现类的效果?

结构体跟类是一种比较类似的结构,都对成员进行了一定程度的封装。区别在于,结构体的成员默认是public,类的成员默认是private。

.h文件

#ifdef __cplusplus  //******  ADD
extern "C" {        //******  ADD
#endif              //******  ADD

#ifdef BCCWIN

#ifdef DOEXPORT
#define EXPORTSPEC __declspec (dllexport)
#else
#define EXPORTSPEC __declspec (dllimport)
#endif

EXPORTSPEC HANDLE __stdcall setPort(char * name, char* baud,char parity);

EXPORTSPEC int __stdcall closePort(HANDLE port);
#endif

//#ifdef __cplusplus  //******  REMOVE moved to top
//extern "C" {        //******  REMOVE moved to top
//#endif              //******  REMOVE moved to top

#define LINUX

#ifdef LINUX

typedef struct ppi{
	long a,b,c,e;
    int int_a;
    int res;
    float d;
    bool isSucceed;
    daveInterface * di;
    daveConnection * dc;
    _daveOSserialType fds;
    void (*ReadBytes)(struct ppi this,int area,int para1,int para2 ,int para3,int *CopyToBuffer);
    void (*WriteBytes)(struct ppi this,int area,int para1,int para2,int para3,int *theAreaOfValue);
}ppi;


void ReadBytes(struct ppi this,int area ,int para1,int para2,int para3,int *CopyToBuffer);

void WriteBytes(struct ppi this,int area ,int para1,int para2,int para3,int *theAreaOfValue);

ppi *NewPPI(ppi *this);
#endif

#ifdef __cplusplus
}
#endif

.c文件

#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#include"nodavesimple.h"
#include"setport.h"
#include"ppi.h"

void ReadBytes(struct ppi this,int area ,int para1,int para2,int para3,int *CopyToBuffer){
	if(this.isSucceed){
	//	printf("",);
		this.res = daveReadBytes(this.dc,area,para1,para2,para3,CopyToBuffer);
		printf("res=%d\n",this.res);
		this.int_a = daveGetU16(this.dc);
	    	printf("U16:0x%X\n",this.int_a);

	}else{
		printf("error!could not open serial port /dev/ttyUSB0 \n");
	}
}

void WriteBytes(struct ppi this,int area ,int para1,int para2,int para3,int *theAreaOfValue){
	if(this.isSucceed){
		printf("Write\n");
		this.res = daveWriteBytes(this.dc,area,para1,para2,para3,theAreaOfValue);
	}else{
		printf("error!could not open serial port /dev/ttyUSB0 \n");
	}
}

ppi *NewPPI(ppi *this){
//	(this->fds).rfd = serPort("/dev/ttyUSB0","9600",'E');
//	(this->fds).wfd = (this->fds).rfd;
	if((this->fds).rfd>0){
		this->isSucceed = true;
		this->di =daveNewInterface(this->fds, "IF1", 0, daveProtoPPI, daveSpeed187k);
		this->dc =daveNewConnection(this->di, 2, 0, 0);  
		daveConnectPLC(this->dc);
	}else{
		this->isSucceed = false;
	}
	this->ReadBytes = ReadBytes;
	this->WriteBytes = WriteBytes;
}



【C语言】实现类的继承

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值