Constructors & Destructors: Controlling Initialization & Destruction
Constructors & Destructors: Controlling Initialization & Destruction
Constructors & Destructors: Controlling Initialization & Destruction
class ToD
{
private:
int h, m;
bool PM;
public:
ToD(){
cout << "ToD object created!" << endl;
}
};
What’s on the screen when the program runs?
Use?
(besides cheesy messages)
• Initialization
• Sometimes objects need to have values or
perform some operation before they can be
used
Isolating Implementation
• User can’t manipulate values
• User doesn’t need to do anything to
initialize
Class Point
{
public:
double x, y;
Point(double a = 0, double b = 0)
{
x = a;
y = b;
}
};