What Is C++?C++ is simply C upgraded with object-oriented (OO) technology. Really, it's nothing more than a superset of C. C++ has the following major upgrades:
Let's take a quick look at each of these features. Classes are simply a way of combining data and functions. Normally, when you program in C, you have data structures that hold data and functions that operate on the data, as shown in part A of Figure D.1. However, with C++, both the data and the functions to operate on the data are contained within a class, as shown in part B of Figure D.1. Why is this good? Well, you can think of a class as an object that has properties and that can perform actions. It's just a more abstract way of thinking. Figure D.1. The structure of a class.The next cool thing about C++ is inheritance. Once you create classes, they give you the abstract ability to create relationships between class objects and base one object or class upon another. It's done all the time in real life, so why not in software? For example, you might have a class called person that contains data about the person and maybe some class methods to operate on the data (don't worry about that for now). The point is, a person is fairly generic. But the power of inheritance comes into play when you want to create two different types of people—a software engineer and a hardware engineer, for example. Let's call them sengineer and hengineer. Figure D.2 shows the relationship between person, sengineer, and hengineer. See how the two new classes are based on person? Both sengineer and hengineer are persons, but with extra data. Thus, you inherit the properties of a person but add new ones to create sengineer and hengineer. This is the basis of inheritance. You build up more complex objects from preexisting ones. In addition, there is multiple inheritance, which enables you to build a new object as a set of subclasses. Figure D.2. Class inheritance.The third and last big deal about C++ and OO programming is polymorphism, meaning "many forms." In the context of C++, polymorphism means that functions or operators are different things depending on the situation. For example, you know that the expression (a + b) in straight C means to add a and b together. You also know that a and b must be built in types such as int, float, char, and short. In C, you can't define a new type and then say (a + b). In C++, you can! Therefore, you can overload operators like +, -, *, /, [], and so on and make them do different things depending on the data. Furthermore, you can overload functions. For example, let's say you write a function Compute() like this: int Compute (float x, float y) { // code } The function takes two floats, but if you send it integers, they're simply converted to floats and then passed to the function. Hence, you lose data. However, in C++ you can do this: int Compute (float x, float y) { // code } int Compute (int x, int y) { // code } Even though the functions have the same names, they take different types. The compiler thinks they're completely different functions, so a call with integers calls the second function, whereas a call with floats calls the first. If you call the function with a float and an integer, things get more complex. Promotion rules come into play, and the compiler decides which one to call using those rules. That's really all there is to C++. Of course, there's some added syntax and a lot of rules about all this stuff, but for the most part all of it has to do with implementing these three new concepts. Pretty easy explanation of what will become a very complicated concept, huh? |