[an error occurred while processing this directive]

Implementing a Class in C

The basic idea of implementing a class in C is to group the data for a C object into structs so that you can have any number of objects. The struct is declared in the .h file so that it is shared between the class and its clients. In addition, you usually need a function that initializes the objects in the class. For the stack class, this function is called create().

The code in this class is not strict C code. All functions that modify the stack have a reference parameter to reference the stack. Reference parameters are not supported in C, but they are supported in C++. The examples will compile correctly and work correctly if compiled with a C++ compiler or a combined C and C++ compiler such as g++.

Code Listings

Although this example contains working code, it is only intended as a simplified example of a C class. Comments and overflow and underflow checks are omitted. There also is no function for determining if a stack is empty.

[an error occurred while processing this directive]