iSearchNotes is a FREE service that allows College Students to
Search and Share Class Notes.
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?
- 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.
- Along came procedural programming, sometimes called structured programming.
- 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.
- 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.
- The primary emphasis in procedural programming is placed on the functions within the program and the operations these functions perform.
- This approach to programming has, over the years, created a few programs given the level of complexity to which software has risen:
- 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?
- 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!
- 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?
- Consider for a moment a cell phone:
- When you look at a cell phone, you will see it has certain properties of attributes.
- for example, it has buttons. Some have your basic "phone" keys (numbers, #, ect.). The fancier ones have complete keyboards.
- Most have some sort of screen. Again, these range from basic LCD text screens to fancy touch screens.
- Cell phones can also perform tasks. That is they have behaviors or methods that allow them to take some action(s) using the attributes.
- For example, you can make and receive phone calls. On some, you can take and send pictures, ect.
- So, a cell phone, as an object, is a collection of both its attributes and its methods.
- This is the approach taken in OOP.
- OOP Focuses on the object, which is a collection of the object's attributes and methods bound together as a single unit.
- When we want an object to take some action of perform a task, we send the object a message with a request.
- 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.
- In c++, this is done by making a function call to one of the methods offered by the object.
- The object can manage its own attributes without "sharing" that knowledge with the outside world.
- Why is this approach better then procedural programming?
- There are several very good reasons for writing programs using the OOP approach
- One reason is the re-usability of code.
- Think about the computer you use. Chances are, there are a variety of icons on the desktop.
- 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?
- Why reinvent the wheel? More and more tasks are very similar, if not downright repetitive. Why not reuse what code already exists?
- This translates directly into savings of both time and money during application development.
- Another reason is the encapsulation mentioned above.
- It is because the attributes and methods are packed together as a unit that a programmer can reuse the same code over and over.
- Again, this translates into savings of both time and money since there's less duplication of effort for repetitive tasks.
- A third reason is information hiding.
- 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.
- 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.
- Languages are not developed and then left unchanged. There are always modification and improvements being made (even in something as old as COBOL).
- 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.
- A fourth reason is that a programmer can take advantage of inheritance.
- 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.
- 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.
- Again, this saves time and money if existing objects van be used as a basis for new objects that might be more application specific.