Login    New User    Forgot Password    Help      




 A class is an actual representation of an ADT. It therefore provides implementation details for the data structure used and operations. We play with the ADTInteger and design our own class for it:
  class Integer {    attributes:      int i      methods:      setValue(int n)      Integer addValue(Integer j)    }  

In the example above as well as in examples which follow we use a notation which is not programming language specific. In this notation class {...} denotes the definition of a class. Enclosed in the curly brackets are two sections attributes: and methods: which define the implementation of the data structure and operations of the corresponding ADT. Again we distinguish the two levels with different terms: At the implementation level we speak of ``attributes'' which are elements of the data structure at the ADT level. The same applies to ``methods'' which are the implementation of the ADT operations.

In our example, the data structure consists of only one element: a signed sequence of digits. The corresponding attribute is an ordinary integer of a programming language. We only define two methods setValue() and addValue() representing the two operations set and add.

Definition (Class) A class is the implementation of an abstract data type (ADT). It defines attributes and methods which implement the data structure and operations of the ADT, respectively. Instances of classes are called objects. Consequently, classes define properties and behaviour of sets of objects.


 Recall the employee example of chapter . We have talked of instances of abstract employees. These instances are actual ``examples'' of an abstract employee, hence, they contain actual values to represent a particular employee. We call these instances objects.

Objects are uniquely identifiable by a name. Therefore you could have two distinguishable objects with the same set of values. This is similar to ``traditional'' programming languages where you could have, say two integers i and j both of which equal to ``2''. Please notice the use of ``i'' and ``j'' in the last sentence to name the two integers. We refer to the set of values at a particular time as the state of the object.

Definition (Object) An object is an instance of a class. It can be uniquely identified by its name and it defines a state which is represented by the values of its attributes at a particular time.

The state of the object changes according to the methods which are applied to it. We refer to these possible sequence of state changes as the behaviour of the object:

Definition (Behaviour) The behaviour of an object is defined by the set of methods which can be applied on it.

We now have two main concepts of object-orientation introduced, class and object. Object-oriented programming is therefore the implementation of abstract data types or, in more simple words, the writing of classes. At runtime instances of these classes, the objects, achieve the goal of the program by changing their states. Consequently, you can think of your running program as a collection of objects. The question arises of how these objects interact? We therefore introduce the concept of a message in the next section.

Example Of Class and objects

class abc


public :

int x,y

} ;



abc ob1; // declaring ob1 for class abc

abc.x=10; // declaring value for members of class abc


cout<<"\nX ="<<ob1.x;

cout<<"\nY ="<<ob1.y;


All members of a class--data and methods--are private by default. Private members can be accessed only within methods of the class itself. Public members can be accessed through any object of the class. This distinction is both important and confusing. To make it a bit clearer, consider an example:

class Cat  
{  unsigned int  itsAge;  
unsigned int  itsWeight; 
 Meow();  };  

In this declaration, itsAge, itsWeight, and Meow() are all private, because all members of a class are private by default. This means that unless you specify otherwise, they are private.

However, if you write

Cat  Boots; 
Boots.itsAge=5;        // error! can't access private data!  

the compiler flags this as an error. In effect, you've said to the compiler, "I'll access itsAge,itsWeight, and Meow() only from within member functions of the Cat class." Yet here you've accessed the itsAge member variable of the Boots object from outside a Cat method. Just becauseBoots is an object of class Cat, that doesn't mean that you can access the parts of Boots that are private.

This is a source of endless confusion to new C++ programmers. I can almost hear you yelling, "Hey! I just said Boots is a cat. Why can't Boots access his own age?" The answer is that Boots can, but you can't. Boots, in his own methods, can access all his parts--public and private. Even though you've created a Cat, that doesn't mean that you can see or change the parts of it that are private.

The way to use Cat so that you can access the data members is

class Cat  
{  public:  unsigned 
int  itsAge;  unsigned int  itsWeight;  
Meow();  };  

Now itsAge, itsWeight, and Meow() are all public. Boots.itsAge=5 compiles without problems.

The program below shows the declaration of a Cat class with public member variables.


Accessing the public members of a simple class.

// Demonstrates declaration of a class and
   // definition of an object of the class,
   #include    // for cout

   class Cat                // declare the class object
    public:                 // members which follow are public
      int itsAge;
     int itsWeight;

  void main()
     Cat billo;
     billo.itsAge = 5;    // assign to the member variable
     cout << "billo is a cat who is " ;
     cout << billo.itsAge << " years old.\n";
Output: Billo is a cat who is 5 years old.