SlideShare a Scribd company logo
1 of 49
Download to read offline
Today’s topics
Why objects?

Object-oriented programming (OOP) in C++

�classes
�fields & methods
�objects
�representation invariant




                       2
What are objects?
Objects model elements of the problem context

Each object has:
�characteristics
�responsibilities (or required behaviors)




                           8
Example
Problem Design and build a computer hockey game

Object Hockey player

Characteristics Position, height, weight, salary, number
of goals

Responsibilities Pass the puck, shoot, skate forward,

skate backward, punch another player, etc.


hockey game ˙œ„”˛ˇ•




punch »·                    9
Another example
      Problem Computation modeling in biology

      Write a program that simulates the growth of virus
      population in humans over time. Each virus cell
      reproduces itself at some time interval. Patients may
      undergo drug treatment to inhibit the reproduction
      process, and clear the virus cells from their body.
      However, some of the cells are resistant to drugs and
      may survive.
simulate ˜£




                                10
Write a program that simulates the growth of virus
population in humans over time. Each virus cell
reproduces itself at some time interval. Patients may
undergo drug treatment to inhibit the reproduction
process, and clear the virus cells from their body.
However, some of the cells are resistant to drugs and
may survive.


What are objects?
Characteristics?
Responsibilities?
                          11
Write a program that simulates the growth of virus
population in humans over time. Each virus cell
reproduces itself at some time interval. Patients may
undergo drug treatment to inhibit the reproduction
process, and clear the virus cells from their body.
However, some of the cells are resistant to drugs and
may survive.


What are objects?
Characteristics?
Responsibilities?
                          12
Patient                         Virus
characteristics                 characteristics
�virus population               �reproduction rate (%)

�immunity to virus (%)
         �resistance (%)

responsibilities                responsibilities
�take drugs                     �reproduce
                                �survive




                          13
Questions
Why didn’t we model an object named Doctor?
Surely, most hospitals have doctors, right?




                       14
Questions
Why didn’t we model an object named Doctor?
Surely, most hospitals have doctors, right?


Doesn’t every patient have an age? Gender? Illness?
Symptoms? Why didn’t we model them as
characteristics?




                          15
Basic OOP in C++
Classes
A class is like a cookie cutter; it defines the shape of
objects
Objects are like cookies; they are instances of the class




                Photograph courtesy of Guillaume Brialon on Flickr.

                                        17
Classes: Declaration vs. definition


   client             declaration                definition
            depends                 implements

Declaration (.h files)
�list of functions & fields
�including functions that the class promises to its client

�it’s like a “contract”

Definition (.cc files)
�implementation of functions
                              18
Class declaration
Class declaration 

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);


};




                                  20
class name                 field


class Virus {

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

 public:
                                       constructors
     Virus(float newResistance);

     Virus(float newReproductionRate, float newResistance);
     Virus* reproduce(float immunity);

     bool survive(float immunity);

};

                                 method

don’t forget the semi-colon! 

                                  21
Fields (characteristics) 

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);


};




                                  22
Methods (responsibilities)

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);


};




                                  23
Constructors

 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);


 };

Note the special syntax for constructor (no return type!)


                                   24
Access control: public vs. private 

class Virus {

                           // rate of reproduction, in %

 float reproductionRate;
  float resistance;                                        private
                           // resistance against drugs, in %

  static const float defaultReproductionRate = 0.1;

 public:

  Virus(float newResistance);

  Virus(float newReproductionRate, float newResistance);
  Virus* reproduce(float immunity);

                                                           public

  bool survive(float immunity);


};

private: can only be accessed inside the class
public: accessible by anyone
                               25
How do we decide private vs. public?


  Client             Interface               Implementation
           depends                satisfies


 interface: parts of class that change infrequently
 (e.g. virus must be able to reproduce)

 implementation: parts that may change frequently

 (e.g. representation of resistance inside virus)

                            26
Protect your private parts!


  Client            Interface    Implementation


                         X

 Why is this bad?




                           27
Access control: public vs. private 

class Virus {

                           // rate of reproduction, in %

 float reproductionRate;
  float resistance;                                        private
                           // resistance against drugs, in %

  static const float defaultReproductionRate = 0.1;

 public:

  Virus(float newResistance);

  Virus(float newReproductionRate, float newResistance);
  Virus* reproduce(float immunity);

                                                           public

  bool survive(float immunity);


};

In general,
�keep member fields as private
�minimize the amount of public parts

                         28
Access control: constant fields 

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);


};


Why make it a constant? Why not just declare it as a
normal field?
                                  29
Class definition
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;                                     31
Header inclusion 

#include <stdlib.h> 

#include "Virus.h" 


Virus::Virus(float newResistance) {
  reproductionRate = defaultReproductionRate;
  resistance = newResistance;
}

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




                               32
Constructor definition 

#include <stdlib.h> 

#include "Virus.h" 


Virus::Virus(float newResistance) {
  reproductionRate = defaultReproductionRate;
  resistance = newResistance;
}

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




Remember to initialize all fields inside constructors!


                               33
Constructor definition 

Can also do:
Virus::Virus(float newReproductionRate, float newResistance) {
  reproductionRate = newReproductionRate;
  resistance = newResistance;
}

Virus::Virus(float newReproductionRate, float newResistance) :
reproductionRate(newReproductionRate), resistance(newResistance)

{

}





                               34
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;
}




                                 35
Working with objects
Patient class declaration 

#include “Virus.h”

#define MAX_VIRUS_POP 1000

class Patient {

  Virus* virusPop[MAX_VIRUS_POP];

  int numVirusCells;

  float immunity;       // degree of immunity, in %


 public:

  Patient(float initImmunity, int initNumViruses);
  ~Patient();
  void takeDrug();
  bool simulateStep();

};

                               37
Patient class declaration 

#include “Virus.h”

#define MAX_VIRUS_POP 1000   Array of pointers to objects

class Patient {

 Virus* virusPop[MAX_VIRUS_POP];

 int numVirusCells;

 float immunity;       // degree of immunity, in %

                     Constructor
 public:

 Patient(float initImmunity, int initNumViruses);
 ~Patient();
 void takeDrug();

 bool simulateStep();


};
                   Destructor

                               38
Static object allocation 

class Patient {
   ...
 public:
   Patient(float initImmunity, int initNumViruses);
   ...
};

int main() {

    float initImmunity = 0.1;

    int initNumVirusCells = 5;


    Patient p(0.1, 5);

    p.takeDrug();
}



                                  39
Calling the constructor 

class Patient {
   ...
 public:
  Patient(float initImmunity, int initNumViruses);
   ...
};

int main() {


float initImmunity = 0.1;
 int initNumVirusCells = 5;


 Patient p(0.1, 5);
    p.takeDrug();
}



                               40
Deleting statically allocated objects

class Patient {
   ...
 public:
   Patient(float initImmunity, int initNumViruses);
   ...
};

int main() {

    float initImmunity = 0.1;

    int initNumVirusCells = 5;


    Patient p(0.1, 5);

    p.takeDrug();
}


Automatically destroyed at the end of scope 

                                  41
Objects on heap
To allocate an object on heap:
�use “new” keyword (analogous to “malloc”)


To deallocate:
�use “delete” keyword (analogous to “free”)

Patient* p = new Patient(0.1, 5);

...

delete p;





                               42
Dynamic object creation: Example

Patient::Patient(float initImmunity, int initNumVirusCells) {
  float resistance;

    immunity = initImmunity;

    for (int i = 0; i < initNumVirusCells; i++) {
      //randomly generate resistance, between 0.0 and 1.0
      resistance = (float) rand()/RAND_MAX;

        virusPop[i] = new Virus(resistance);
    }

    numVirusCells = initNumVirusCells;
}




                                   43
Using dynamically allocated objects

