Procedural vs Object-Oriented Programming (OOP)

iSearchNotes is a FREE service that allows College Students to
Search and Share Class Notes.

Vote Up
Vote Down
By Professor: Phyllis Tedford
January 28, 2010 | Subject: Computer Science
Save Notes for Word

The Following pages are a discussion of classes that was developed by Professor Phyllis Tedford who has been teaching COSC 1436 Intermediate C++ and COSC 2437 Data Structures at Texas A&M Corpus Christi for the last several years. It is based on chapter 13 of Gaddis's Book.

 What is procedural programming?

  1. Back in the good old days, after the shift from machine language to higher-level languages, there existed spaghetti code. This was not good because spaghetti code was difficult to modify an nearly impossible to maintain.
  2. Along came procedural programming, sometimes called structured programming.
  3. The basic idea of procedural programming is to break up a long, complicated program into smaller parts or procedures. In c and C++, we call them functions.
  4. These functions take some sort of action on the data stored in the various identifiers (e.g., variables, constants, structures, etc) within the program by passing the data around as parameters.
  5. The primary emphasis in procedural programming is placed on the functions within the program and the operations these functions perform.
  6. This approach to programming has, over the years, created a few programs given the level of complexity to which software has risen:
    1. Programs written under the procedural approach can become very complex and convoluted. It would be easy for a real-world application of moderate size to contain hundreds if not thousands, of functions that communicate with one another in a multitude of ways. Would you want to be the new kid of the block who has to try and figure out the flow of such a program?
    2. As programs grow in size, the temptation arises to start using more and more global variable. This can introduce sometimes-devastating results, particularly in a large program worked on by multiple programmers. Remember: Global variables are bad!
    3. A Very important part of producing software involves the maintenance of that software. Programs written under the procedural approach can be, at best, difficult to maintain and modify. If multiple functions are communicating with one another, a seemingly minor change in one can cause unintended changes in one of more of the others

What is Object Oriented Programming (OOP) and why is it better?

  1. Consider for a moment a cell phone:
    1. When you look at a cell phone, you will see it has certain properties of attributes.
    2. for example, it has buttons. Some have your basic "phone" keys (numbers, #, ect.). The fancier ones have complete keyboards.
    3. Most have some sort of screen. Again, these range from basic LCD text screens to fancy touch screens.
    4. Cell phones can also perform tasks. That is they have behaviors or methods that allow them to take some action(s) using the attributes.
    5. For example, you can make and receive phone calls. On some, you can take and send pictures, ect.
    6. So, a cell phone, as an object, is a collection of both its attributes and its methods.
  2. This is the approach taken in OOP.
    1. OOP Focuses on the object, which is a collection of the object's attributes and methods bound together as a single unit.
    2. When we want an object to take some action of perform a task, we send the object a message with a request.
      1. Since the object is aware of its own current state (e.g. the values of attributes), it can perform the task or action requested and send the results back.
      2. In c++, this is done by making a function call to one of the methods offered by the object.
      3. The object can manage its own attributes without "sharing" that knowledge with the outside world.
    3. Why is this approach better then procedural programming?
      1. There are several very good reasons for writing programs using the OOP approach
      2. One reason is the re-usability of code.
        1. Think about the computer you use. Chances are, there are a variety of icons on the desktop.
        2. If you were writing the program that would implement the user interface for the operating system, would you want to have to rewrite the same code over and over to accomplish essentially the same task?
        3. Why reinvent the wheel? More and more tasks are very similar, if not downright repetitive. Why not reuse what code already exists?
        4. This translates directly into savings of both time and money during application development.
      3. Another reason is the encapsulation mentioned above.
        1. It is because the attributes and methods are packed together as a unit that a programmer can reuse the same code over and over.
        2. Again, this translates into savings of both time and money since there's less duplication of effort for repetitive tasks.
      4. A third reason is information hiding.
        1. Since the attributes and methods are packaged together as a unit the "how" of the object is hidden from the user. That is, the user does not need to know exactly how the tasks are performed or wht the specifics of the atrtributes are. In fact the user is not able to change anything (attribute of method) inside the object.
        2. This is more important because more that one programmer may be using an object or class in a language. If programmer A changes something on a whim, it might completely ruin what Programmer B needs the object to do.
        3. Languages are not developed and then left unchanged. There are always modification and improvements being made (even in something as old as COBOL).
        4. Since the "how" of an object is hidden, the programmer who maintains the object can tweak the insides of the object without having and discernible effect on how the object behaves. As a result, any code written using an earlier version of the object will still run. Java from Sun Microsystems is an example of this.
      5. A fourth reason is that a programmer can take advantage of inheritance.
        1.  Languages such as C++ and Java allow a programmer to take an existing object or class and add his/her own "bells and whistles" by using inheritance.
        2. For example suppose there is a class called Shape. This class comes with some basic attributes and methods. A programmer comes along and needs a specific class called triangle. By using the inheritance feature, the programmer can extend the class called by shape, using the existing methods and attributes and add methods and attributes specific to a triable.
        3. Again, this saves time and money if existing objects van be used as a basis for new objects that might be more application specific.

Enjoyed These Notes?
Your support will help spread the extreme awesomeness that is iSearchNotes!


You must login to comment. on Facebook

Login - Register