Chapter 10: Classes


Download CD Content

The entire purpose of C++ is to add object orientation to the C programming language. The first 9 chapters have illustrated simple, non-object-oriented C++. You will still use all these techniques in the rest of this book, but now object-oriented programming will be added. This chapter will introduce you to the concepts of object-oriented theory as well as to the basic unit of object-oriented programming: classes.

Object-Oriented Theory

The first question we should address is: What is object-oriented programming? The answer is simple: It is programming that is meant to more accurately represent objects in the real world, by using objects in code. Object-oriented programming is simply about accurately modeling the real world in your programs. It’s also about code reusability. The heart of object-oriented programming is to create an object, in code, that has certain properties and methods. This is not unlike the real world. You car is an object. It has certain properties such as color, number of doors, and the like. It also has certain methods such as accelerate, brake, and so on. You use that object by executing one or more of its methods. An object in programming is exactly the same. It has certain properties that describe it, and certain methods you can use to do certain things with it.

There are four principle concepts that form the foundation of object-oriented programming. These concepts are abstraction, encapsulation, inheritance, and polymorphism.

Abstraction

Abstraction means you can deal with things in a general sort of way, and only get as specific as you need to at a given point in time. For example, we could discuss the general characteristics of an automobile without dealing with a specific make and model. Another, and more formal, way to define abstraction is that it is the act of specifying a general interface, hiding implementation details.

Encapsulation

Encapsulation is placing the data and the functions that work on that data in the same place. In procedural programming, like what you have seen in the first 8 chapters of this book, it is not always clear which functions work on which variables. The more complex the program, the more unclear this relationship becomes. With object-oriented programming you place the data and the relevant functions together in the same object. Thus, which functions work on which data is abundantly clear.

Inheritance

One of the most useful aspects of object-oriented programming is code reusability. One object can inherit the public functions and data from another object.

Polymorphism

Once you have inherited a function from another object, you can manipulate it and change it. Thus, one function can take many (poly) forms (morph) (i.e., polymorphism).

You should probably try to commit these four principle concepts and their explanations to memory. At this point, you are probably still a bit fuzzy on exact definitions of these terms. Don’t let that worry you too much. Most people have a little difficulty when they first encounter object-oriented programming. Only through continued use and reexplanation of the principles involved, will you become comfortable with these concepts. In this chapter and the next few chapters, each of these concepts will be explored in more detail.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net