OOP TERMS


Objects are created from classes. The class acts as a blueprint for the object, defining how it will behave within your program. You can use a class to create many objects, just as you can use a blueprint of a car or building to create many cars or buildings. It is important, however, to understand that just as a blueprint is not what is created from it, a class is not the same as an object. Rather, in OOP terminology, objects are instances of classes that are created at runtime.

Hint 

A class is a user-created data type that can instantiate objects.

Visual C++ provides access to a large collection of classes. For example, the code that drives the behavior of a Windows Forms application is largely based on the Form class, which is located in the System::Windows::Forms namespace. Examples of other classes include System::Windows::Forms::Button, which defines the Button class and System::Windows::Forms::MessageBox, where the MessageBox class is defined.

Hint 

A namespace is a kind of container that can store classes, program functions, and variables. The .NET Framework provides access to multiple namespaces, which it makes available in its class library.

Visual C++ also allows you to create your own custom classes, which is the focus of the bulk of this chapter. However, before you can start doing so, you need to know some basic terminology. The core concepts of OOP are known as abstraction, encapsulation, polymorphism, and inheritance.

Abstraction

In general terms, an abstraction is essentially a representation of something concrete, such as a real-world object like a car or house. Abstractions summarize the important details of what they represent so that you can understand and exploit similarities without getting bogged down in details. When you use the word car or house, for instance, you naturally understand that cars and houses have many different details, such as shape, size, and color, but that car or house represents the essentials of something.

In OOP terms, abstraction is used to model in program code certain essential behavior and information. Abstraction takes the form of the logical representation of that behavior and information as a class. This includes the specification of the base, or core functionality of the class, in the form of its methods and events. It also includes its most essential data, in the form of its properties.

Encapsulation

Encapsulation is the process whereby you decide how a class's data and functions will be grouped together and what elements of the class will be hidden or revealed. You make available to the programmer who will use your class only the information and data necessary for controlling the object he will use the class to create. You do this by designing an interface for the class that makes only certain properties, methods, and events usable and visible to the class's user.

When you encapsulate the internals of a class, you relieve the class's user of having to keep track of how the class works. The programmer doesn't have to worry about how code within the class functions or what data it relies on. This simplicity allows a programmer to focus on using the class where needed. It also cuts down on the overall task of managing complexity within their code.

Polymorphism

Polymorphism is the ability to create something in different forms. It is achieved in Visual C++ through a programming technique known as overloading. Using overloading, a programmer might develop multiple versions of a function, each with the same name, all designed to meet different needs. The varieties of the method or function would be distinct from one another because each would accept different combinations of arguments.

Hint 

Overloading is the process of defining the same function multiple times, each with a different argument list.

A good example of polymorphism and overloading in action is the MessageBox class's Show method. This method supports 21 different formats, each useful in specific instances. Each of these overloads allows the Show() method to accept various combinations of arguments and data types.

Trick 

To view the many available combinations of arguments and data that you can pass to the MessageBox::Show method, open the Visual C++ Code Editor and type MessageBox::Show. IntelliSense activates and displays a pop-up window showing the various forms of this method. Click on the up and down arrows in the upper-left corner of the pop-up window to explore the available options.

Inheritance

Inheritance is the process whereby one class is derived from another. This allows a programmer to base one class on another, in effect using the features of the base class without having to do extra work. You can see an example of inheritance each time you create a Windows Forms application. The application you create is derived from System::Windows::Forms::Form Each time you build an application, you effectively extend the original work done by the .NET team in new and different directions.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

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