Tutorial on Object Oriented Programming

EE 5340/7340: Biomedical Instrumentation

Carlos E. Davila

Electrical Engineering Dept, SMU

 

 

What is an Object? An object in computer programming is very much like everyday objects we see in the real world. For example, as I write this tutorial, I see a coffee mug, a telephone, some speakers, and of course, a PC monitor. All of these objects are characterized by having a certain state, for example, my coffee mug is empty, my phone is white, my speakers are silent, my monitor is turned on. In addition, each of these objects has a particular behavior or functionality (the phone rings, the coffee mug fills with a certain amount of hot liquid, the speakers generate audio at a certain volume, etc). Therefore, real-world objects are characterized by a certain state and behavior. In computer programming we also have objects having states and behaviors, therefore objects used in Object Oriented Programming (Oop) effectively model real world objects.

 

In Oop, an object is defined by a class. The class can therefore be considered a blueprint for a particular object. For example lets look at those everyday objects called cars.  All cars have features in common, they all have four wheels, they all have an engine, a certain mileage on the odometer, a certain gas mileage, and a certain volume of gas in the tank. The values these features take on represent the state of a car object that used the car class as a blueprint. As for behavior, well, there are many different ways we can characterize the behavior of a car. For our example why don’t we limit our car’s behavior to “driving” since that is what cars do:-) The act of driving the car a certain distance will affect the state of the car (for example it will increase its mileage and decrease the amount of gas in its tank). The class called “cars” then defines the features of a car that are common to all cars and it also defines the behavior and how that behavior interacts with its state. A car object is an instantiation of the car class. For example the object “Ford_Mustang” would be an instantiation of our car class. In C++ the class declaration would look like this:

 

class cars

{

private:

      double gas_mileage; //gas mileage in miles/gallon

      double mileage; //odometer reading

      double gas_volume; //gas volume in gallons

public:

      cars(void); //constructor function

public:

      ~cars(void); //destructor function

      void drive(double); //prototype for the drive function

};

 

Note some of the members of the class are designated as “private” others are “public”. The private member variables represent the internal state of the car (its mileage, gas volume, etc) and can only be accessed by functions of the class (in this case the constructor, destructor, and the drive function). Public functions can be accessed by any other part of the program. The public functions represent the interface to the class and are the only way we can access and modify the car’s internal state. Limiting access to an object’s internal state to functions in its public interface is called data hiding or data encapsulation. A nice graphical representation of this is adapted from the Java Tutorial as follows:

 

In this diagram, the internal state, represented by the private variables “gas mileage”, “gas in tank”, and “total miles” can only be accessed from the outside world by the public constructor, destructor, drive, and dashboard functions (the interface to the class). The constructor function allocates the memory necessary for the object when it is instantiated. However in this case, since the parameter passed to it is “void”, none of the objects variables are initialized.

 

Bring up Visual C++ 2005 and start a new project (from the File menu select “New” and click on “Project”). Select the “Win32” project type and then select “Win32 Console Application” as shown:

 

 

For the project name, enter “mycars” (or whatever you prefer) and click the “OK” button. Then you will see the Win32 Application Wizard, click “Applications Settings” on the left part of the wizard, then check the “Empty project” checkbox and click “Finish”:

 

 

The application wizard generates an empty project and you will see the standard Visual C++ 2005 IDE. The solution explorer is found on the left side of the IDE. Right click on “Source files” and select “Add” and click on “Class”.  Under “Categories” select C++ and the select “C++ Class” and click the “Add” button.

 

 

 

 You will see the following window:

 

 

Under “Class name” type “cars”, notice it automatically selects a name for the header file “cars.h” and the .cpp file “cars.cpp”. Click the “Finish” button. This process generates two files. The first, “cars.h” is found in the Header files folder in the Solution Explorer, and the cars.cpp file is found in the Source files folder. Double click on the file “cars.h” in the Solution Explorer and copy and paste the cars class definition found above into the editor window.

 

