C++ Classes And Objects and its Program


Objects are the key to understand object oriented programming approach. It is often used to represent an entity in the real world that can be distinctly identified. An object can be a person, place, thing, concept or anything you see in the real world. Some common examples of an object include car, book, clock, computer, apple etc.

Every object is described by their attributes and behaviours. An attribute possess certain properties that help to distinguish it from other objects. For example - You as an object have attributes like name, age, sex, height etc. Behaviour of an object tells us about what operations or functions an object is capable of performing. For example - An object like you may move, sit, listen, speak etc.

Objects used in the programming are modelled in a similar manner after the real world objects as they also possess attributes as well as behaviours. In the program, data describing the object are referred to as attributes of an object. The behaviour of an object is implemented using functions.

Each object has identity that distinguishes it from all other objects. Two mangoes with same colour, shape and texture are still individual mangoes as a person can eat one and then eat the other. Similarly, two identical twins are two distinct persons even though they may look the same. The Object possess the following characteristics :-

  • Each object has an unique identity, attributes and behaviours.
  • Data and Functions are binded together under a single unit to form an object.
  • Objects in the system communicate with each other using the concept of Message Passing.
  • Objects represents the basic run time entities in an Object Oriented System Approach. They occupy space in memory that stores its data which is manipulated by its own functions.


In the real world, you will often find many individual objects all of same kind. For example - Consider an object -you. Now you have certain properties(attributes) like your age, name, height etc. and possess certain behaviour (functions) like you have some style of walking, speaking, eating. Similarly your friends Pranav, Amit, Kiran also possess certain properties (attributes) like age, name, height etc. and possess certain behaviours (functions) like walking, speaking, eating etc. All these people represents different objects having same properties (attributes) and common behaviours (functions). So all these objects can be grouped together to form a class, Person. Thus, a class is a group of objects with same attributes and common behaviours. It is just a template or blueprint using which individual objects are created.

Objects having same attributes and sharing common behaviour constitute a class. In other words, an object is an instance of a class. All the objects of a class are distinguished on the basis of their attribute values. Once the class has been specified, we can create multiple objects belonging to that class. so class may be thought of as a data type and object as a variable of that data type.


The object is a basic key concept of Object Oriented Programming but still we focus on classes because classes provide us the ability to generalize similar type of objects. A class is conceptual representation of all the objects that share common attributes and behaviours. It defines the attributes and behaviours that are used by all the instances (objects) of that class. Operations can be written once for each class, so that all the objects in a class can take benefits from the code reuse.


  1. Data and functions that operate on that data are binded together to form an object.
  2. Objects are real world entities.
  3. Objects occupy some space in the memory.
  4. Object is an instance of class.
  5. An object is a dynamic entity that has limited lifespan. Objects are created and eventually destroyed. Also during their lifetime, the attributes of the object may undergo significant change.
  6. Table, chair, sofa are some examples of the objects.

  1.  A collection of objects with same attributes and common behaviour is called a class.
  2. Class is just a specification or a logical entity to create objects.
  3. Class doesn't consume space in memory.
  4. A class can have many objects.
  5. Class is a static entity. All the attributes of a class are fixed before, during and after the execution of the program.
  6. Furniture is an example of class which has table, chair and sofa objects.

Program To Calculate Area Of Rectangle Using Objects And Classes

class rectangle
int a,b;
void setdata(int x,int y)
void area( )
int ar=a*b;
cout<<"\n Area of rectangle = "<<ar;
};                   //end of class specification
int main ( )
clrscr( );
rectangle r1,r2;       //object definition
r1.setdata(5,10);     //object r1 calls setdata( )
cout<<"Rectangle 1";
r1.area ( );              //object r1 calls area
r2.setdata(10,20);  //object r2 calls setdata ( )
cout<<"\n Rectangle 2";
r2.area ( );             //object r2 calls area
getch( );
return 0;

Rectangle r1
Area of rectangle = 50
Rectangle r2
Area of rectangle = 200

Explanation - In the above program, a class rectangle is specified that contains two private data members a and b and two public member functions defined inside the class.

The private data members a and b cannot directly be accessed from main ( ) because their scope is limited to the class. The public member functions setdata( ) and area( ) can be directly accessed from main( ) and hence provide well defined interfaces to access the private data members of the class.

Post a Comment