/////////////////////////////////////////////////////// // // constructors.cpp // // Started 1/16/97 by Jeff Ondich // Last modified: 1/16/97 // // 1. Read the program. What's it going to do // as it stands? In particular, which constructor // do you think will get called, and when? // Try it and see if you were right. // // 2. Uncomment line B in main(). Predict what's // going to happen now. When is the constructor // called, when is the destructor called, and // why? // // 3. Get rid of BigBird() (or at least don't call // him from main()--too much clutter). Now, // move line A inside the for loop body. Recompile // and run. What happened? Why? Surprised? // //////////////////////////////////////////////////////// #include /////////////////////////////////////////////////////// // Here's the interface for a new version of DopeyClass. // Note that in addition to our old pals GetNumber() // and SquareNumber(), DopeyClass has an extra // constructor and a destructor. /////////////////////////////////////////////////////// class DopeyClass { private: int mNumber; public: DopeyClass( int N ); DopeyClass( void ); ~DopeyClass( void ); int GetNumber( void ); void SquareNumber( void ); }; /////////////////////////////////////////////////////// // The next five function definitions constitute the // implementation of DopeyClass. /////////////////////////////////////////////////////// DopeyClass::DopeyClass( int N ) { mNumber = N; cout << "One-parameter DopeyClass constructor: " << mNumber << endl; } DopeyClass::DopeyClass( void ) { mNumber = 3; cout << "Zero-parameter DopeyClass constructor: " << mNumber << endl; } DopeyClass::~DopeyClass( void ) { cout << "The DopeyClass destructor: " << mNumber << endl; } int DopeyClass::GetNumber( void ) { return( mNumber ); } void DopeyClass::SquareNumber( void ) { mNumber = mNumber * mNumber; } /////////////////////////////////////////////////////// // The main program /////////////////////////////////////////////////////// void BigBird( int n ); int main( void ) { cout << "The beginning of main()" << endl; DopeyClass myDopeyObject; // Line A for( int i=0; i < 4; i++ ) { myDopeyObject.SquareNumber(); cout << myDopeyObject.GetNumber() << endl; } // BigBird( 5 ); // Line B cout << "The end of main()" << endl; return( 0 ); } void BigBird( int n ) { cout << "The beginning of BigBird()" << endl; DopeyClass d( n ); d.SquareNumber(); cout << "Here's BigBird's number now: " << d.GetNumber() << endl; cout << "The end of BigBird()" << endl; }