Difficulty: Introductory | Prerequisites: C++ Classes and Objects: OOP Fundamentals (Part 1 of these notes).
This is the second half of Chapter 7. Once you understand classes, objects, and access specifiers from Part 1, this section covers the lifecycle of an object (how it is born and destroyed), how structs compare to classes, and how enumerated types give you named constants with type safety. All of these topics appear on the Chapter 7 exam.
Constructors initialise an object automatically when it is created; destructors clean up when it is destroyed. C++ structs are nearly identical to classes except their members default to public. Enumerated types (enum) let you define a set of named integer constants, but cout prints their underlying integer value, not the name.
Constructor
A special public member function that is automatically called when an object of the class is created. Its job is to initialise the object's member data. A constructor must have the same name as the class and has no return type at all (not even void).
Default constructor
A constructor that does not require any arguments to be passed to it. A class may have only one default constructor. If you do not write any constructor, the compiler provides one implicitly.
Destructor
A member function that is automatically called when an object is destroyed (for example, when it goes out of scope). Its name is the class name preceded by a tilde (~). A class may have only one destructor.
Tilde (~)
The character prefixed to the class name to form the destructor's name. For a class called Circle, the destructor is ~Circle().
Overloaded constructor
A class can have more than one constructor, each accepting different parameters. This is constructor overloading. The constraint is that only one of them may be the default (no-argument) constructor.
Structure (struct)
A C++ data type similar to a class, but whose members are all public by default. Structures are typically accessed directly with the dot operator rather than through member functions.
Object composition
When a class has a member variable that is an instance of another class. The exam tests the terminology here: this is called object composition, not object nesting.
Enumerated data type (enum)
A programmer-defined type consisting of a set of named integer constants called enumerators. Declared with the enum keyword. The enumerators are assigned integer values starting from 0 by default.
Enumerator
One of the named constants inside an enum declaration. For example, in enum Season {Spring, Summer, Fall, Winter}, Spring is an enumerator with value 0, Summer has value 1, and so on.
Constant reference parameter
Passing an object or struct to a function as const ClassName&. The function accesses the original object (not a copy), cannot modify its members, and it is more efficient than passing by value. All three of those properties are true simultaneously.
A constructor is a public member function that runs automatically when an object is created. You do not call it yourself later to reinitialise data.
It must have the same name as the class.
It has no return type whatsoever. Not void, not int, not bool. None of the above.
A class can have more than one constructor (overloading), but only one of them can be the default (zero-argument) constructor.
A class is not required to have exactly one constructor. It can have zero (compiler provides one), one, or many.
A destructor is automatically called when an object is destroyed (goes out of scope, is deleted, etc.).
Its name is the tilde (~) followed by the class name: ~Circle().
A class may have only one destructor. (Contrast with constructors, where you can have many.)
A class may have only one default constructor and only one destructor.
A structure (struct) has member variables like an object, but they are usually all public and accessed directly with the dot operator, rather than through member functions.
A structure is similar to a class in that its data can be initialised with a constructor, and it can be passed to or returned from a function. But unlike a class, its member data is not typically private.
When a class has a member variable that is an instance of another class, this is called object composition, not object nesting. The exam tests this specific term.
Declared with enum: enum Season {Spring, Summer, Fall, Winter};
To also define an object in the same statement, the correct syntax uses a separate declaration: enum Season {Spring, Summer, Fall, Winter}; Season favoriteSeason; The exam's trick question shows several incorrect one-line combinations, and the answer is often "None of these."
Enumerators are assigned integer values starting at 0. So Spring = 0, Summer = 1, Fall = 2, Winter = 3.
When you cout an enum variable, C++ prints the integer value, not the name. cout << favoriteSeason; after assigning Summer prints 1, not Summer.
The enum keyword is lowercase. ENUM is not valid C++.
Enumerator names are bare identifiers, not strings. {"Spring", "Summer"} is wrong; {Spring, Summer} is right.
When you pass an object or struct by constant reference (const ClassName& obj), three things are true simultaneously:
The function accesses the original object, not a copy.
The function cannot modify any member variables.
It is more efficient than passing by value.
The exam tests whether all three of these are true. The answer is yes, all of them.
Students often think a constructor can have a return type of void. It cannot. Constructors have no return type at all.
Students often assume a class must have exactly one constructor. A class can have zero, one, or many constructors. The constraint is on default constructors (at most one) and destructors (exactly one).
Students sometimes think a constructor is called whenever you want to reinitialise an object's data. A constructor is called once, automatically, at object creation. To change data later, you use mutator functions.
Students sometimes think cout will print the name of an enum value (e.g. "Summer"). It prints the underlying integer (e.g. 1).
Students often think a member variable that is an instance of another class is called "object nesting." The correct term is object composition.
Students sometimes think struct members are private by default, like class members. They are public by default.
⚠️ Constructors have no return type. The answer is "none of the above" when given int, bool, void, or "any of the above."
⚠️ A constructor must have the same name as the class.
⚠️ A destructor name begins with a tilde (~).
⚠️ A class may have only one default constructor and only one destructor.
⚠️ The enum cout question is a favourite: assigning Summer (the second enumerator, index 1) and printing it outputs 1, not Summer.
⚠️ The enum syntax question often has "None of these" as the correct answer because all listed options contain subtle errors (strings instead of identifiers, wrong punctuation, uppercase ENUM).
⚠️ Object composition, not object nesting. The term matters.
⚠️ Constant reference passing: all three properties (original access, no modification, more efficient) are true. The answer is "all of the above," not a subset.
True or False: A constructor may have a return type of void. (Answer: False)
The name of a destructor begins with a ________. (Answer: tilde, ~)
True or False: A class must have exactly one constructor. (Answer: False)
In enum Season {Spring, Summer, Fall, Winter};, the integer value of Fall is ________. (Answer: 2)
True or False: Structure members are private by default. (Answer: False)
Q: A constructor may have a return type of (a) int, (b) bool, (c) void, (d) any of the above, (e) none of the above. Which is correct?
A: (e) None of the above. Constructors have no return type at all.
Q: A constructor that does not require any arguments be passed to it is called what kind of constructor?
A: A default constructor.
Q: A destructor is a member function that does what?
A: It is automatically called when an object is destroyed.
Q: A class may have how many default constructor(s) and how many destructor(s)?
A: Only one default constructor and only one destructor.
Q: A class can have a member variable that is an instance of another class. What is this called?
A: Object composition (not object nesting).
Q: A structure is similar to a class in which ways?
A: Its data can be initialised with a constructor, and it can be passed to or returned from a function. But its member data is not typically private like a class's.
Q: What will cout << favoriteSeason; display if favoriteSeason was assigned Summer from enum Season {Spring, Summer, Fall, Winter}?
A: It displays 1. Enumerators are integers starting from 0. Summer is at index 1.
Q: When an object is passed to a function as a constant reference, which of the following are true? (a) The function accesses the original object. (b) The function cannot modify members. (c) It is more efficient than passing by value. (d) All of the above.
A: (d) All of the above.
Constructors and destructors become critical in later chapters when you work with dynamic memory allocation (new and delete), where the destructor's job is to free heap memory and prevent leaks. Object composition introduced here is the precursor to understanding "has-a" relationships, which you will contrast with "is-a" relationships when inheritance arrives in Chapter 11. Enumerated types reappear in switch statements and state machines throughout the course.
C++ constructor, C++ destructor, default constructor, tilde destructor, constructor no return type, constructor overloading, C++ struct vs class, structure public by default, enum C++, enumerated data type, enumerator integer value, cout enum output, object composition, constant reference parameter, const reference C++, pass by reference, Gaddis Chapter 7, Starting Out with C++ Early Objects, CS101, object lifecycle, class initialization