Cute FTP Pro

Demonstration of a Class

A program to demonstrate Classes

Before getting into the nuances of classes let’s take a look at a few simple examples to illustrate classes and objects.

Let’s say that we want to create a timer, something like a stopwatch. We should be able to set the timer to a start value, pause it, stop it or start it. Thinking in terms of OOP we would have one member data:

bullet

count (let’s keep it as an integer)

Being the first example, we’ll implement some simple functions:

bullet

initialize( )

bullet

display( )

bullet

increment( )

The function names are self-explanatory and the functions would operate on the member data (i.e. someone who uses our timer shouldn’t be able to change the value of count directly. If this were allowed then the user might set count to a negative value or might misuse it). The user of our timer object should be able to access our timer in a controlled manner.

#include <iostream.h>
class Timer
{
private:
    int count;

public:

void initialize( )
{
cout<<"timer!";
count=0;
}

void display( )
{
cout<<"remaining:"<<count;
}

void increment( )
{
count=count+100;
}

};

int main( )
{
Timer t1,t2;
t1.display( );
t1.initialize( );
t1.display( );
t1.increment( );
t1.display( );
t2.initialize( );
t2.increment( );
t2.increment( );
t2.display( );
return 0;
}

When you run the program you may get an output similar to this:

Seconds remaining:4289044
Resetting timer!
Seconds remaining:0
Seconds remaining:100
Resetting timer!
Seconds remaining:200

Since this is our first program in classes, we’ll dissect it line-by-line.

Dissection of the program

Explanation for each part

class Timer
{

Begin the declaration of our class called ‘Timer’.

private:
   int count;

As part of data hiding, we’ve put the member data (count) within the private section of the class. Only the public functions of the class can access ‘count’.

public:
   void reset( )
   {
      cout<<"timer!";
      count=0;
   }

The keyword public indicates that everything following this label is part of the public section of the class.

   void initialize( ) { cout<<"timer!"; count=0; }

initialize( ) is a member function of class Timer which will be used for resetting the value of count to zero. Since it is present in the public section, anyone using our class can call this function (and since this function is part of the class, it is permitted to access the member data ‘count’).

   void display( ) { cout<<"remaining:"<<count; }

   void increment( ) { count=count+100; }

We’ve defined 2 more functions display ( ) and increment ( ) which also access the member data ‘count’.

};

Signals the end of our class declaration.

int main( )

{

   Timer t1,t2;

Next comes the main( ) function in which we’ve created two objects. ‘t1’ is an object or an instance of the class Timer. ‘t2’ is another object (i.e. we now have two timer objects in our program). Every instance of a class will have its own copy of the member data. This is similar to how we used to create variables from the fundamental data types (ex: int x, y; creates two integer variables).

   t1.display( );

All public members of the class can be called using the dot operator. Thus we call the member function display using the dot operator. This would display the value of the t1’s variable ‘count’. Since it hasn’t been initialized, you’ll find a garbage value on your screen.

   t1.initialize( );
   t1.display( );

Set member data count to 0.

   t1.increment( );
   t1.display( );

We’ve incremented the value of count from 0 by 100. Thus the last display would yield: Seconds remaining:100

   t2.initialize( );
   t2.increment( );
   t2.increment( );
   t2.display( );

These will all act on the ‘count’ of object t2. Thus at the end of our program: count (t1) will be 100 count (t2) will be 200

return 0;
}

End of the program.

Remember: When we call member functions using the object t1 we are only manipulating the ‘count’ of t1. The object t2 remains unaffected.

Remember: Only functions belonging to the class can access the private members of the class.

This example should have clarified your doubts about a class and an object. We create objects from a class. The class acts like a general framework from which we can create many objects. Each object will have the same member functions and data but the value for the data can be different.

If we apply what we learnt about OOP earlier we can state this in another way: Each object that we create will have its own state. In real life this corresponds to having two timers which are identical in functionality but independent of each other (each one can have a different time).

Try adding the following statement to the code above:

cout<<t2.count; //ERROR

The compiler would complain saying:

cannot access private member declared in class 'Timer'

Let’s modify our class by adding a public member data:

class Timer
{
private:
    int count;

public:
    int dummy;

void initialize( )
{
cout<<"timer!";
count=0;
}

};

int main( )
{
Timer t1,t2;
t1.dummy=90;
cout<<t1.dummy;
return 0;
}

In this case, ‘dummy’ is a public member data of the class Timer. Thus an object of class Timer can directly access dummy (which means you can use the dot operator on dummy). But if you try:

    cout<<dummy;

the compiler would complain saying that the identifier dummy is undeclared. ‘dummy’ can only be accessed through a Timer object.

Note:

You generally won’t find programs with a function like display ( ) which we’ve used above. Instead of this we would generally define a function through which the user (i.e. the programmer who would later use our class) can retrieve the value of the member data. So, instead of the function:

void display( )
{
    cout<<"remaining:"<<count;
}

we can define a function:

int get_count( )
{
    return count;
}

Now the user can code something like this:

int main( )
{
    Timer t1;
    cout<<”The time remaining is:”<<t1.get_count( );
    return 0;
}

Similarly we could also provide another function called set_count using which the user can change the value of the count.

void set_count(int x)
{
    count = x;
}

This can be used instead of the increment( ) function we implemented earlier. But there is a problem in the above function. Can you spot it?

The user of our class, can code:

    t1.set_count(-200);

and the compiler would faithfully obey (it’s like creating a digital clock which can be accidentally/ intentionally set to negative time!). To prevent this then we have to test the value of the argument passed within the member function set_count( ).

Remember: In real application programming, a class designer would avoid using I/O within member functions (i.e. it is preferable to avoid using cout or cin within member functions). This task is left to the user of the class (of course the class designer should provide some member functions through which the user can set member data). In this book you may find cout statements within member functions; these are primarily used to illustrate concepts and how C++ works.

Some important points about classes:

bullet

You cannot access private members of a class directly.

bullet

Private members can be accessed only through the public member functions.

bullet

An object in the program can directly access only the public members through the dot operator (the dot operator is also called the member operator). Private members cannot be accessed directly by an object.

Whenever we call a member function, we actually say that ‘we are passing a message to the object’. This is another term frequently used in OOP.

Can a function be private?

Yes. Nothing prevents you from doing so. Once you declare a function as private, it cannot be accessed using the dot operator. You can only call the private function from within a public function. But you might be wondering why would need to create a private function? This topic will be dealt with an example after learning about constructors and destructors.

Remember: By default, all data and functions in a class are made private. To make something public you have to explicitly specify the access specifier in your class declaration. It is a good practice to explicitly state which members are private and public in your class (as done in the above program).

Go back to the Contents Page

Copyright © 2005 Sethu Subramanian All rights reserved.