Week 6 - Gemini Flashcards
(33 cards)
What is the primary purpose of a constructor in a C++ class? When is it automatically called?
A constructor’s primary purpose is to initialize an object of a class when it is created. It is called automatically whenever an object of that class is declared or dynamically allocated using new
.
What are the key rules for naming and defining a constructor in C++?
- A constructor must have the same name as the class. 2. A constructor definition cannot return a value, not even
void
.
What is a ‘default constructor’? When is it typically used, and what happens if you don’t define any constructors in your class?
A default constructor is a constructor that can be called with no arguments (either it has no parameters, or all parameters have default values). It’s used when an object is declared without initializers (e.g., MyClass obj;
). If you don’t define any constructors, the compiler will attempt to generate a public default constructor for you.
What is a ‘parameterized constructor’? Give a conceptual example.
A parameterized constructor is a constructor that accepts arguments, which are used to initialize the member variables of the object being created. Example: BankAccount(double initial_balance, double initial_rate);
allows creating a bank account with specific starting values.
How can you use an initializer list in a constructor definition, and what is one benefit? E.g., ClassName::ClassName(int val) : member_var(val) {}
An initializer list is used after the constructor’s parameter list, preceded by a colon. It initializes member variables directly (e.g., member_var(val)
). One benefit is that it’s the required way to initialize const
member variables and reference member variables. It can also be more efficient for member objects.
What is a ‘copy constructor’? What is its typical signature, and when is it automatically invoked?
A copy constructor creates an object by initializing it with an existing object of the same class. Typical signature: ClassName(const ClassName& other_object)
. It’s automatically invoked when: 1. An object is initialized with another object of the same type (e.g., MyClass obj2 = obj1;
). 2. An object is passed to a function by value. 3. An object is returned from a function by value.
What happens if you do not define a copy constructor for your class?
If you don’t define a copy constructor, the compiler will generate a default one for you. This default copy constructor performs a member-wise copy of the source object’s members to the new object.
What is a ‘destructor’ in a C++ class? What is its primary purpose and syntax?
A destructor is a special member function that is executed automatically when an object of its class goes out of scope or is explicitly deleted. Its primary purpose is to perform cleanup tasks, such as releasing resources (e.g., de-allocating memory allocated by the constructor). Syntax: ~ClassName();
(no return type, no arguments).
When would a destructor be particularly crucial for preventing resource leaks?
When a class’s constructor dynamically allocates memory (using new
or new[]
), the destructor is crucial for de-allocating that memory using delete
or delete[]
. Without this, the memory would be leaked each time an object is destroyed.
What is the this
pointer in C++ class member functions?
this
is an implicit pointer available within any non-static member function of a class. It points to the specific object instance on which the member function was called.
Give two reasons why you might explicitly use the this
pointer within a member function.
- To disambiguate when a member variable has the same name as a parameter (e.g.,
this->data = data;
). 2. To return a reference or pointer to the current object (e.g.,return *this;
orreturn this;
).
What is the type of the this
pointer within a non-const member function of a class named MyClass
?
MyClass* const
(a constant pointer to a non-constant MyClass object). This means this
itself cannot be made to point to a different object, but the object it points to can be modified.
What is a static
member variable in a class? How does it differ from a non-static member variable?
A static
member variable is shared by all instances (objects) of the class; there’s only one copy of it for the entire class, regardless of how many objects are created. Non-static member variables have a separate copy for each object.
Where is a static
member variable of a class typically defined (not just declared)?
It is declared inside the class definition (with the static
keyword) but must be defined (and optionally initialized) outside the class definition, usually in the .cpp
file. Example: int User::next_id = 0;
.
How are static
member variables accessed or referred to?
They can be referred to using the class name and the scope resolution operator (e.g., User::next_id
) or through an object of the class (though the former is clearer as it emphasizes it’s a class-level variable).
What is a static
member function in a class? What are its key limitations?
A static
member function belongs to the class rather than to a specific object instance. Limitations: 1. It can only directly access other static
members (variables or functions) of the class. 2. It does not have a this
pointer because it’s not associated with a particular object.
How is a static
member function typically called?
Using the class name and the scope resolution operator: ClassName::staticFunctionName();
.
What is a common use case for static
member variables and static
member functions?
Tracking data common to all objects of a class, like a counter for the number of objects created, or providing utility functions that are related to the class but don’t need a specific object instance to operate.
When an array name is used in an expression (e.g., assigned to a pointer or passed to a function), what does it ‘decay’ into?
It typically decays into a pointer to its first element.
If int arr[5]; int *p = arr;
, what is the meaning of p[i]
in terms of pointer arithmetic?
p[i]
is equivalent to *(p + i)
. It accesses the i-th element of the array starting from the address p
points to.
What are the potential dangers if you delete p;
when p
points to an array allocated with int* p = new int[10];
? What should be used instead?
Using delete p;
for an array allocated with new int[10];
leads to undefined behavior; typically only the first element’s memory might be freed, and the rest becomes a memory leak. delete[] p;
should be used to correctly deallocate the entire array.
When a function returns a pointer to dynamically allocated memory (using new[]
), who is generally responsible for deallocating this memory using delete[]
?
The caller of the function is generally responsible for deallocating the memory. This should be clearly documented as part of the function’s contract.