Thursday, 5 July 2012

Class Declaration In CPP

class Virus {
float reproductionRate; // rate of reproduction, in % float resistance; // resistance against drugs, in % static const float defaultReproductionRate = 0.1;
public:
Virus(float newResistance);
Virus(float newReproductionRate, float newResistance);
Virus* reproduce(float immunity);
bool survive(float immunity);
};


Class Name
                     class Virus {

 Fields (Characterstics)
                                        float reproductionRate; // rate of reproduction, in % float resistance;                  //resistance   against drugs, in % static const float defaultReproductionRate = 0.1;

Method (Responsbilities)
                                       Virus* reproduce(float immunity);
                                        bool survive(float immunity);
Constructors
                      Virus(float newResistance);
                      Virus(float newReproductionRate, float newResistance);


Access Control : Public vs Private
 Private
float reproductionRate; // rate of reproduction, in %
// resistance against drugs, in %
float resistance;
static const float defaultReproductionRate = 0.1;

Public
Virus(float newResistance);
Virus(float newReproductionRate, float newResistance);
Virus* reproduce(float immunity);
bool survive(float immunity);

private: can only be accessed inside the class
public: accessible by anyone

Class Definition

#<stdlib.h> #include “Virus.h”
Virus::Virus(float newResistance) {
reproductionRate = defaultReproductionRate;
resistance = newResistance; }
Virus::Virus(float newReproductionRate, float newResistance) {
reproductionRate = newReproductionRate;
resistance = newResistance;
}
// If this virus cell reproduces,
// returns a new offspring with identical genetic info.
// Otherwise, returns NULL.
Virus* Virus::reproduce(float immunity) {
float prob = (float) rand() / RAND_MAX; // generate number between 0 and 1
// If the patient's immunity is too strong, it cannot reproduce
if (immunity > prob)
return NULL;
// Does the virus reproduce this time?
if (prob > reproductionRate)
return NULL; // No!
return new Virus(reproductionRate, resistance); }
// Returns true if this virus cell survives, given the patient's immunity bool Virus::survive(float immunity) {
// If the patient's immunity is too strong, then this cell cannot survive if (immunity > resistance) return false;
return true; }
const float Virus::defaultReproductionRate

Header inclusion
#include <stdlib.h>
#include "Virus.h"

Constructor Definition
Virus::Virus(float newResistance) { reproductionRate = defaultReproductionRate; resistance = newResistance;
}
Virus::Virus(float newReproductionRate, float newResistance) { reproductionRate = newReproductionRate; resistance = newResistance;
}


Method definition
// Returns true if this virus cell survives, // given the patient's immunity
bool Virus::survive(float immunity) {
// If the patient's immunity is too strong, // then this cell cannot survive
if (immunity > resistance)
return false;
return true; }


No comments:

Post a Comment