Lesson #1

Overview of C++

Overview

In this lesson I will cover the following topics

  1. C++ as a better C.

  2. C++ as C++.

  3. Design goals of C++.

  4. Pros and cons of C++.

Body

C++ as a better C

Welcome to my tutorial on C++. This tutorial assumes that you are already familiar with C and builds on concepts from C that you should already know. Be aware that teaching C++ as an extension of C is controversial. Some C++ instructors feel it is better to learn C++ on its own terms without the student having to unlearn "bad habits" acquired from C. This is a reasonable view; C++ provides better ways than C of solving certain problems. It also requires a somewhat different way of thinking.

On the other hand, one of the strengths of C++ is that it is highly compatible with C. Many C programs (especially carefully written C programs) will compile as C++ programs with no changes. This allows you to start using a C++ compiler on an existing C program and then introduce C++ features gradually as necessary. In this way you can take advantage of C++ without having to rewrite your entire application.

Also there are situations when C is available, but C++ is not. To work effectively in those cases, you should understand the difference between C and C++. Learning C first and then learning C++ from that background will help keep the two languages distinct in your mind. C and C++ are different languages. Despite their close relationship, they have different goals, different pasts, and probably different futures. It is useful to keep them separate in your thinking.

C++ can be used as a "better C" without delving into the complexities of its more advanced features. For example, C++ has a stronger type system than C, meaning that it will catch more program errors at compile time than a C compiler could. C++ also has a number of convenience features that you can use to make what would otherwise be an ordinary C program a little easier to write. Such features include:

I will discuss all of these features in this tutorial as well as others; don't worry if you don't understand what they mean right now.

C++ as C++

C++ also has several important features that allow it to be used in ways that are awkward (at best) for C to emulate. These features allow C++ to be used as an object-oriented language, as well as also supporting some other important programming paradigms such as generic programming. Unlike some object-oriented languages, you can program in C++ without using object-oriented techniques. For example, you can write C programs. C++ does not require you to use any particularly programming style, but rather lets you apply one of several different approaches that best fits the needs of your problem.

Some of these more advanced features include:

I will discuss the first four features above in this tutorial. I will also say a few words about generic programming as a way of introduction. However, many aspects of these more advanced topics are outside the scope of this tutorial. C++ is large and complex. No single tutorial, course, or even text book can do justice to the entire language. However, after working through this tutorial, you should have a good understanding of how C++ supports abstract types and object-oriented programming. You should also be in a good position to read and understand some of the more advanced material on C++.

Design goals of C++

Like all programming languages, C++ was designed with certain goals in mind. It is useful to understand these design goals because doing so allows you to put the features of C++ into their proper perspective. Programming language features that seem odd or pointless sometimes make sense when you understand the overall philosophy that language is following.

Pros and cons of C++

I've already discussed some advantages of C++: its good performance, its compatibility with C, and its flexibility. One advantage I haven't mentioned is its expressivity. The advanced features of C++ can be used by themselves or together to implement complex and powerful ideas in elegant ways. In some respects, this is the most exciting aspect of the language. It gives you a way to express the solution to your programming problem in a very abstract and general manner.

However, no overview of C++ would be complete without also talking about the language's disadvantages. Probably the biggest such disadvantage is the language's complexity. Because it is so complex, C++ is hard to learn and frequently surprises even experts. The complexity also has other, indirect negative consequences. C++ compilers tend to be large, slow, and buggy (as compared to C compilers), and advanced programming tools are either non-existent or expensive (because they are difficult to write). C++ can also be hard to read because it has a tricky syntax. Finally, many C++ features interact in complicated ways that are hard to understand.

Some of the complexity of C++ is a direct consequence of its advanced features. Any language with the expressive power of C++ is going to be complex. However, C++ is more complex than necessary. This is largely due to historical reasons. C++ is not a new language, and it has accumulated baggage over the years. New versions of the language must remain compatible with legacy code, and so old, bad features end up remaining in the language even as new and better ways of doing things are introduced. In addition, C++'s dedication to C compatibility makes C++ more complicated than would otherwise be the case. C has its own set of quirks. Taken alone, C's quirks are not a major problem, but after adding on the new features of C++, those quirks become magnified into significant issues. Thus, C compatibility is both an advantage and disadvantage of C++.

Summary

  1. C++ is approximately a superset of C, and thus most C programs will compile without changes as C++ programs.

  2. C++ provides many new features relative to C, and it enables many additional programming techniques.

  3. C++ was intended to retain the efficiency of C in part by not requiring you to pay a performance penalty for advanced features you don't use. C++ was intended to support very large programming projects.

  4. C++ is very powerful and expressive. It allows the programmer to use many advanced programming techniques. However, C++ is also very complex and hard to master. It contains a number of warts and odd (mis)features related to its C compatibility and to its age.

© Copyright 2023 by Peter Chapin.
Last Revised: July 17, 2023