Anda di halaman 1dari 44

Object Oriented Programming with C++

Text Book
The Complete Reference C++, 4th edition

Brief history of computers-comparison What is object oriented programming? Why do we need it? What are the principles behind OOP?

The beginning-brief history of computers

Early in computer history, programmers worked with the most primitive computer instructions, known as machine language These instructions were represented by strings of 0s and 1s. Such instructions had to be loaded into memory through a set of switches or through a numeric keyboard, which was complex.

What next????
Assembly language was then invented to
improve the situation. It enabled programmers to use mnemonic names for the machine instructions and symbolic names for memory locations.

Buthow the computer understands mnemonics????

A translator called assembler converts assembly language programs into machine code.


They were readable and manageable by programmers very easily.

And what next ???

As programs continued to grow, high level languages were introduced. This gave the programmer more tools to handle complexity. Enabled programmers to work with instructions approximating words and sentences. Example: Let X = 100.

How these instructions gets translated?????

Interpreter compiler

The first widespread language was FORTRON Although FORTRON was a very impressive first step, it does not encourage clear, easy to understand programs. The 1960s gave birth to structured programming.

Structured programming
The long programs that the programmer found difficult to comprehend could now be broken down into smaller units of few hundred statements. A structured program is built by breaking down the programs primary purpose into smaller pieces that then become functions within the program. This made it possible to write moderately complex programs fairly easily.

Although structured languages are powerful tool, they reach their limit when a project becomes too large. In a structured program, datatypes are processed in many functions, and when changes occur in datatypes, modifications must be made at every location that acts on those data types within the program This is a frustrating and time consuming task for large sized programs.

Another problem with structured programming is that its primary components-functions and data structures-dont model the real world very well. For example, suppose you are writing a program to create the elements to a GUI: menus, windows, etc., there are no obvious program elements to which a menu or window would correspond. To build a house, grow a tomato first you think about the object and its purpose and behavior. Continued....

Then you select your tools and procedures. The world is therefore, object oriented. Here, problem solvers deal with their problem domains by concentrating on the objects and letting the characteristics of those objects determine the procedures to apply to them. OOP took the best ideas of structured programming and combined them with several new concepts.

In the most general sense, a program can be organized in one of two ways:
Around its code(what is happening) or Around its data(who is being affected) Using only structured programming techniques, programs are typically organized around code. This approach can be thought of as code acting on data. For example, a program written in a structured language such as C is defined by its functions, any of which may operate on any type of data used by the program.

Object oriented programs work the other way around. They are organised around data, with the key principle being data controlling access to code. In an object oriented language, you define the
data and The routines that are permitted to act on that data. To support the principles of object oriented programming, all OOP languages have 3 traits in common: encapsulation, polymorphism and inheritance.

Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. When code and data are linked together in this fashion, an object is created. In other words, an object is the device that supports encapsulation.

Within an object, code , data, or both may be private to that object or public. Private code or data is known to and accessible only by another part of the object. That is private code or data may not be accessed by a piece of the program that exists outside the object. When code or data is public, other parts of your program may access it even though it is defined within an object.

For all intents and purposes, an object is a variable of a user defined type.

Abstraction means to show only the necessary details to the client of the object.

Do you know the inner details of the Monitor of your PC?

What happen when you switch ON Monitor? Does this matter to you what is happening inside the Monitor? No Right, Important thing for you is weather Monitor is ON or NOT.

When you change the gear of your vehicle are you really concern about the inner details of your vehicle engine? No but what matter to you is that Gear must get changed thats it!! This is abstraction; show only the details which matter to the user.

OOP supports polymorphism Which is characterized by one interface , multiple methods. For example, consider a program that defines three different types of stacks.
One stack is used for integer values One for character values One for floating-point values.

Example continued..

Polymorphism allows us to define one set of names, push() and pop(), that can be used for all 3 stacks.

In your program you will create 3 specific versions of these functions, one for each type of stack, but names of the functions will be the same. The compiler will automatically select the right function based upon the data being stored.

Thus, the interface to a stack-the functions push() and pop() are the same no matter which type of stack is being used.

The individual versions of these functions define the specific implementations(methods) for each type of data. Polymorphism helps reduce complexity by allowing the same interface to be used to access a general class of actions.

Its the compilers job to select the specific action (i.e method) as it applies to each situation. The programmer need not to do this selection manually.

The first Object Oriented Programming languages were interpreters, so polymorphism was, of course, supported at runtime. However, C++ is a compiled language. Therefore in C++, both runtime and compiletime polymorphism are supported.

It is a process by which one object can aquire the properties of another object. i.e it supports the concept of classification For example, a Red Delicious apple is part of the classification apple Which inturn is part of the fruit class Which is under the larger class food.
Without the use of classifications, each object would have to define explicitly all of its characteristics.

A Real-World Example
Let's say that you are working for a vehicle parts manufacturer that needs to update it's online inventory system. Your boss tells you to program two similar but separate forms for a website, one form that processes information about cars and one that does the same for trucks.

For cars, we will need to record the following information: Color Engine Size Transmission Type Number of doors

For trucks, the information will be similar, but slightly different. We need: Color Engine Size Transmission Type Cab Size Towing Capacity

In procedural programming, you would write the code first to process the car form and then the code for the truck form.

With object-oriented programming, you would write a base class called vehicle that would record the common characteristics what we need from both trucks and cars. In this case, the vehicle class will record: Color Engine Size Transmission Type

We'll make each one of those characteristics into a separate method. The color method, for example, could take the color of the vehicle as a parameter and do something with it, like storing it in a database.

Next, we will create two more classes: truck and car, both of which will inherit all of the methods of the vehicle class and extend it with methods that are unique to them.

The car class will have a method called numberOfDoors and the truck class will have the methods cabSize and towingCapacity.


Scenario 1
Suppose that we suddenly need to add a bus form, that records the following information: Color Engine Size Transmission Type Number of passengers Procedural: We need to recreate the entire form, repeating the code for Color, Engine Size, and Transmission Type. OOP: We simply extend the vehicle class with a bus class and add the method, numberOfPassengers.

Scenario 2
Instead of storing color in a database like we previously did, for some strange reason our client wants the color emailed to him. Procedural: We change three different forms: cars, trucks, and buses to email the color to the client rather than storing it in the database. OOP: We change the color method in the vehicle class and because the car, truck, and bus classes all extend (or inherit from, to put it another way) the vehicle class, they are automatically updated.

Scenario 3
We want to move from a generic car to specific makes, for example: Nissan and Mazda. Procedural: We create a new form for each make, repeating all of the code for generic car information and adding the code specific to each make. OOP: We extend the car class with a nissan class and a mazda class and add methods for each set of unique information for that car make.

Wrapping It Up
As you can see from the above scenarios, employing an OOP style has significant advantages over procedural programming, especially as your scale increases.

Consider the savings we would receive from OOP in terms of

repeated code, flexibility, and maintenance if we also had to add forms for boats, motorcycles, planes, go-karts, ATVs, snowmobiles, etc.

Objects and methods are also far easier to test than procedural programming by using unit testing to test results.