Skip to content

Instantly share code, notes, and snippets.

@otaks
Last active June 11, 2016 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otaks/0b4ec7281479b2aa78f85ca079536e46 to your computer and use it in GitHub Desktop.
Save otaks/0b4ec7281479b2aa78f85ca079536e46 to your computer and use it in GitHub Desktop.
c keisho
#include "AnimalP.h"
int Animal_init(Animal animal, char* name) {
memset( animal->name, 0x00, sizeof( animal->name ) );
strcpy( animal->name, name );
}
int Animal_done( Animal animal ) {
/* do nothing */
}
void Animal_sayName( Animal animal ) {
printf( "%s\n", animal->name );
}
#pragma once
typedef struct animal* Animal;
void Animal_sayName( Animal animal );
#pragma once
#include "Animal.h"
struct animal {
char name[ 20 ];
};
int Animal_init( Animal animal, char* name );
int Animal_done( Animal animal );
#include <stdlib.h>
#include "dog.h"
#include "AnimalP.h"
struct dog {
struct animal; //スーパークラスは構造体の一番上に定義
//以降犬クラス固有のメンバ
};
Dog Dog_create( char* name ) {
Dog t = calloc( sizeof( struct dog ) ,1);
Animal_init( (Animal)t, name );
return t;
}
void Dog_berg( Dog dog )
{
printf( "wan!!\n" );
}
void Dog_delete( Dog dog ){
Animal_done( ( Animal )dog );
free( dog );
}
#pragma once
#include <stdio.h>
typedef struct dog* Dog;
Dog Dog_create( char* );
void Dog_berg( Dog );
void Dog_delete( Dog );
#include "animal.h"
#include "dog.h"
int main() {
Dog one = Dog_create("lewis");
Dog two = Dog_create( "mike" );
Animal_sayName( ( Animal )one );
Animal_sayName( ( Animal )two );
Dog_delete( one );
Dog_delete( two );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment