More on Classes and Objects


The idea of OOP is to write programs which will mirror the real world. Whenever you write a program using classes you have to decide as to what are the members of the class? To do this take the real-world entity that you are going to model. For example: Let us model a class based on a cricket player (say a batsman). Our problem is that we want to write a program to maintain records of different batsman (particularly we need to keep track of their highest and lowest scores). Every batsman will have the following attributes (or properties):

We are not interested in storing information about all the attributes and hence we must isolate the attributes that are relevant to our problem. Considering our problem we will only need to use the name, player number, best score and worst score properties.

Next we have to decide on the methods (or functions) that are required in the class. The following will be needed:

Classes are sometimes modeled diagrammatically as shown below:

Text Box: Member functions (methods)
Text Box: Member data (attributes)
Text Box: Name of the class
Text Box: initialize( )
update_best( )
update_worst( )
display( )
Text Box: name : string
number : int
best_score : int
worst_score : int
Text Box: batsman

 

 

 

 

 

 

In this example, ‘batsman’ is the class and any instance created from this class is an object (i.e. if you create an object having values for each of the properties then that object is said to be an instance of the class). For example: if we create an object called ‘ob1’ with the following member values:

Name: Sachin

Number: 10

Highest score: 200

Lowest score: 5

then ‘ob1’ is an instance of the class (or entity) ‘batsman’. This should clarify any doubts you might be having about the difference between a class and an object.

Similarly whatever real life object you consider, you can model them into classes depending on the problem.


Private Member functions:

Usually member data are made private while functions (or methods) are made public. There might be instances where you might want to make certain functions private (i.e. you may not want the user to directly access these functions). Private functions can only be called from within public member functions. These functions are also called ‘helper functions’ Why do we need them?

Let’s take the example of the class ‘batsman’. After every match the user will enter the batsman’s new score and then he will have to call two functions to update the batsman’s record (i.e. the user has to call update_best ( ) and update_worst ( )). It is unnecessary to bother the user with this kind of a double function call. Why should the user access these two functions directly? Instead of this, we could define another function called update ( ) which will call update_best ( ) and update_worst ( ). In this way the user needs to only call one function after every match.

The idea of classes is to restrict user access. We don’t want the user to access data or functions unnecessarily. So, we will make update_best ( ) and update_worst ( ) as private functions while update ( ) will be a public function.

// To illustrate the use of helper functions (private functions)

#include <iostream.h>

class batsman
{
private:
int player_number;
int best_score,worst_score;
void update_best(int);
void update_worst(int);

public:
batsman(int n, int b, int w) //constructor
{
player_number=n;
best_score=b;
worst_score=w;
}

void update(int);
void display( );
};

void batsman::update(int x)
{
update_best(x); //private function is called
update_worst(x);
cout<<"\n\nThe scores have been updated\n";
}

void batsman::display( )
{
cout<<"\nHighest score : "<<best_score;
cout<<"\nLowest score : "<<worst_score;
}

void batsman::update_best(int y) //defining the private functions
{
    if (y>best_score)
    {
    best_score=y;
    }
}

void batsman::update_worst(int z)
{
    if (z<worst_score)
    {
    worst_score=z;
    }
}

int main( )
{
batsman b(1, 140, 20);
cout<<"The scores before the match is ";
b.display( );
b.update(180);
cout<<"\nAfter this match the scores are ";
b.display( );
return 0;
}

The output will be:

The scores before the match is
Highest score : 140
Lowest score : 20
The scores have been updated
After this match the scores are
Highest score : 180
Lowest score : 20

In this example it may seem unnecessary to provide two functions called update_best ( ) and update_worst ( ). Both could have been combined into one function but this example was meant to illustrate how private functions can be used in more complex classes.


Go back to the Contents Page


Copyright © 2004 Sethu Subramanian All rights reserved.