Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
All objects support three specific concepts: encapsulation, inheritance, and polymorphism. Think about the objects around you—no, scratch that, and think about yourself. You are an object: You are made up of arms, legs, a torso, and a head, but how they work does not matter to you—this is encapsulation. You are a mammal, human, and male or female—this is inheritance. When greeted, you respond with "Good day," "Bonjour," "Guten Tag," or "Buon giorno"—this is polymorphism.
As you shall see shortly, you can apply the object paradigm to software development as well. Managed C++ does it by using software objects called classes and structs. But before I get into software objects, let's examine the concepts of an object more generically.
Encapsulation
All objects are made up of a combination of different things or objects. Many of these things are not of any concern to the other objects that interact with them. Going back to you as an example of an object, you are made up of things such as blood, muscles, and bone, but most objects that interact with you don't care about that level of things. Most objects that interact with you only really care that you have hands, a mouth, ears, and other features at this level of abstraction.
Encapsulation basically means hiding the parts of an object that do things internal to that object from other objects that interact with it. As you saw in the previous example, the internal workings of hands, a mouth, and ears are irrelevant to other objects that interact with you.
Encapsulation simplifies the model that other objects have to interact with. It allows other objects to only have to worry about using the right interface and passing the correct input to get the required response. For example, a car is a very complex object. But to me, a car is simple: A steering wheel, an accelerator, and a brake represent the interface, and turning the steering wheel, stepping on the accelerator, and stepping on the brake represent input.
Encapsulation also allows an object to be fixed, updated, or replaced without having to change the other objects interacting with it. When I trade in my Mustang LX for a Mustang GT, I still only have to worry about turning the steering wheel, stepping on the accelerator, and stepping on the brake.
The most important thing about encapsulation is that because portions of the object are protected from external access, it is possible to maintain the internal integrity of the object. This is because it is possible to allow only indirect access, or no access at all, to private features of the object.
Inheritance
Inheritance is hardly a new concept. We all inherit many traits (good and bad) from both of our parents. We also inherit many traits from being a mammal, such as being born, being nursed, having four limbs, and so on. Being human, we inherit the traits of thumbs, upright stature, capacity for language, and so forth. I'm sure you get the idea. Other objects also inherit from other more generic objects.
You can think of inheritance as a tree of objects starting with the most generic traits and expanding to the most specific. Basically, each level of the tree expands upon the definition of the previous level, until finally the object is fully defined.
Inheritance allows for the reuse of previously defined objects. For example, when you say that a Mustang is a car, you know that it has four wheels and an engine. If you say a mustang is a horse, you know that it has four legs and eats grass. In both of these scenarios, the base object definition came for free—you didn't have to define it again.
Notice, though, that a Mustang is always a car (or a horse), but a car need not be a Mustang. The link of inheritance is one way, toward the root.
Polymorphism
The hardest concept to grasp is polymorphism; not that it's difficult, it's just taken so much for granted that it's almost completely overlooked. Basically, polymorphism is simply the ability for more than one object to respond to the same stimuli in completely different ways.
For example, if a teacher were to ask a classroom full of students to draw a picture of their favorite food, then most of the students would draw completely different things. With the same stimuli to the same type of objects, in this case students, you are getting different responses.
You might have noted from the previous example that some students may select the same food to draw. There is a good chance the reason for this comes from some form of inherited link of the students in question. Polymorphism is closely related to inheritance, and common responses can often be attributed to something that both objects inherit or have in common.
A key thing about polymorphism is that you know that you will get a response of a certain type, but the object responding, and not the object requesting, determines what the actual response will be.
Applying Objects to Software Development
Okay, you know what objects and their concepts are and how to apply them to software development. With procedural programming, there is no concept of an object, just a continual stream of logic and data. Let me back up a bit on that. It could be argued that even in procedural programming objects exist, as variables, literals, and constants could be considered objects (albeit simple ones). In procedural programming, breaking up the logic into smaller, more manageable pieces is done by way of functions. To group common data elements together, the structure or class is used depending on language.
Before you jump on me, I would like to note that there were (obviously) other object-oriented languages before C++, but this book only covers Managed C++'s history.
It wasn't until C++ that computer data and its associated logic was packaged together into the struct and a new construct known as the class. With the combination of data and logic associated with this data into a single construct, object-oriented concepts could be applied to programming.
Here in a nutshell is how objected-oriented concepts are applied to Managed C++ development. Classes and structures are programming constructs that implement within the C++ language the three key object-oriented concepts: encapsulation, inheritance, and polymorphism.
Encapsulation, or the hiding of complexity, is accomplished by not allowing access to all data and functionality found in a class. Instead, only a simpler and more restricted interface is provided to access the class.
Inheritance is the ability to reuse functionality and data of one class within another class, without having to worry about the complexity of the first. The class has the ability to derive itself from another class.
Polymorphism is the ability for different classes to respond to the same request in different ways. Classes provide something called the virtual method, or function, which allows any class derived from the same parent class to respond differently to the same request.
I expand on each of these concepts as the chapter progresses.
Now that you understand OOP conceptually, let's see how it is actually done with Managed C++.
Категории