Polymorphism/C: Difference between revisions

Updated formatting to use <syntaxhighlight>.
(Moved Polymorphism C example to here)
 
(Updated formatting to use <syntaxhighlight>.)
 
(3 intermediate revisions by 3 users not shown)
Line 1:
{{Programming-example-page|Polymorphism}}
<syntaxhighlight lang="c">/* After reading this you may understand */
/* why Bjarne Stroustrup's invented C++ */
#if defined( _WIN32 ) || defined( MSC_VER )
#define FN_PTR(x) (& x)
#else
#define FN_PTR(x) (x)
#endif
 
typedef struct Point
'''Compiler:''' GCC, MSVC, BCC, Watcom
{
int x;
int ry;
void (*dtor)(); /* virtual */
void (*print)(); /* virtual */
} Point;
 
Point* Point_new0()
'''Libraries:''' Standard
{
/* After reading this you may understand */
Point* pthis = (Point*) malloc( sizeof( Point ) );
/* why Bjarne Stroustrup's invented C++ */
memcpymemset(pthis, p0, sizeof( Point ) );
#if defined( _WIN32 ) || defined( MSC_VER )
#definepthis->dtor = FN_PTR(x) (& xPoint_dtor);
pthis->print = FN_PTR(Point_print);
#else
}
#define FN_PTR(x) (x)
#endif
 
Point* Point_new1(int x0)
typedef struct Point
{
Point* pthis = malloc( sizeof( Point ) );
int x;
pthis->x = int yx0;
pthis->y = 0;
void (*dtor)(); /* virtual */
pthis->dtor = FN_PTR(Point_dtor);
void (*print)(); /* virtual */
pthis->print = FN_PTR(Point_print);
} Point;
}
 
Point* Point_new0Point_new2(int x0, int y0)
{
Point* pthis = (Point*) malloc( sizeof( Point ) );
memset(pthis, 0, sizeof( Point->x )= )x0;
pthis->dtor y = FN_PTR(Point_dtor)y0;
pthis->printdtor = FN_PTR(Point_printPoint_dtor);
pthis->dtor print = FN_PTR(Point_dtorPoint_print);
}
}
 
void Point_delete(Point** Point_new1(int x0pthis)
{
if(pthis && *pthis)
{
Point(* pthis = (Point*) malloc->dtor( sizeof( Point ) );
free(*pthis); *pthis->x = x0NULL;
pthis->y = 0;
pthis->dtor = FN_PTR(Point_dtor);
pthis->print = FN_PTR(Point_print);
}
}
 
Point* Point_new2Point_copy(int x0, intPoint* y0p)
{
Point* pthis = (Point*) malloc( sizeof( Point ) );
memcpy(pthis, p, sizeof( Point ) );
pthis->x = x0;
pthis->ydtor = y0FN_PTR(Point_dtor);
pthis->dtor print = FN_PTR(Point_dtorPoint_print);
return pthis->print = FN_PTR(Point_print);
}
 
int void Point_deletePoint_getX(Point** pthis) { return pthis->x; }
int Point_getY(Point* pthis) { return pthis->y; }
{
int Point_setX(Point* pthis, int x0) { pthis->x = x0; }
if(pthis && *pthis)
int Point_setY(Point* pthis, int y0) { pthis->y = y0; }
{
void Point_print() { printf("Point\n"); }
(*pthis)->dtor();
void Point_dtor() {}
free(*pthis); *pthis = NULL;
}
}
 
Point* Point_copy(Point* p)
{
Point* pthis = (Point*) malloc( sizeof( Point ) );
memcpy(pthis, p, sizeof( Point ) );
pthis->dtor = FN_PTR(Point_dtor);
pthis->print = FN_PTR(Point_print);
return pthis;
}
 
int Point_getX(Point* pthis) { return pthis->x; }
int Point_getY(Point* pthis) { return pthis->y; }
int Point_setX(Point* pthis, int x0) { pthis->x = x0; }
int Point_setY(Point* pthis, int y0) { pthis->y = y0; }
void Point_print() { printf("Point\n"); }
void Point_dtor() {}
 
// Trick: This way Circle.x, Circle.y, Circle.r are available
typedef union Circle
{
Point point;
struct _Circle
{
Point point;
structint _Circler;
{};
} Circle;
Point point;
int r;
};
} Circle;
 
 
Circle* Circle_new0()
{
Circle* pthis = (Circle*) malloc( sizeof( Circle ) );
memset(pthis, 0, sizeof( Circle ) );
pthis->dtor = FN_PTR(Circle_dtor);
pthis->print = FN_PTR(Circle_print);
}
 
Circle* Circle_new1(int x0)
{
Circle* pthis = (Circle*) malloc( sizeof( Circle ) );
pthis->x = x0;
pthis->y = 0;
pthis->r = 0;
pthis->dtor = FN_PTR(Circle_dtor);
pthis->print = FN_PTR(Circle_print);
}
 
Circle* Circle_new2(int x0, int y0)
{
Circle* pthis = (Circle*) malloc( sizeof( Circle ) );
pthis->x = x0;
pthis->y = y0;
pthis->r = 0;
pthis->dtor = FN_PTR(Circle_dtor);
pthis->print = FN_PTR(Circle_print);
}
 
Circle* Circle_new3(int x0, int y0, int r0)
{
Circle* pthis = (Circle*) malloc( sizeof( Circle ) );
pthis->x = x0;
pthis->y = y0;
pthis->r = r0;
pthis->dtor = FN_PTR(Circle_dtor);
pthis->print = FN_PTR(Circle_print);
}
 
Circle* Circle_newP0(Point* p)
{
Circle* pthis = (Circle*) malloc( sizeof( Circle ) );
pthis->x = p->x;
pthis->y = p->y;
pthis->r = 0;
pthis->dtor = FN_PTR(Circle_dtor);
pthis->print = FN_PTR(Circle_print);
}
 
Circle* Circle_newP1(Point* p, int r0)
{
Circle* pthis = (Circle*) malloc( sizeof( Circle ) );
pthis->x = p->x;
pthis->y = p->y;
pthis->r = r0;
pthis->dtor = FN_PTR(Circle_dtor);
pthis->print = FN_PTR(Circle_print);
}
 
void Circle_delete(Circle** pthis)
{
if(pthis && *pthis)
{
if(pthis && *pthis)->dtor();
free(*pthis)->dtor(); *pthis = NULL;
{
(*pthis)->dtor();
free(*pthis); *pthis = NULL;
}
}
}
 
Circle* Circle_copy(Circle* c)
{
Circle* pthis = (Circle*) malloc( sizeof( Circle ) );
memcpy(pthis, c, sizeof( Circle ) );
pthis->dtor = FN_PTR(Circle_dtor);
pthis->print = FN_PTR(Circle_print);
return pthis;
}
 
int Circle_getX(Circle* pthis) { return pthis->x; }
int Circle_getY(Circle* pthis) { return pthis->y; }
int Circle_getR(Circle* pthis) { return pthis->r; }
int Circle_setX(Circle* pthis, int x0) { pthis->x = x0; }
int Circle_setY(Circle* pthis, int y0) { pthis->y = y0; }
int Circle_setR(Circle* pthis, int r0) { pthis->r = r0; }
void Circle_print() { printf("Circle\n"); }
void Circle_dtor() {}
 
int main()
{
Point* p = Point_new0();
Point* c = (Point*)Circle_new0();
p->print();
c->print();
return 0;
}</syntaxhighlight>
}
1

edit