2014-5-27 用c实现c++的类

来源:互联网 发布:java新技术框架 编辑:程序博客网 时间:2024/06/08 16:46

C语言的结构体里是不允许有函数的,但可以有指向函数的指针

这里是实现sting.h这个头文件里包含的几个字符串操作函数

1、

定义头文件


定义指

#ifndef CLASS_H_  2 #define CLASS_H_  3 #include <stdio.h>  4   5 typedef struct student st;  6 typedef st *sp;  7 struct student  8 {  9         char *name; 10         int (*stl)(sp s1); 11         void (*stcy)(sp s1,char *s); 12         void (*stct)(sp s1,char *s); 13         int (*stcp)(sp s1,sp s2); 14         void (*shch)(sp s1); 15         void (*ctr)(sp obj,char *name); 16 }; 17  18 int my_strlen(sp s); 19 void my_strcpy(sp s1,char *s); 20 void my_strcat(sp s1,char *s); 21 int my_strcmp(sp s1,sp s2); 22 void my_show(sp s1); 23 void construct (sp obj,char *name); 24 #endif   
针函数shnm,inc,fstr,ctr指向show_name,increase_i,free_struct,construct.想要添加到类来实现的函数


2、分别定义要实现的函数

(1)这个是实现strcpy复制字符串函数

  1 #include "class.h"  2 #include <stdlib.h>  3 void my_strcpy(sp s1,char *s)  4 {  5         int i=0;  6         (s1->name)=(char *)malloc(sizeof(st));  7         while(s[i]!='\0'){  8                 *((s1->name)+i)=s[i];  9                 i++; 10         } 11         *((s1->name)+i)='\0'; 12         return ; 13 }                                                                                <span style="font-family: song, Verdana; line-height: 22.383333206176758px; ">  </span>


(2)这个是实现strcmp比较字符串函数

  1 #include "class.h"  2 int my_strcmp(sp s1,sp s2)  3 {  4         int result;  5         char *a=s1->name;  6         char *b=s2->name;  7         for(;(*a)!='\0'||(*b)!='\0';a++,b++){  8                 if(*a==*b)  9                         result=0; 10                 if((*a)>(*b)) 11                         result=1; 12                 else 13                         result=-1; 14         } 15         switch(result){ 16                 case -1:printf("s1<s2\n");break; 17                 case 0:printf("s1=s2\n");break; 18                 case 1:printf("s1>s2\n");break; 19                 default:break; 20         } 21         return result; 22 }


(3)这个是实现strcat字符串连接函数

  1 #include "class.h"  2 #include <stdlib.h>  3 void my_strcat(sp s1,char * s2)  4 {  5   6         int i,j;  7         (s1->name)=(char *)malloc(sizeof(st));  8                 for(i=0;(s1->name[i])!='\0';i++);  9                         for(j=0;s2[j]!='\0';j++) 10                                 (s1->name)[i+j]=s2[j]; 11         s1->name[i+j]='\0'; 12  13  14         return ; 15 }


(4)这个是实现字符串长度strlen和字符串显示函数show

  1 #include "class.h"  2   3 int my_strlen(sp mm)  4 {   5         int i;  6         char *a=(mm->name);  7         for(i=0;(*a)!='\0';a++)  8                 i++;  9         printf("i=%d\n",i); 10 } 11 void my_show(sp dp) 12 { 13         printf("%s\n",dp->name); 14 }

3、构造函数的定义与使用

15 void construct (sp obj,char *name) 16 { 17         obj->name=name; 18         obj->shch=my_show; 19         obj->stl=my_strlen;  20         obj->stct=my_strcat; 21         obj->stcy=my_strcpy; 22         obj->stcp=my_strcmp; 23   24         return; 25  26 }

  1 #include "class.h"  2 int main(void)  3 {  4         st ldp,ldp1;  5   6         ldp.ctr=construct;  7         ldp.ctr(&ldp,"he");  8         ldp.shch(&ldp);  9  10         ldp.stl(&ldp); 11  12         ldp.stcy(&ldp,"cat"); 13         ldp.shch(&ldp); 14  15         ldp.stct(&ldp,"www"); 16         ldp.shch(&ldp); 17  18         ldp.ctr(&ldp1,"she"); 19         ldp.stcp(&ldp,&ldp1); 20  21         return 0;



0 0
原创粉丝点击