Demonstration of the Bacteria Class


                      We shall create a class called ‘bacteria’. This class will model a real life bacteria and we shall assume that the bacteria is going to move only on a 2-D (two-dimensional) area (i.e. we will only bother about the x and y axis). Of course you can extend it to 3-D as well. To keep track of the position, we shall make use of two variables x[2] and y[2].

x[0] and y[0] will be used to store the starting position. Once the bacteria moves, the new position will be stored in x[1] and y[1]. For the next movement, x[1] and y[1] will become the starting point and so we set:

x[0]=x[1] and

y[0]=y[1]

Another data member is the variable ‘stepsize’. This determines the length of each stride of the bacteria. Higher the value, the more distance the bacteria will cover each time.

The other two variables needed are ‘ai’ and ‘bi’. This is used to specify the direction of the movement (those of you who have learnt about vectors will know about the properties of a unit vector). Anyway a unit vector is needed to decide the direction of movement.

The program written below is meant to illustrate the use of comments as well. Many programmers have different styles of commenting but you should always make use of comments throughout your program.

//****************************************************************
//Aim :Illustrate the movement of a bacteria based on food available *
// The class bacteria models a general bacteria *
//Author :Santhosh *
//Written on :12 Feb 2004

                    //Revised on :16 Feb 2004 *
                    // (modified the unit_vec( ) function) *
                    //****************************************************************

class bacteria
{

protected:
float x[2],y[2],food[2];                               
     //To store the old and new position
int stepsize;
float ai,bi;                                         
           //Unit vector

public:

bacteria( )                                                     //Constructor with no arguments
{

x[0]=rand( );
y[0]=rand( );
stepsize=1;
food[0]=x[0]+y[0];
ai=bi=food[1]=0;
x[1]=y[1]=0;

}

void disp_final( );                                         //To display new position
void disp_initial( );                            
            //Display initial position
void unit_vec( );                                             //To generate direction
void calculate( );                                                 //To move the bacteria
void move( );
int test( );
};

void bacteria::unit_vec( )

{

ai = rand( );
bi = rand( );
float mag = (ai*ai) + (bi*bi);
mag = sqrt(mag);
ai = ai/mag;
bi = bi/mag;

}

void bacteria::calculate( )

{

x[1]=x[0]+(stepsize*ai);
y[1]=y[0]+(stepsize*bi);
food[1]=x[1]+y[1];

}

int bacteria::test( )

{

if (food[1]>food[0])                                             //only if food increases we want to move bacteria
{
    return 1;
}

else
    return 0;

}

void bacteria::move( )

{

x[0]=x[1];
y[0]=y[1];
food[0]=food[1];

}

void bacteria::disp_initial( )

{

cout<<endl<<endl<<"The original position of bacteria is : ";
cout<<x[0]<<" , "<<y[0];

}

void bacteria::disp_final( )
{

cout<<endl<<"The new position of bacteria is : ";
cout<<x[1]<<" , "<<y[1];

}

int main( )

{
int round;
bacteria fever;

for (round=0; round<3; round++)

{
fever.disp_initial( );
fever.unit_vec( );
fever.calculate( );
while (fever.test( )= =0)

{
fever.unit_vec( );
fever.calculate( );
}

fever.move( );
fever.disp_final( );
}

return 0;
}

The output would be:

The original position of bacteria is : 41 , 18467
The new position of bacteria is : 41.2325 , 18468
The original position of bacteria is : 41.2325 , 18468
The new position of bacteria is : 42.0056 , 18468.6
The original position of bacteria is : 42.0056 , 18468.6
The new position of bacteria is : 42.3698 , 18469.5

The test( ) function is used to ensure that the bacteria moves to positions with more food. If the amount of nutrition at the new place is less, then we will find a new unit vector and search in another position. In the above program the food will always keep increasing in the new position (because there is no provision provided for generating a negative unit vector). In real applications you would have to provide means for doing that as well. Also, the food function here is simply ‘x’ coordinate plus ‘y’ coordinate; in real applications you will have complicated equations.

The output may appear a little weird. What does 42.3698 , 18469.5 mean? Actually we are working with vectors; thus the output is as good as saying:

42.3698 x + 18469.5 y

42.3698 units on the x axis and 18469.5 units on the y axis.

This is a very simple illustration. In reality, the bacteria would probably have to move around thousands of times and it has to move in some logical manner. Over here we make the bacteria move in a completely random manner. And you will also need to use a lot more bacteria objects to search for the best point (one bacteria object will not be sufficient to search a huge area).

The use of Inheritance:

We’ve modeled a general class called ‘bacteria’ but the problem is that certain types of bacteria have different methods for movement (for example: E.Coli and TB bacterium move differently). E.Coli and TB are bacteria but they have different movements. Now what can we do? Should we declare another class called ‘ecoli’ and ‘tb’? Should we again declare the coordinate variables x,y,z and the member functions in each of these classes? And if we should do that, what is the purpose of having a general ‘bacteria’ class?

The idea of having a general bacteria class serves two purposes:

The ecoli and TB will also be modeled as classes but they will be derived from the general ‘bacteria’ class. In this way the two classes will inherit the properties of the ‘bacteria’ class. This is known as inheritance. Thus now you can create objects of type ‘TB’ and ‘ecoli’ as well. Inheritance will be dealt with in Chapter 10.

Suppose another programmer in future wants to add another type of bacteria, he can simply derive a new class from the existing ‘bacteria’ class (thus saving time and reusing the code).


Go back to the Contents Page


Copyright © 2005 Sethu Subramanian All rights reserved. Sign My Guestbook