Professional Documents
Culture Documents
6.088 Intro To C/C++: Day 4: Object-Oriented Programming in C++
6.088 Intro To C/C++: Day 4: Object-Oriented Programming in C++
Today’s topics
Why objects?
�classes
�fields & methods
�objects
�representation invariant
Why objects?
assembly languages
binary code
6
Figure by MIT OpenCourseWare.
There are always trade-offs
High-level languages
�simpler to write & understand
�more library support, data structures
Low-level languages
�closer to hardware
�more efficient (but also dangerous!)
Example
Problem Design and build a computer hockey game
Another example
Problem Computation modeling in biology
10
Patient Virus
characteristics characteristics
�virus population �reproduction rate (%)
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?
15
Classes
A class is like a cookie cutter; it defines the shape of
objects
Objects are like cookies; they are instances of the class
17
Classes: Declaration vs. definition
Class declaration
class Virus {
public:
Virus(float newResistance);
};
20
class Virus {
public:
constructors
Virus(float newResistance);
};
method
21
Fields (characteristics)
class Virus {
public:
Virus(float newResistance);
};
22
Methods (responsibilities)
class Virus {
public:
Virus(float newResistance);
};
23
Constructors
class Virus {
public:
Virus(float newResistance);
};
24
Access control: public vs. private
class Virus {
// rate of reproduction, in %
float reproductionRate;
float resistance; private
// resistance against drugs, in %
public:
Virus(float newResistance);
public
};
26
27
class Virus {
// rate of reproduction, in %
float reproductionRate;
float resistance; private
// resistance against drugs, in %
public:
Virus(float newResistance);
public
};
In general,
�keep member fields as private
�minimize the amount of public
28 parts
class Virus {
public:
Virus(float newResistance);
};
Class definition
#<stdlib.h>
#include “Virus.h”
Virus::Virus(float newResistance) {
reproductionRate = defaultReproductionRate;
resistance = newResistance;
}
return NULL;
// 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;
}
#include <stdlib.h>
#include "Virus.h"
Virus::Virus(float newResistance) {
reproductionRate = defaultReproductionRate;
resistance = newResistance;
}
32
Constructor definition
#include <stdlib.h>
#include "Virus.h"
Virus::Virus(float newResistance) {
reproductionRate = defaultReproductionRate;
resistance = newResistance;
}
33
Constructor definition
34
Method definition
return false;
return true;
}
35
#include “Virus.h”
class Patient {
Virus* virusPop[MAX_VIRUS_POP];
int numVirusCells;
public:
};
37
#include “Virus.h”
class Patient {
Virus* virusPop[MAX_VIRUS_POP];
int numVirusCells;
Constructor
public:
bool simulateStep();
};
Destructor
38
class Patient {
...
public:
Patient(float initImmunity, int initNumViruses);
...
};
int main() {
int initNumVirusCells = 5;
p.takeDrug();
}
39
class Patient {
...
public:
Patient(float initImmunity, int initNumViruses);
...
};
int main() {
40
class Patient {
...
public:
Patient(float initImmunity, int initNumViruses);
...
};
int main() {
int initNumVirusCells = 5;
p.takeDrug();
}
41
Objects on heap
To allocate an object on heap:
�use “new” keyword (analogous to “malloc”)
To deallocate:
�use “delete” keyword (analogous to “free”)
...
delete p;
42
immunity = initImmunity;
numVirusCells = initNumVirusCells;
}
43
bool Patient::simulateStep() {
Virus* virus;
bool survived = false;
...
survived = virus->survive(immunity);
if (survived) {
...
} else {
...
}
...
}
44
...
delete p;
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
47
Example
class Patient {
Virus* virusPop[MAX_VIRUS_POP];
int numVirusCells;
public:
~Patient();
void takeDrug();
bool simulateStep();
};
48
Rep. invariant violation
void Patient::takeDrug(){
immunity = immunity + 0.1;
}
49
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
50
#include <cassert>
class Patient {
...
bool checkRep();
public:
...
};
void Patient::takeDrug() {
assert(checkRep());
...
assert(checkRep());
}
Yes, but you can take them out once you are confident
about your code.
52
Next lecture
�inheritance & polymorphism
�templates
53
References
Thinking in C++ (B. Eckel) Free online edition!
54
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.