Object Oriented Design for C

Introducing the OO Paradigm for Non-C++ Programmers

© Guy Lecky-Thompson

Tutorial article for C and C++ programmers describing the OO design paradigm, and how it fits into C based languages.

Introduction

While it is somewhat of an over-simplification to state that C++ is just C with classes, this is often the best approach to learning how object oriented programming with C++ can be used in applications. C++ adds much more to the C language, and is as a different language in its own right as, for example, Java. Both Java and C++ borrow heavily on the C language for:

However, while C programs can be compiled with a C++ compiler, the same is not true for Java. So, although both Java and C++ are object oriented programming languages based on C, since a C program can be compiled with a C++ compiler, many programmers see C as a subset of C++.

This is not helped by a school of thought that states that anything that can be done in C++, can be done in C, just not with the same elegance or ease. In fact, some C compilers come with a C++ pre-processor that can translate, or recreate, C code based on the C++. It is important to remember that C and C++ are different languages, and that mixing them is not a good idea. For learning puropses, however, it makes sense to use C as a basis.

OO Design

Object Oriented Design breaks down the problem domain into a series of interacting objects. The exact granularity of the object universe depends on the requirements of the design. For example, a bicycle is a collection of parts - frame, wheels, handlebars, breaks, gears, etc. - but it might not make sense to create a design where a bicycle really is a colelction of smaller parts. On the other hand, if the bicycle was not just another object, but the only type of object in the universe, it probably would be a good idea to model it as a collection of parts.

Deciding the level at which the model is going to be situated will have a profound effect on the complexity of the resulting code, so it is important to get right. Each object will have to be defined in terms of its internal state, and interface with the rest of the design universe, so having a high level of granularity may cause unncessesary headaches.

OO Programming

It is perfectly possible to implement OO designs using a traditionally non-object oriented labguage like C. Arguably, it is not so much about the language as it is about the spirit in which the implementation is performed. Any language that has support for multiple code files, prototyping, and user defined data types, ideally with pointer support, can be used in an object oriented fashion. However, the addition of some key programming support is helpful. Such support can be in the form of:

So, for C programmers, the best route is either to look at C++ as C with classes, and ignore the vast complexities of the full C++ implementation, or to face up to the fact that it is necessary to almost learn a whole new language from scratch. Depending on individual circumstances, either approach might be equally valid; but be wary of claiming to know C++ whilst being, deep down, a C programmer at heart.

Links

Understanding C++ Classes Tutorial

Getting Started in C++ Programming


The copyright of the article Object Oriented Design for C in Computer Programming Languages is owned by Guy Lecky-Thompson. Permission to republish Object Oriented Design for C must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo