3. Developing C++ Classes Flashcards
True or false?
If we do not provide any custom constructors, the compiler provides an automatic default constructor to our class for free!
True.
The automatic default constructor will only initialize all member variables to their default values.
True or false?
A class can only have one constructor.
False.
In addition to the default constructor, you can also specify custom constructors that accept arguments.
True or false?
Providing any constructor at all prevents the automatic default constructor provided by C++ from being created.
True.
Even if your custom constructor accepts three arguments, the default constructor that accepts zero arguments will not be created automatically.
What is a copy constructor?
A copy constructor is a special constructor that allows us to make a copy of an existing object.
True or false?
If we don’t provide a custom copy constructor, the compiler will provide an automatic copy constructor for us.
True.
The automatic copy constructor will copy the contents of all member variables.
When are copy constructors invoked automatically?
- Passing an object as a parameter (by value)
- Returning an object from a function (by value)
- Initializing a new object
What is a copy assignment operator?
A copy assignment operator defines the behavior when an object is copied using the assignment operator.
What’s the difference between a copy constructor and an assignment operator?
A copy constructor creates a new object.
An assignment operator assigns a value to an existing object (it’s called on an object that has already been constructed.
Name 3 different ways to pass variables around to functions.
- Pass by value (default)
- Pass by pointer
- Pass by reference
Name 3 different ways to return variables from functions.
- Return by value (default)
- Return by pointer
- Return by reference
True or false?
If we don’t provide any custom destructor, the compiler provides an automatic default destructor to our class for free!
True.
The only action of the automatic default destructor is to call the default destructor of all member objects.