OOAD Overview Q/A #4



Question:What is meant by object? How are these created?

Answer:

An object is a single unit having both data and the processes that operate on that data. For example, in object oriented programming language like C++, the data and functions are bundled together as a self contained unit called an object. An object is an entity which has some properties and behavior associated with it. Objects are the basic run time entities in an object oriented system. programming problems are analyzed in terms of objects. The main purpose of using objects are following.

  • They correspond to the real life entities.

  • They provide interactions with the real world.

  • They provide practical approach for the implementation of the solution.

All the objects have a state, behavior and identity.

  • State of an object - The state or attributes are the built in characteristics or properties of an object. For example, a T.V has the size, colour, model etc.

  • Behaviour of the object - The behavior or operations of an object are its predefined functions. For example, a T.V. can show picture , change channels, tune for a channel etc. in object oriented programming terminology the behavior is implemented through methods.

  • Object identity - Each object is uniquely identifiable. For example, the fridge can not become the T.V.

Object representation

An object is represented as shown below:

Object Representation

An object consists of the following.

  • Data members - Data members are the variables. They establish the state or attributes for the object.

  • Member functions - Member functions represent the code to manipulate the data. The behaviour of the object is determined by the member functions.

Example

Objects are the variables of a user defined data type called class. They are created in the same way as the built in data type variables are created. When a class is defined , it does not define or create object of the class. It only specifies the type of information that the object of that class will contain. Consider the class triangle given below:

class triangle
{
   private :
      float side1;
      float side2;
      float side3;
      float area;
   public :
       void read_data ( );
       void area_triangle ( );
       void display ( );
};

Objects of the class triangle can be declared as:

Triangle t;

Here t is object of the class triangle. A number of objects can also be declared as:

Triangle t1 ,t2, t3;

Here t1 ,t2, t3 are three objects of the class triangle. Each object has separate copy of data members.

Object can also be created when a class is defined by placing their names immediately after the closing brace. For example,

class triangle
{
...
} t1 ,t2, t3 ;

Here t1 ,t2, t3 are objects of class triangle.

Advertisements