#include using namespace std; class AliveT{ public: AliveT() { myId = ++count; cout << "\tCreating instance " << myId << endl; } AliveT( AliveT & other) { myId = ++count; cout << "\tParameter instance " << myId << " from " << other.GetID() << endl; } AliveT & operator = (const AliveT & ) { return *this; } ~AliveT() { cout << "\tDestroying instance " << myId << endl; } int GetID() { return myId; } private: inline static int count = 0; int myId; }; void Function1(void); void Function2(AliveT a); void Function3(AliveT & a); void Function4(void); void Next(void); int main() { // the first two instances come into existance. cout << endl << endl; cout << "Entering Main" << endl; AliveT a; AliveT b; cout <<"after the declarations" << endl; cout << endl; Next(); int i{0}; // declaring c inside of the body makes // a new istance each time the loop is iterated. cout << "Before the while loop" << endl; while (i < 4) { AliveT c; cout << "\t\t i = " << i << " C's id is " << c.GetID() << endl; ++i; } cout << "After the while loop" << endl; cout << endl; Next(); // d will be alive for all iterations of the loop. cout << "Before the for loop" << endl; i = 0; for(AliveT d ; i < 3; ++i) { cout << "\t\t Inside the for loop i = " << i << " and d's id is " << d.GetID() << endl; } cout << "After the for loop " << endl; cout << endl; Next(); // e has a very short lifetime. // And code blocks don't need to be associated with a control struructure // but they probably should be. cout << "In a stand alone code block" << endl; { AliveT e; cout << "\t\t\tE's ID is " << e.GetID() << endl; } cout << "After a stand alone code block" << endl; cout << endl; Next(); // function 1 declares a local variable cout << "Calling Function1" << endl; Function1(); cout << "After return from Function 1" << endl; cout << endl; Next(); // function 1 declares a local variable cout << "Calling Function1" << endl; Function1(); cout << "After return from Function 1" << endl; cout << endl; Next(); // function 2 has a pass by value cout << "Calling Function2" << endl; Function2(a); cout << "After return from Function 2" << endl; cout << endl; Next(); // function 3 has a pass by reference cout << "Calling Function3" << endl; Function3(a); cout << "After return from Function 3" << endl; cout << endl; Next(); //function 4 has a static variable // It will be destroyed last. cout << "Calling Function4" << endl; Function4(); cout << "After return from Function 4" << endl; cout << endl; Next(); // notice no new version of the last variable is created. cout << "Calling Function4" << endl; Function4(); cout << "After return from Function 4" << endl; cout << endl; cout << "Returning from Main " << endl; return 0; } void Next(void) { cout << "Press Enter to continue" << endl; cin.ignore(100,'\n'); cout << endl << endl << endl; } void Function1(void) { cout << "Starting Funciton 1 " << endl; AliveT a; cout << "Exiting Funciton 1 " << endl; return; } void Function2(AliveT p ) { cout << "Starting Funciton 2 " << endl; cout << "In Function 2 and the parameter is id " << p.GetID() << endl; cout << "Exiting Funciton 2 " << endl; return; } void Function3(AliveT & p) { cout << "Starting Funciton 3 " << endl; cout << "In Function 3 and the parameter is id " << p.GetID() << endl; cout << "Exiting Funciton 3 " << endl; return; } void Function4(void) { cout << "Starting Funciton 4 " << endl; static AliveT a; static int x{3}; cout << "\t\t\tIn function 4, a's id is " << a.GetID() << " x = " << x << endl; x++; cout << "Exiting Funciton 4 " << endl; return; }