[ Introduction to OOP ] [ Encapsulation ] [ Objects ] [ More on Objects ] [ Abstract Data Types ] [ Inheritance ] [ Abstract Classes ] [ Templates ]
3. Objects
3.1  What others say…

 “An object has state, behavior, and identity; the structure and behavior of similar objects are defined in their class; the terms instance and object are interchangeable”. - Grady Booch [Booch, 91].

“An object is a concept, abstraction, or thing, with crisp boundaries and meaning for the problem at hand. Objects serve two purposes: they promote understanding of the real world and provide a practical basis for computer implementation. Decomposition of a problem into objects depends on judgment and the nature of the problem. There is no one correct representation”.  - James Rumbaugh [Rumbaugh, 91].

3.2   About objects

“Objects” are, in some sense, an unusual issue in programming. In fact, it might even be a little hard to think of a "program" in terms of "objects". You hear the phrase  “object oriented” all the time. You might wonder what all this talk means? What does the word “object” mean in programming? An object  has some information (characteristics, contents, or attributes), and some things you can do to it (operations). You will get different definitions of objects depending on whom you ask. But the most accepted one in the C++ community is that, an object is “an instance of a class”.

An object is something that “represents an individual identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain” [ Smith and Tockey @ www.cyberdyne-object-sys.com ]. An object is the primitive element of object oriented programming and represents a distinct, real-world entity. The use of object approach needs modeling during program analysis and design.

An object basically has two parts – information and operations.  Information, also called data, tells something about the object, but not how to use it. Operations, also called instructions or procedures or actions or methods, tell how to use it. This concept is summarized in Figure 1 [Source: Hares and Smart, 94]. This is a modified version of the authors’ diagram.

Let us take a look at a simple example.

3.3  Counter example

In the following example we define a class called "counter". We start by setting the counter value to "0" (the default value) or by specifying an initial value. We can increment the counter by an "amount" that we specify. To get the value of the counter after incrementing, we use the get() method. We can reset the counter to "0" or to any initial value that we desire. The class definition is shown below.
 class counter {private:
    int value;
 public:
    counter()       { value = 0; } 
    set(int i)      { value = i; }
    incr(int amount){ value += amount; return value; }
    int  get()      { return value; }
    void reset()    { value = 0; }
 }

This class could be used in a program as shown below. In this case, the value of the counter is reset to 0 whenever a value entered exceeds 20.
 
 main() {
    int x;
    counter Counter1, Counter2;
    cout << "Enter the value of counter: ";
    cin  >> x;
    Counter1.set(x);
    if (Counter1.get() > 20)
       Counter1.reset();
    else 
       Counter1.incr(1);
    cout << "Counter1: " << Counter1.get() << "\tCounter2: " << Counter2.get();
    getch();
 }

Two sample outputs are shown below.
 
Enter the value of counter: 12
Counter1: 13 Counter2: 0

Enter the value of counter: 21
Counter1: 0 Counter2: 0
 

It has been found that (see [Booch, 91]), a user can perform five typical operations upon an object. They are:

As mentioned before, an object has state and behavior. We can create an object by creating an instance of a class or, in other words, instantiating a class, or, in still other words, declaring a variable whose type is the class itself.

There are basically two ways to create an object in C++ -- static and dynamic. The static case is the simplest way to create an object. Just like the declaration  int x,  the syntax to create a statically allocated object Y of a given class in  this way in C++ is

class-name Y;

This declares Y to be an object of the class whose name appears on the declaration. In the dynamic case, to create a new object in C++, we use the “new” operator plus a constructor for the type of object  to be created. The syntax is

new class-name(...)

A constructor is a  special method that allows a programmers to initialize the state of objects of that type. A constructor may take arguments to initialize the state of the object. When creating an object, one would choose the constructor whose arguments best reflect how to initialize the new object.

3.4   Signal example

This class is simply called “signal”. It could be used for various things such as: an alarm system, or an LED signifying when a processor is busy. It is a simple class with basic methods to initialize the signal status, that is, to set it on or off. The class definition is shown below.
class signal {private:
    int signal_status;
public:
    int  GetStatus();
    void InitSignal();
    void SetOn();
    void SetOff();
};
    int  signal::GetStatus()  { return signal_status; }
    void signal::InitSignal() { signal_status = 0; }
    void signal::SetOn()      { signal_status = 1; }
    void signal::SetOff()     { signal_status = 0; }

