Wednesday, May 6, 2009
Example of object oriented C
I have previously written an entry stating that c can be used as object oriented language. Now I decided to write an example demostrating this. So here it is, a program drawing lottery numbers, written in ansi c, using object oriented approach.
I know the code looks horrible. It is. Object oriented code often is. And this example could have been written so much easier with plain procedural c. But this shows how basic requirements of object orientation are fullfilled. Lottery machine is (virtual) base class, and basic & viking lottery are derived classes. If i had more enthusiasm, i would have written joker class too.--
And as an explanation, in finland we have two different types of lottery, viking lotto and regular lotto. Theres different amount of numbers drawn, but i cant remember the accurate amounts. Theres also so called joker, but i cant remember the amount of numbers in it either.
And as a final note, i know rand() is not perfect function to use, but since this was not meant to be real lottery machine, i could not care less :)
So here's the lottery source. Compile with
gcc -Wall -o lotteryExe lotto.c lottoTest.c regularlotto.c vikinglotto.c
|All examples||No related posts|
|Explode function in C||ANSI C explode|
|Atomic Operations||(Finnish!) Atomiset Operaatiot (säikeet II)|
|Packed Array||C - optimize memory usage|
|Bitset in C||C - optimize memory usage|
|Trim/Rtrim (examples extended beyond post)||Trim/Rtrim|
|Linked list||No blog posts|
|Lottery machine||You can do OOP in C|