COM SCI 32 Lecture Notes - Lecture 3: Member Variable
Document Summary
When an outer objects contains member objectsc++ automatically adds code to the outer object"s constructor to first call the default constructors of all the member objects. And then after they"re constructed, it runs the body of the outer object"s constructor. Now our outer constructor can safely use any of the member variables. If there"s no default constructor, the code won"t compile. class stomach { public: Stomach() { mygas = 0; void eat() { mygas++ ~stomach() { cout << fart!n; class brain { public: Brain() { myiq = 100; void think() { myiq += 10; class hungrynerd { public: **order of construction and destruction may be on exam** The c++ adds code to the end of the outer destructor to call the inner objects" destructors. Then, after the outer destructor runs, it calls the destructors of the inner objects (in reverse order of construction). Finally, after all embedded objects have been destructed, the outer object"s memory is freed.