Now double click on the “cars.cpp” file in the Solution Explorer and you will see the following

 

#include "cars.h"

 

cars::cars(void)

{

}

 

cars::~cars(void)

{

}

 

This file contains a definition of the constructor and destructor and an include statement that adds the cars header file.

 

Lets modify the constructor so that we can initialize the gas volume, gas mileage and odometer when we construct a car object. The constructor will have to be modified in two places. The “cars.h” file should like this:

 

class cars

{

private:

      double gas_mileage; //gas mileage in miles/gallon

      double mileage; //odometer reading

      double gas_volume; //gas volume in gallons

public:

      cars(double, double, double); //constructor function

public:

      ~cars(void); //destructor function

      void drive(double); //prototype for the drive function

};

 

where the highlighted area is the one that needs modification. Then in the “cars.cpp” file modify the constructor definition as follows:

 

cars::cars(double new_gas_mileage, double new_mileage, double new_gas_volume)

{

      gas_mileage = new_gas_mileage;

      mileage = new_mileage;

      gas_volume = new_gas_volume;

}

 

Therefore, when the constructor is called, it initializes the cars state to user-defined values. Now lets add the constructor, we can do this in the main function, which always goes at the bottom of our program file:

 

void main()

{

      cars civic(39, 0, 10); //construct a Honda Civic object

}

 

Here, we have created an instance of a cars object called “civic”, which has a gas mileage of 39 gallons/mile, has 0 miles (new!) and has 10 gallons of fuel in the tank.

 

Modify the “cars.cpp” file by adding the following function definition right after the destructor:

 

void cars::drive(double distance)

//this function results in the car being driven

//dist miles. It must modify the internal variables

//accordingly

{

//amount of gas cosumed

double g_consum = distance/gas_mileage;

if (g_consum < gas_volume) {

      mileage += distance;//increase mileage on odometer

      gas_volume -= g_consum;

      }

else {

      mileage += gas_volume*gas_mileage;

      gas_volume = 0;

      }

}

 

The “drive” function takes as the input parameter the distance to be driven. The function first computes the amount of gas required to drive that distance (g_consum = distance/gas_mileage). If the gas consumption is below the total gas volume in the tank, we’re fine and we can add the distance traveled to our odometer mileage and reduce the gas volume in the tank by g_consum. However, if the gas consumption exceeds the amount of gas in the tank, we run out of gas:) the gas in the tank must be set to zero and the odometer mileage can only be increased by  gas_vol*gas_mileage.

 

We still need to add some additional functionality to our class. We need to be able to display the car’s state, otherwise, the user cannot directly access the private variables we’ve already defined. Lets add a function to the class header file called “dashboard” as follows:

 

private:

      double gas_mileage; //gas mileage in miles/gallon

      double mileage; //odometer reading

      double gas_volume; //gas volume in gallons

public:

      cars(double, double, double); //constructor function

public:

      ~cars(void); //destructor function

      void drive(double); //prototype for the drive function

      void dashboard(void); //displays internal variables

};

 

Then in your “cars.cpp” file add the following function definition between the cars::drive and the main function:

 

void cars::dashboard(void)

{

      cout << "gas volume:" << gas_volume << "gallons\n";

      cout << "odometer reading:" << mileage << "miles\n";

}

 

You will also have to add

 

#include <iostream>
using namespace std;

 

to the top of your “cars.cpp” file so you can use the input/output stream that prints to the console.

 

Finally, lets take the Civic out for a spin, add the following to your main function:

 

void main()

{

      cars civic(39, 0, 10); //construct a Honda Civic object

      civic.drive(300);

      civic.dashboard();

}

 

Build the program (from the Build menu, select “Build Solution”) and then run it (“Debug” menu, then click on “Start without debugging”).

 

Try adding some additional objects, like a Hummer (16 miles/gallon) and see how far you get on 10 gallons of gas:)

 

References:

 

http://java.sun.com/docs/books/tutorial/java/concepts/index.html