Re: OOP



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
.



Relevant Pages

  • Re: what is the best datatype for..
    ... If you know you'll have say 1000 possible indices, then define an array of ... 1000 structs, then when you see a value of 605, you simply do ... you'd have to sort 100,000 entries. ... then converting it to a list and sorting the list. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Sorting
    ... I have an array that I want to sort without losing the index ... of structs where each struct holds an X value and its index: ...
    (comp.lang.c)
  • Re: Sorting a struct
    ... >> I'm having a problem sorting an array of structs that is a class ... >> then I pass sort a pointer to the first array element and the number ... >> use in a card game, and I need to sort the cards as part of my shuffle ...
    (comp.lang.cpp)
  • Re: Matrix with text problem
    ... Use an array of structs, ... represents the numbers on a numeric format. ... Or sort both data sets, ...
    (comp.soft-sys.matlab)
  • Re: macro for enum to string?
    ... >>This sort of thing is a poor solution to the problem, in my opinion, ... >>Any change to the enum requires a change to the array, ... I've never macros used quite that way. ...
    (comp.lang.cpp)