|
|
|
|
|
|
|
C++ and Object-Oriented Programming |
|
|
|
|
|
|
|
|
C++ fully supports object-oriented programming, including the four pillars of object-oriented development: encapsulation, data hiding, inheritance, and polymorphism. |
|
|
|
|
|
|
|
|
Encapsulation and Data Hiding |
|
|
|
|
|
|
|
|
When an engineer is creating a new device, he wires together component pieces. He can wire in a resistor, a capacitor, and a transistor. The transistor has certain properties and can accomplish certain behaviors. He can use the transistor without understanding the details of how it works, as long as he knows what it does. |
|
|
|
|
|
|
|
|
New Term: To achieve this, the transistor must be self-contained. It must do one well-defined thing, and it must do it completely. Doing one thing completely is called encapsulation. |
|
|
|
|
|
|
|
|
All the properties of the transistor are encapsulated in the transistor object; they are not spread out through the circuitry. It is not necessary to understand how the transistor works in order to use it effectively. |
|
|
|
|
|
|
|
|
C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. Once created, a well-defined class acts as a fully encapsulated entity; it is used as a whole unit. The actual inner workings of the class should be hidden; users of a well-defined class do not need to know how the class works, they just need to know how to use it. You'll see how to create classes in Hour 6, Basic Classes. |
|
|
|
|
|
|
|
|
In the late 1980s, I worked for Citibank building a device for home banking. We didn't want to start from scratch; we wanted to get something out into the market quickly. Therefore, we started with the telephone and enhanced it. Our new enhanced telephone was a kind of telephone; it just had added features. Thus, I was able to reuse all the calling features of a plain old telephone but add new capabilities to extend its utility. |
|
|
|
|
|
|
|
|
New Term: C++ supports the idea of reuse through inheritance. A new type can be declared that is an extension of an existing type. This new subclass is said to derive from the existing type and is sometimes called a derived type. The enhanced telephone is derived from a plain old telephone and thus inherits all its qualities, but additional features can be added to it as needed. Inheritance and its application in C++ are discussed in Hour 16, Inheritance. |
|
|
|
|
|