[an error occurred while processing this directive]

Implementing an Object in C


When you need only one object, you can implement it in C using file-level encapsulation. Data variables for the object are declared in a .C file along with the functions that manipulate the data. If the variables are declared outside of the functions then they can be accessed by all of the functions in the .C file, but they cannot be accessed from C code outside the file.

The function parameter and returned value types must be declared for use in client code. This is done for all clients by putting declarations (no function body) for the functions into a .h file. Usually, this file has the same name as the .C file except for the suffix. All clients incorporate the declarations by using a #include for the .h file. The .C file for the object also uses a #include for the .h file to ensure that the parameters and returned values are consistent between clients and the implementation.

Code Listings

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

[an error occurred while processing this directive]