Use the Object object to implement your own objects.
To do so, you need to create 2 files.
- One with
.hextension. - One with
.cextension.
Use the following template to implement your object.
For example, you wish to define a new object with the "class name" of Circle:
-
Create a new file named
Circle.hwith the following content:#ifndef CIRCLE_H #define CIRCLE_H #include "oop/oop.h" DEFINE_CLASS_H(Circle) Circle *CircleConstructor(); #endif /* CIRCLE_H */
-
Create a new file named
Circle.cwith the following content:#include "Circle.h" Circle *CircleConstructor() { Object *instance = ObjectConstructor("Circle"); instance->addIntegerRValueMember(instance, PUBLIC, FIELD, "name", (IntegerRValue) "smallCircle"); printf("%s\n", (const char *) instance->getIntegerRValueMember( instance, PUBLIC, FIELD, "name")); instance->addDoubleRValueMember(instance, PUBLIC, FIELD, "radius", 2.34); printf("%f\n", (double) instance->getDoubleRValueMember(instance, PUBLIC, FIELD, "radius")); return (Circle *) instance; }
-
Create a file named
main.c. -
Use
Circleinmain.c.#include "oop/oop.h" #include "Circle.h" int main() { Circle *circle = CircleConstructor(); /* * `circle` is automatically freed, thus invoking `DESTRUCT_OBJECT` is not * required, but optional in case you want to destruct the object manually. */ /* DESTRUCT_OBJECT(circle); */ return 0; }