The Objects of Object Oriented Programming

When you first started learning to program in the C/C++ language your programs were mostly unstructured. That is, you put everything in main() and your primary focus was on the basics and the syntax of simple commands for conditional statements and loops. As your knowledge of the language increased you began doing procedural programming. Now you were learning about functions so you added some to the file your main() function was in and called them from main(). With more experience you "graduated" to modular programming in which you added more than one source code file to a project and grouped together similar functions. You may have even begun having those other source files define classes, the beginnings of object oriented programming.

Real object oriented programming or OOP as it is sometimes called by programming Geeks, with just a bit of humor, is much more than just programming with classes. It is a different way of thinking.

What is an Object?

An object is a software "bundle" consisting of a set of variables which define the states the object can exist in and a set of functions that define the behavior of that object. Software objects are often used to model the real-world objects that you find in everyday life.

The "objects" used to create an OOP program are classes and structures. A class groups together a number of related fields (member variables) of different data types which define the State of the object, and a number of Methods (functions) which define the Behavior of the object and ways of modifying the object's state.

Characteristics of Objects

  1. State - An object stores its state in fields, i.e. member variables in classes.
  2. Defined behaviors - An object expresses its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding the internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation which is a fundamental principle of object-oriented programming.
  3. Defined ways of modifying the state - Only an object's methods should be able to modify its' state, i.e. none of the fields should be directly modifiable by other object's code.

Objects in Programs

  1. Structures - A structure is basically a container of variables.

    You access the fields in a structure with dot notation (.)if you have an instance of the structure or pointer (->) if you have a pointer to an instance of the structure. You can actually place functions in structures in C++ but any programmer over the age of 30 will look at you like you have lost your mind if they see you creating a structure and putting functions in it.

  2. Unions - An object similar in definition to a structure but which is only as large as its' largest field and can only contain a value for one field at a time. The primary purpose of a union is to save on memory. These are not used much in programming now as memory is cheap and other programming techniques are better to use.
  3. Classes - A class is a blueprint for an object.

    A class consists of an interface (.h) file defining the states (member variables) and behaviors (prototyping the functions) of the class, and an implementation (.cpp) file defining the behaviors (functions) of the class.

Classes and Object Oriented Concepts

  1. Friend Functions: A function that is not a part of a class, but has been given access to the private members (variables and functions) of the class. Suppose you defined a class called time_class to hold time and in a separate part of your code, not a part of the class you define a function called presentTime whose purpose is to display the time held in a time_class object. The code sample below shows how to define the class and declare presentTime as a friend function.
    class time_class
    		long m_lSecs;
    		friend char *presentTime(time_class tz);
    		time_class(char *t);
    The following code shows how you might define the function presentTime. It can be in the same file with main() or it can be in the time_class.cpp file, but not defined as part of the class, i.e. it does not have time_class:: in its heading.
    char *presentTime(time_class tz)
    	char *ctbuf;
    	ctbuf = new char[40];
    	long seconds_total;
    	seconds-total = tz. m_lSecs;
    	ltoa(seconds_total, ctbuf, 10); // long to ascii function found in stdlib.h
    	return ctbuf;
    The function presentTime() is external to the class, but by defining it as a friend of the class it is permitted to directly access the m_lSecs private member variable.

  2. Inheritance: A mechanism by which one class acquires the properties (member variables and member functions) of another class. In the drawing below the implementation of a CRectangle only has to define the constructor, destructor, and the Draw() function with platform specific graphics commands. All other variables and methods are inherited from the parent class, CShape. Because the class CRectangle is a subclass of CShape when the CRectangle constructor is called it automatically calls the constructor of its parent class so initializations that apply to all CShape objects can be done in the CShape constructor and applied to all sub-class objects.

  3. Multiple Inheritance: A class can have more than one superclass. Thus it inherits variables and functions from more than one class. In the drawing below a CString object inherits from both the CShape parent class and the string parent class.
    The following opening line for a class definition shows how to create a class with more than one parent:

  4. Polymorphism: The ability to use the same named function in sub-classes of a parent class to perform different functions. The draw() function defined in the CShape class is overridden in each of the sub-classes to draw the appropriate shape. Because draw() is declared as virtual in the parent class, all sub-classes are required to override it.

Class Messages

  1. A message is a request sent from one object to another object telling the receiving object to apply one of its' methods to itself.
    The message to object i is Apply the method setValue with argument 1 to yourself.

    A message then contains:
    1. The name or reference to the object which should receive the message
    2. The name of the method to be invoked
    3. A list of appropriate arguments for the method being invoked
    As an example consider what happens when you press a key and a character appears on the screen.
    1. You press a key and a physical contact is made which results in a message being sent to an OS object called Key telling it to change its state to Pressed.
    2. The key object sends a message to an OS object called Screen telling it to display a given character.
    Note that key knows nothing about the current state of Screen, what program is running, where the character will appear, or anything else. But, it doesnít need to know any of this. It only needs to know the character associated with it and when it receives a message to alter itís state to Pressed then it sends the appropriate message to the Screen object. You may find, such as in the case of mouse events in a GUI, that the messages passed are themselves objects containing information to be used or acted on.

Class Relationships

  1. When you create classes in a program the resulting objects that are instantiated have certain ways of relating to the other objects. Letís look at some of these.

Object Oriented Thinking

The main purpose of a computer program is to solve a problem. Using an Object Oriented approach to solving a problem requires a different way of thinking. You must:

  1. Understand the problem. What exactly is the problem we are trying to solve?
  2. Figure out a solution to the problem, NOT DECIDE ON HOW TO WRITE A PROGRAM.
  3. Outline what objects will be needed to solve the problem, again WITHOUT THINKING IN TERMS OF COMPUTER CODE.
  4. Plan how these objects should interact to solve the problem
  5. Create a design for how these objects will be simulated in a computer program.

What is the value of bundling programs into objects?

  1. Modularity: The source code for an object can be written and maintained independently of the source code for other objects. This means that it is easier to divide a large project up into smaller sections in which different team members can work on separately. Once created, an object can be easily passed around inside the system.
  2. Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
  3. Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement, test, and debug complex, task-specific objects, which you can then trust to run in your own code.
  4. Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine. And you donít have to retest the entire machine functionality just because you replaced a bolt. (Imagine the absurdity of having to have every mechanical and electrical system of your car retested to make sure it functions correctly every time you replaced the tires.)