# C Programming/GObject

Since the C Programming-Language was not created with Object Oriented Programming in mind, it has no explicit support for classes, inheritance, polymorphism and other OO Concepts. Neither does it have its own Virtual Table, which is found in object-oriented languages such C++, Java and C#. Therefore, it might not be as easy to implement an object-oriented programming paradigm using only C's language features and standard library. However, it can be done using structures which contain both function pointers as well as data, for example, or by using third-party libraries.
There are many third-party libraries designed to add support for object-oriented programming in C. The most general-purpose and widely used among these is the GObject System, which is part of Glib. The GObject System comes with its own virtual table. To create an object in C using the GObject system, it has to be sub-classed from the GObject struct.

## Object-Creation

In this example a new object will be implemented directly derived from GObject. For simplicity, the object is named MyObject.

### Declaring An Object

To create a simple non-derivable (final) object, two structs must be declared, the instance and the class. They are declared using a macro:
This declares two structures, MyObject and MyObjectClass. MyObject must be defined in the C implementation, and MyObjectClass is already defined by the macro.

### Boiler-Plate Code

Since the GObject System is just a third-party library and therefore cannot make any changes to the C Language itself, creating a new object requires a lot of boiler-plate code. This is mostly handled by the macro shown above. However, the following is also required:
The macro defines several functions, namely MY_OBJECT and MY_OBJECT_CLASS, used for casting, MY_IS_OBJECT and MY_IS_OBJECT_CLASS for testing whether an object or class is of the correct type and MY_OBJECT_GET_CLASS for getting the class structure from an instance.

### Defining The Object

Before use, the newly created object must be defined, along with the instance structure.

### Static Functions

There are a few static functions that may or may not to be defined, depending on your object. For a minimal object these ones are compulsory:

### The Constructor

There is no internal way of allocating memory for an object in C. Therefore an explicit constructor must be declared for the new object.

### Object-Usage

Although creating the object using its own pointer-type is perfectly valid, it is recommended to use the pointer-type of the object at the top of the hierarchy i.e the furthest off base class. The newly created object may now be used like this:

## Inheritance
### Concept

Inheritance is one of the most widely used and useful OO Concepts. It provides an efficient way to reuse existing code by wrapping it up into an object and then sub-classing it. The new classes are known as derived classes. Many object hieriarchies can be created using inheritance. Inheritance is also one of the most efficient ways of abstracting code.

### Implementation

In the GObject System, inheritance can be achieved by sub-classing GObject. Since C provides no keyword or operator for inheritance, a derived object is usually made by declaring the base instance and base class as a member of the derived instance and derived class respectively. In C code:

## Further reading

* Hanser. "Object-oriented programming with ANSI-C". 1994. Hanser describes another way of implementing classes, inheritance, instances, methods, objects, vtables, polymorphism, late binding, etc. in standard ANSI C.
* Gregory Naçu - C64OS.com. "Object Orientation in 6502 (Take 2)". 2019. Greg Nacu describes another way of implementing classes, inheritance, instances, methods, objects, etc., using very little memory, in 6502 Assembly language.
* Greg Kroah-Hartman. "Everything you never wanted to know about kobjects, ksets, and ktypes". mirror: "Everything you never wanted to know about kobjects, ksets, and ktypes". 2007.