This class could be used in a program as follows. Here, the signal is set “on” in case of an emergency, and  “off” otherwise.
 main() {
    signal S;
    int emergency_level;
    cout << "\n Enter the emergency level: ";
    cin  >> emergency_level;
    if (emergency_level > 4){
       S.SetOn();
       cout << “\n WARNING!! Emergency level exceeded!”;
    }
    else {
       S.SetOff();
       cout << “\n Situation normal!”;
    }
 }

Two sample outputs when the above program is compiled and executed are shown below.
Enter the emergency level: 2
Situation normal!

Enter the emergency level: 5
WARNING!! Emergency level exceeded!

3.5  Point and Rectangle example

In the next example, we make use of two classes, point and rectangle. The example shows how to declare and use objects of one class, in this case ‘rectangle’, that contains the type of some other class, in this case, ‘point’. That is, one class uses another as member data. We have objects (variables) within the class rectangle that are of the type Point.
 
class point { public:
   int x;
   int y;
};

class rectangle { public:
   point UpperLeft, LowerRight;
   rectangle(point P1, point P2);
   int getArea();
};
rectangle::rectangle(point P1, point P2){
   UpperLeft  = P1;
   LowerRight = P2;
}
int rectangle::getArea(){
   return (LowerRight.x – UpperLeft.x)*(UpperLeft.y – LowerRight.y);
}
 

The above class can be used in a program as shown below. This is a simple program that calculates the area of a rectangle. The coordinates of two diagonally opposite edges of the rectangle are set within the program.
int main() {
   int Area;
   point P1, P2;
   P1.x = 50;  P1.y = 50;
   P2.x = 80;  P2.y = 30;
   rectangle R(P1, P2);
   Area = R.getArea();
   cout << "Area of the rectangle = " << Area << endl;
   return 0;
}

When the above program is compiled and executed, we get the following output.
Area of the rectangle = 600

3.6  Cylinder example

The following program creates an object denoting a cylinder. The variables of a cylinder define its dimensions. The class definition is shown first.
class cylinder {
private:
   float OuterRadius = 0.0;
   float InnerRadius = 0.0;
public:
   float SurfaceArea(int length);
   float Volume(int length);
   void  SetInnerRadius(float r) { InnerRadius = r; }
   void  SetOuterRadius(float r) { OuterRadius = r; }
}
#define PI 3.142
float cylinder::SurfaceArea(int Length) {
   float OuterArea, InnerArea, TotalArea;
   OuterArea = 2*PI*Length*OuterRadius;
   InnerArea = 2*PI*Length*InnerRadius;
   TotalArea = InnerArea + OuterArea;
   return TotalArea;
}
float cylinder::Volume(int Length) {
   float Volume;
   Volume = PI*(OuterRadius+InnerRadius)*(OuterRadius+InnerRadius)*Length;
   return Volume;
}

A program that uses this class is shown below. It generates the surface area and volume of a cylinder for lengths 1 to 15.
main() {
   cylinder c1();
   c1.setInnerRadius(1);
   c2.setOuterRadius(2);
   for(int i = 1; i < 15; i++) {
      cout << “Surface area: “ << Cylinder1.SurfaceArea(i) << endl;
      cout << “Volume: “       << Cylinder1.Volume(i) << endl;
   }
}

We go a little deeper into objects in the next section, “More on objects”.

3.7  References :

1. [Booch, 91] Grady Booch, Object oriented design with applications, Benjamin/Cummings, 1991.
2. [Rumbaugh, 91] James Rumbaugh, Object oriented programming, Addison-Wesley, 1991.
3. [Berard, 93] Edward V. Berard, Essays on object oriented software engineering, Prentice Hall,    1993.
4. [Smith, 91] David N. Smith, Concepts of object oriented programming, McGraw Hill, 1991.
5. [Hares and Smart, 94] Hares and Smart, Object orientation : Technology, techniques, management and migration, Wiley, 1994.
6. [Budgen, 94] David Budgen, Software design, Addison-Wesley, 1994.
7. http://www.soft-design.com/softinfo/objects.htm
8. http://www.heretics.demon.co.uk/ooman/frames/objects.htm

[ Introduction to OOP ] [ Encapsulation ] [ Objects ] [ More on Objects ] [ Abstract Data Types ] [ Inheritance ] [ Abstract Classes ] [ Templates ]