Overview of C++
In this lesson I will cover the following topics
C++ as a better C.
C++ as C++.
Design goals of C++.
Pros and cons of 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++ 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++.
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.
C++ should be (almost) compatible with C. The high degree of compatibility between C++ and C is no accident. C++ was designed from the beginning with the intention that any legal C program should compile as C++ with no change to the meaning of the program. Alas, this goal is somewhat inconsistent with C++'s other design goals. As a result, there are a few features of C that are not allowed in C++ programs. However, these features are few and far between; the incompatibilities were only introduced as a last resort. It is easy to write C programs that don't use the incompatible features. However, it is also true that most large C programs that were written without C++ in mind will need a bit of editing before being acceptable to a C++ compiler.
You shouldn't pay for features you don't use. In addition to accepting (most) valid C programs, C++ compilers should generate executables for such programs that are as fast as a C compiler could produce. In other words, the advanced features in the language should not penalize the performance of a program that chooses not to use them. As with the first design goal, this goal is not met 100% in C++. There are a couple of features that cause a (slight) reduction in performance just by existing in the language.
C++ should be suitable for high-performance programming and low-level programming. Even when you do use the advanced features of C++, the execution and space performance of your program should still be good. Like C, C++ trades other features (like safety) for speed. This makes C++ suitable for use in demanding applications that require the highest performance possible. Also, like C, C++ can be used for low-level hardware control applications. These design goals from C are also goals for C++.
C++ should not require you to use a particular programming style. As I mentioned before, it is possible to write object-oriented programs in C++, but it is not necessary to do so. In cases where the object-oriented paradigm is not the most natural approach, you can simply not use that paradigm. Some programming languages (and some programmers) are almost fanatical about the "one true way" of programming. Of course, everyone has a different point of view about what that one true way might be. C++ is intended to be less demanding in this regard. It gives you the tools you need to use a variety of programming techniques and lets you decide on how to use those tools.
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++.
C++ is approximately a superset of C, and thus most C programs will compile without changes as C++ programs.
C++ provides many new features relative to C, and it enables many additional programming techniques.
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.
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.