Designing Reusable Code  «Prev 

C++ Course Coding Standards

Is there an industry coding standard for C++?
there are several coding standards that C++ developers can adopt to maintain code consistency, enhance readability, and improve maintainability. There's no singular "industry standard" as different organizations often have their own internal standards or may use those developed by prominent entities in the field. Some of these well-recognized standards include:
  1. C++ Core Guidelines: This is an open-source project led by Bjarne Stroustrup and Herb Sutter, two prominent figures in the C++ community. The C++ Core Guidelines provide a comprehensive set of rules, recommendations, and best practices for writing modern, safe C++. It covers many aspects of coding, from naming conventions and layout to performance and concurrency. This is arguably the most authoritative and extensive C++ coding standard.
  2. MISRA C++: MISRA (Motor Industry Software Reliability Association) published MISRA C++, a coding standard widely used in the automotive industry and other safety-critical sectors. MISRA C++ is focused on preventing errors by banning or restricting use of certain language features known to be error-prone.
  3. Google C++ Style Guide: Google has developed a comprehensive style guide for C++ coding within the company. It includes sections on naming conventions, file organization, class design, function overloading, and much more. The guide is known for its clear and straightforward rules. However, some of its guidelines can be controversial and are not universally accepted in the broader C++ community.
  4. High Integrity C++ (HIC++): This is a coding standard developed by Programming Research Ltd (PRQA). It's focused on achieving high reliability and maintainability, with an emphasis on safety-critical systems. The standard covers a wide range of topics, including resource management, class design, and exception safety.
  5. CERT C++ Secure Coding Standard: Developed by the CERT Division of the Software Engineering Institute (SEI), this standard provides rules and recommendations for avoiding security vulnerabilities in C++ code. It covers topics such as input validation, memory management, concurrency, and error handling.

It's important to note that these standards should be seen as guidelines and not absolute rules. The best standard to use depends on the context, including the nature of the project, the expertise of the team, and any constraints of the working environment. Organizations often start with one of these standards and then adapt it to their own needs, creating an internal coding standard that all developers follow. Finally, automated tools, known as static code analyzers, can help enforce coding standards. These tools can automatically flag deviations from the chosen standard, improving code quality and consistency.
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
C++ Compilation
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.