bool Patient::simulateStep() {
  Virus* virus;
  bool survived = false;
  ...

    for (int i = 0; i < numVirusCells; i++){
      virus = virusPop[i];

     survived = virus->survive(immunity);

      if (survived) {
        ...
      } else {
        ...
      }
    ...
}


                                 44
What happens during destruction?

The destructor is automatically called
Patient::~Patient(){
  for (int i = 0; i < numVirusCells; i++){
    delete virusPop[i];
  }
}


Patient* p = new Patient(0.1, 5);

...

delete p;




But why didn’t we have a destructor for Virus?

                               45
Representation
invariant
Representation invariant
Statements about characteristics of objects
�defines what it means for an object to be valid
�e.g.“Every IKEA coffee table must have four legs”





         Valid                   Invalid

                                    Figures by MIT OpenCourseWare.

                          47
Example

class Patient {

     Virus* virusPop[MAX_VIRUS_POP];

     int numVirusCells;

     float immunity;       // degree of immunity, in %


 public:

     Patient(float initImmunity, int initNumViruses);

     ~Patient();

     void takeDrug();

     bool simulateStep();


};


What are the representation invariants for Patient?


                                  48
Rep. invariant violation

void Patient::takeDrug(){
  immunity = immunity + 0.1;
}



What’s wrong with this method?





                               49
Preserving rep. invariant

bool Patient::checkRep() {
  return (immunity >= 0.0) && (immunity < 1.0) &&
         (numVirusCells >= 0) &&
         (numVirusCells < MAX_VIRUS_POP);
}


checkRep
�returns true if and only if the rep. invariants hold true

�call checkRep at the beginning and end of every
public method
�call checkRep at the end of constructors

                               50
Preserving rep. invariant

#include <cassert>

class Patient {
   ...
   bool checkRep();
 public:
   ...
};

void Patient::takeDrug() {
  assert(checkRep());
  ...
  assert(checkRep());
}

Patient::Patient(float initImmunity, int initNumViruses) {
  ...
  assert(checkRep());
}
                               51
Preserving rep. invariant
Will calling checkRep() slow down my program?

Yes, but you can take them out once you are confident
about your code.




                         52
Until next time...
Homework #4 (due 11:59 PM Monday)

�implementing BSTs using classes

Next lecture
�inheritance & polymorphism
�templates




                        53
References
Thinking in C++ (B. Eckel) Free online edition!


Essential C++ (S. Lippman)


Effective C++ (S. Meyers)


C++ Programming Language (B. Stroustrup)


Design Patterns (Gamma, Helm, Johnson,Vlissides)


Object-Oriented Analysis and Design with 

Applications (G. Booch, et. al)

                               54
MIT OpenCourseWare
http://ocw.mit.edu




6.088 Introduction to C Memory Management and C++ Object-Oriented Programming
January IAP 2010




For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

More Related Content

Similar to 麻省理工C++公开教学课程(一)

33. an introduction to the viruses
33. an introduction to the viruses33. an introduction to the viruses
33. an introduction to the virusessulochan_lohani
 
Les dossiers d’expertise du Dr Hervé Seligmann, du Prof. Luc Montagnier et du...
Les dossiers d’expertise du Dr Hervé Seligmann, du Prof. Luc Montagnier et du...Les dossiers d’expertise du Dr Hervé Seligmann, du Prof. Luc Montagnier et du...
Les dossiers d’expertise du Dr Hervé Seligmann, du Prof. Luc Montagnier et du...Guy Boulianne
 
33. introduction to virology
33. introduction to virology33. introduction to virology
33. introduction to virologysulochan_lohani
 
Platform Technologies to Accelerate Novel Vaccine Development and Manufacturing
Platform Technologies to Accelerate Novel Vaccine Development and ManufacturingPlatform Technologies to Accelerate Novel Vaccine Development and Manufacturing
Platform Technologies to Accelerate Novel Vaccine Development and ManufacturingMerck Life Sciences
 
Platform Technologies to Accelerate Novel Vaccine Development and Manufacturing
Platform Technologies to Accelerate Novel Vaccine Development and ManufacturingPlatform Technologies to Accelerate Novel Vaccine Development and Manufacturing
Platform Technologies to Accelerate Novel Vaccine Development and ManufacturingMilliporeSigma
 
SIV Viral Variation; Implications for Vaccines and Transmission - Mars Stone PhD
SIV Viral Variation; Implications for Vaccines and Transmission - Mars Stone PhDSIV Viral Variation; Implications for Vaccines and Transmission - Mars Stone PhD
SIV Viral Variation; Implications for Vaccines and Transmission - Mars Stone PhDMars Stone
 
EpiVax FastVax 23Feb2010
EpiVax FastVax 23Feb2010EpiVax FastVax 23Feb2010
EpiVax FastVax 23Feb2010Annie De Groot
 

Similar to 麻省理工C++公开教学课程(一) (10)

33. an introduction to the viruses
33. an introduction to the viruses33. an introduction to the viruses
33. an introduction to the viruses
 
Les dossiers d’expertise du Dr Hervé Seligmann, du Prof. Luc Montagnier et du...
Les dossiers d’expertise du Dr Hervé Seligmann, du Prof. Luc Montagnier et du...Les dossiers d’expertise du Dr Hervé Seligmann, du Prof. Luc Montagnier et du...
Les dossiers d’expertise du Dr Hervé Seligmann, du Prof. Luc Montagnier et du...
 
33. introduction to virology
33. introduction to virology33. introduction to virology
33. introduction to virology
 
Platform Technologies to Accelerate Novel Vaccine Development and Manufacturing
Platform Technologies to Accelerate Novel Vaccine Development and ManufacturingPlatform Technologies to Accelerate Novel Vaccine Development and Manufacturing
Platform Technologies to Accelerate Novel Vaccine Development and Manufacturing
 
Platform Technologies to Accelerate Novel Vaccine Development and Manufacturing
Platform Technologies to Accelerate Novel Vaccine Development and ManufacturingPlatform Technologies to Accelerate Novel Vaccine Development and Manufacturing
Platform Technologies to Accelerate Novel Vaccine Development and Manufacturing
 
Antiviral Retrovirals Fungi
Antiviral Retrovirals FungiAntiviral Retrovirals Fungi
Antiviral Retrovirals Fungi
 
SIV Viral Variation; Implications for Vaccines and Transmission - Mars Stone PhD
SIV Viral Variation; Implications for Vaccines and Transmission - Mars Stone PhDSIV Viral Variation; Implications for Vaccines and Transmission - Mars Stone PhD
SIV Viral Variation; Implications for Vaccines and Transmission - Mars Stone PhD
 
EpiVax FastVax 23Feb2010
EpiVax FastVax 23Feb2010EpiVax FastVax 23Feb2010
EpiVax FastVax 23Feb2010
 
Page2018 duwal
Page2018 duwalPage2018 duwal
Page2018 duwal
 
Virus
VirusVirus
Virus
 

麻省理工C++公开教学课程(一)

  • 1. Today’s topics Why objects? Object-oriented programming (OOP) in C++ �classes �fields & methods �objects �representation invariant 2
  • 2. What are objects? Objects model elements of the problem context Each object has: �characteristics �responsibilities (or required behaviors) 8
  • 3. Example Problem Design and build a computer hockey game Object Hockey player Characteristics Position, height, weight, salary, number of goals Responsibilities Pass the puck, shoot, skate forward, skate backward, punch another player, etc. hockey game ˙œ„”˛ˇ• punch »· 9
  • 4. Another example Problem Computation modeling in biology Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive. simulate ˜£ 10
  • 5. Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive. What are objects? Characteristics? Responsibilities? 11
  • 6. Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive. What are objects? Characteristics? Responsibilities? 12
  • 7. Patient Virus characteristics characteristics �virus population �reproduction rate (%) �immunity to virus (%) �resistance (%) responsibilities responsibilities �take drugs �reproduce �survive 13
  • 8. Questions Why didn’t we model an object named Doctor? Surely, most hospitals have doctors, right? 14
  • 9. Questions Why didn’t we model an object named Doctor? Surely, most hospitals have doctors, right? Doesn’t every patient have an age? Gender? Illness? Symptoms? Why didn’t we model them as characteristics? 15
  • 11. Classes A class is like a cookie cutter; it defines the shape of objects Objects are like cookies; they are instances of the class Photograph courtesy of Guillaume Brialon on Flickr. 17
  • 12. Classes: Declaration vs. definition client declaration definition depends implements Declaration (.h files) �list of functions & fields �including functions that the class promises to its client �it’s like a “contract” Definition (.cc files) �implementation of functions 18
  • 14. Class declaration 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); }; 20
  • 15. class name field class Virus { float reproductionRate; // rate of reproduction, in % float resistance; // resistance against drugs, in % static const float defaultReproductionRate = 0.1; public: constructors Virus(float newResistance); Virus(float newReproductionRate, float newResistance); Virus* reproduce(float immunity); bool survive(float immunity); }; method don’t forget the semi-colon! 21
  • 16. Fields (characteristics) 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); }; 22
  • 17. Methods (responsibilities) 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); }; 23
  • 18. Constructors 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); }; Note the special syntax for constructor (no return type!) 24
  • 19. Access control: public vs. private class Virus { // rate of reproduction, in % float reproductionRate; float resistance; private // resistance against drugs, in % static const float defaultReproductionRate = 0.1; public: Virus(float newResistance); Virus(float newReproductionRate, float newResistance); Virus* reproduce(float immunity); public bool survive(float immunity); }; private: can only be accessed inside the class public: accessible by anyone 25
  • 20. How do we decide private vs. public? Client Interface Implementation depends satisfies interface: parts of class that change infrequently (e.g. virus must be able to reproduce) implementation: parts that may change frequently (e.g. representation of resistance inside virus) 26
  • 21. Protect your private parts! Client Interface Implementation X Why is this bad? 27
  • 22. Access control: public vs. private class Virus { // rate of reproduction, in % float reproductionRate; float resistance; private // resistance against drugs, in % static const float defaultReproductionRate = 0.1; public: Virus(float newResistance); Virus(float newReproductionRate, float newResistance); Virus* reproduce(float immunity); public bool survive(float immunity); }; In general, �keep member fields as private �minimize the amount of public parts 28
  • 23. Access control: constant fields 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); }; Why make it a constant? Why not just declare it as a normal field? 29
  • 25. 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; 31
  • 26. Header inclusion #include <stdlib.h> #include "Virus.h" Virus::Virus(float newResistance) { reproductionRate = defaultReproductionRate; resistance = newResistance; } Virus::Virus(float newReproductionRate, float newResistance) { reproductionRate = newReproductionRate; resistance = newResistance; } 32
  • 27. Constructor definition #include <stdlib.h> #include "Virus.h" Virus::Virus(float newResistance) { reproductionRate = defaultReproductionRate; resistance = newResistance; } Virus::Virus(float newReproductionRate, float newResistance) { reproductionRate = newReproductionRate; resistance = newResistance; } Remember to initialize all fields inside constructors! 33
  • 28. Constructor definition Can also do: Virus::Virus(float newReproductionRate, float newResistance) { reproductionRate = newReproductionRate; resistance = newResistance; } Virus::Virus(float newReproductionRate, float newResistance) : reproductionRate(newReproductionRate), resistance(newResistance) { } 34
  • 29. 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; } 35
  • 31. Patient class declaration #include “Virus.h” #define MAX_VIRUS_POP 1000 class Patient { Virus* virusPop[MAX_VIRUS_POP]; int numVirusCells; float immunity; // degree of immunity, in % public: Patient(float initImmunity, int initNumViruses); ~Patient(); void takeDrug(); bool simulateStep(); }; 37
  • 32. Patient class declaration #include “Virus.h” #define MAX_VIRUS_POP 1000 Array of pointers to objects class Patient { Virus* virusPop[MAX_VIRUS_POP]; int numVirusCells; float immunity; // degree of immunity, in % Constructor public: Patient(float initImmunity, int initNumViruses); ~Patient(); void takeDrug(); bool simulateStep(); }; Destructor 38
  • 33. Static object allocation class Patient { ... public: Patient(float initImmunity, int initNumViruses); ... }; int main() { float initImmunity = 0.1; int initNumVirusCells = 5; Patient p(0.1, 5); p.takeDrug(); } 39
  • 34. Calling the constructor class Patient { ... public: Patient(float initImmunity, int initNumViruses); ... }; int main() { float initImmunity = 0.1; int initNumVirusCells = 5; Patient p(0.1, 5); p.takeDrug(); } 40
  • 35. Deleting statically allocated objects class Patient { ... public: Patient(float initImmunity, int initNumViruses); ... }; int main() { float initImmunity = 0.1; int initNumVirusCells = 5; Patient p(0.1, 5); p.takeDrug(); } Automatically destroyed at the end of scope 41
  • 36. Objects on heap To allocate an object on heap: �use “new” keyword (analogous to “malloc”) To deallocate: �use “delete” keyword (analogous to “free”) Patient* p = new Patient(0.1, 5); ... delete p; 42
  • 37. Dynamic object creation: Example Patient::Patient(float initImmunity, int initNumVirusCells) { float resistance; immunity = initImmunity; for (int i = 0; i < initNumVirusCells; i++) { //randomly generate resistance, between 0.0 and 1.0 resistance = (float) rand()/RAND_MAX; virusPop[i] = new Virus(resistance); } numVirusCells = initNumVirusCells; } 43
  • 38. Using dynamically allocated objects bool Patient::simulateStep() { Virus* virus; bool survived = false; ... for (int i = 0; i < numVirusCells; i++){ virus = virusPop[i]; survived = virus->survive(immunity); if (survived) { ... } else { ... } ... } 44
  • 39. What happens during destruction? The destructor is automatically called Patient::~Patient(){ for (int i = 0; i < numVirusCells; i++){ delete virusPop[i]; } } Patient* p = new Patient(0.1, 5); ... delete p; But why didn’t we have a destructor for Virus? 45
  • 41. Representation invariant Statements about characteristics of objects �defines what it means for an object to be valid �e.g.“Every IKEA coffee table must have four legs” Valid Invalid Figures by MIT OpenCourseWare. 47
  • 42. Example class Patient { Virus* virusPop[MAX_VIRUS_POP]; int numVirusCells; float immunity; // degree of immunity, in % public: Patient(float initImmunity, int initNumViruses); ~Patient(); void takeDrug(); bool simulateStep(); }; What are the representation invariants for Patient? 48
  • 43. Rep. invariant violation void Patient::takeDrug(){ immunity = immunity + 0.1; } What’s wrong with this method? 49
  • 44. Preserving rep. invariant bool Patient::checkRep() { return (immunity >= 0.0) && (immunity < 1.0) && (numVirusCells >= 0) && (numVirusCells < MAX_VIRUS_POP); } checkRep �returns true if and only if the rep. invariants hold true �call checkRep at the beginning and end of every public method �call checkRep at the end of constructors 50
  • 45. Preserving rep. invariant #include <cassert> class Patient { ... bool checkRep(); public: ... }; void Patient::takeDrug() { assert(checkRep()); ... assert(checkRep()); } Patient::Patient(float initImmunity, int initNumViruses) { ... assert(checkRep()); } 51
  • 46. Preserving rep. invariant Will calling checkRep() slow down my program? Yes, but you can take them out once you are confident about your code. 52
  • 47. Until next time... Homework #4 (due 11:59 PM Monday) �implementing BSTs using classes Next lecture �inheritance & polymorphism �templates 53
  • 48. References Thinking in C++ (B. Eckel) Free online edition! Essential C++ (S. Lippman) Effective C++ (S. Meyers) C++ Programming Language (B. Stroustrup) Design Patterns (Gamma, Helm, Johnson,Vlissides) Object-Oriented Analysis and Design with Applications (G. Booch, et. al) 54
  • 49. MIT OpenCourseWare http://ocw.mit.edu 6.088 Introduction to C Memory Management and C++ Object-Oriented Programming January IAP 2010 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.