OOPortal OOPortal

Designing Reusable Code  «Prev 

C++ Course Coding Standards

Because many older compilers do not understand some of the lastest ANSI C++ additions to the language, the code in this course is specifically written to compile on both new and old compilers. For this reason, we will continue to use the .h suffix for libraries named in #include statements. In addition, we will not use namespaces, since only the most recent compilers support them.
The C and C++ programming languages are closely related. C++ grew out of C and is mostly a superset of the C Programming Language.
C code is often developed with C++ IDEs, integrated with C++ code, and compiled in C++ compilers.
While most C source code will compile as C++ code without any changes, certain language differences prevent C++ from being a strict superset of C. C++ introduces many features that are not available in C. Hence, C++ code is not valid C code. Here, however, we focus on differences that cause valid C code to be invalid C++ code, or to be valid in both languages but to behave differently in C and C++.


Once all of the source code files associated with a program have been compiled by the compiler, the object code files need to be linked together along with the object code files that implement the functions declared in the standard library. This task is performed by a program known as the linker, which produces a file that can be loaded into memory by the operating system and executed. The relationship among the library, preprocessor, compiler, and linker is shown in Figure 1.5.

Functions, Classes, and Objects

In C++ the fundamental programming unit is the function. The class is also a programming unit. Every program is written as a collection of functions and classes. Functions may either be stand-alone or belong to a class. In C++, function and class
Figure 1.5 C++ Compilation

declarations are stored in include files, also known as headers, that have the extension .h.
Function and class definitions are stored in files with the extension .cpp. (Other conventions are sometimes used: include files may also use the extension .hpp or .H, and definition files may use the extension .cc or .C.)

C++ Class

A class is a named description for a group of entities called objects, or instances of the class, that all have the same kinds of information (known as attributes, data fields, or instance variables) and can participate in the same operations (functions). The attributes and the functions are members of the class and are not to be confused with the instances or objects in the class. The functions are referred to as member functions. If you are new to object-oriented design, you may be confused about the differences between a class, an object or instance, and a member. A class is a general description of a group of entities that all have the same characteristics which means, they can all perform or undergo the same kinds of actions, and the same pieces of information are meaningful for all of them. The individual entities are the objects or instances, whereas the characteristics are the members. For example, the class House would describe a collection of entities that each have a number of bedrooms, a number of bathrooms, a kind of roof . They can all be built, remodeled, assessed for property tax. The house where you live and the house where your best friend lives can be represented by two objects, or instances, of class House. The numbers of bedrooms and bathrooms and the actions of building and assessing the house would be represented by members of the class House.