COM SCI 32 Lecture Notes - Lecture 3: Member Variable

60 views4 pages

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.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents