Building Classes in C++   «Prev  Next»
Lesson 5 C++ compilers
Objective How this course attempts to support the differences in C++ compilers.

Building Classes using C++ compilers

This course attempts to support the differences in C++ compilers. The code in this course is written in ANSI C++ and should compile without errors on many ANSI-compliant compilers. ANSI C++, however, is a rapidly evolving standard and many older compilers are not able to use the latest additions in the language. For this reason, the code used in this course was written to run on as wide a variety of compilers as possible. The code has been tested using GNU gcc/g++ (Unix), Microsoft Visual C++ (Windows). If you are using another compiler, though, you may need to modify the code slightly to get it to compile. You may have to rely on the compiler vendor's support if compiling errors occur. Key differences might exist in various compiler flags and library files. Keep this in mind when working through this course.

C++ Coding Standards

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++.

The C++ Crash Course

Link Object Code Files using Linker

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.
  • Most widely used Programming Languages:
    C++ is one of the most widely used programming languages in the world and well-written C++ programs are fast and efficient. The language is more flexible than other languages because you can use it to create a wide range of apps. From fun and exciting games, to high-performance scientific software, to AI programs that need to discover underwater mines. For more than 20 years, C++ has been used to solve many problems. What you might not know is that an increasing number of C++ programmers have folded up the traditional C-style programming of yesterday and instead have begun to use modern C++. One of the original requirements for C++ was backward compatibility with the C language. Since then, C++ has evolved through several iterations:
    1. C with Classes,
    2. then the original C++ language specification,
    3. and then the many subsequent enhancements.

    Because of this heritage, C++ is often referred to as a multi-paradigm programming language. In C++, you can do purely procedural C-style programming that involves raw pointers, arrays, null-terminated character strings, custom data structures, and other features that may enable great performance but can also spawn bugs and complexity. Because C-style programming is fraught with perils like these, one of the founding goals for C++ was to make programs both type-safe and easier to write, extend, and maintain. Early on, C++ embraced programming paradigms such as object-oriented programming. Over the years, features have been added to the language, together with highly-tested standard libraries of data structures and algorithms. It's these additions that have made the modern C++ style possible.

SEMrush Software