Re: OOP
- From: pete <pfiland@xxxxxxxxxxxxxx>
- Date: Mon, 30 Jun 2008 13:43:00 -0400
Tor Rustad wrote:
pete wrote:campyhapper@xxxxxxxxx wrote:Hi folks,
I tend to prefer C, and of course I know that structs
can be used in C to achieve something like
an object-oriented design. And I prefer C in part
because C++ has, I think, grown into a bit of a
monster wherein readability is sacrificed.
But I wonder, has anyone ever tried to create a
sort of lite version of C++, a C+ if you will, that adds
to C just a few key features and disallows things
like templates, multiple inheritance and the like?
I got the e_type interface for sorting functions from Dann Corbit.
It's a step in the direction towards templates. I like it.
All you have to do is
#define E_TYPE /* array element type */
and
#define GT(A, B) /* a "Greater Than" macro */
and you can sort a one dimensional array of any type.
Interesting, this reminds me of the ADT trick used by Sedgewick, in his "Algorithms in C" books, where an algorithm implementation could look like this:
#include <stdlib.h>
#include "Item.h"
#include "STACK.h"
static Item *s;
static int N;
void STACKinit(int maxN)
{ s = malloc(maxN*sizeof(Item)); N = 0; }
int STACKempty()
{ return N == 0; }
void STACKpush(Item item)
{ s[N++] = item; }
Item STACKpop()
{ return s[--N]; }
while the user write an <Item.h> and call the generic algorithms like this:
#include <stdio.h>
#include <string.h>
#include "Item.h"
#include "STACK.h"
main(int argc, char *argv[])
{ char *a = argv[1]; int i, N = strlen(a);
STACKinit(N);
for (i = 0; i < N; i++)
{
if (a[i] == ')')
printf("%c ", STACKpop());
if ((a[i] == '+') || (a[i] == '*'))
STACKpush(a[i]);
if ((a[i] >= '0') && (a[i] <= '9'))
printf("%c ", a[i]);
}
printf("\n");
}
I have a six file program for timing sorting functions here:
http://www.mindspring.com/~pfilandr/C/e_driver/
The element type and GT macros are defined in e_driver.h,
and all the other *.h files and e_driver.c, include it.
It's set up so that simply by flipping a macro to either 1 or 0,
the program will either sort arrays of unsigned long
or arrays of structs,
which contain an array of 50 char and a type double data member.
--
pete
.
- Prev by Date: Re: Newbie Question about multiple system calls within loops
- Next by Date: Re: adding colums to text
- Previous by thread: Re: OOP
- Next by thread: Re: OOP
- Index(es):
Relevant Pages
|