Week 6 - Gemini Flashcards

(33 cards)

1
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the primary purpose of a constructor in a C++ class? When is it automatically called?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the key rules for naming and defining a constructor in C++?

A
  1. A constructor must have the same name as the class. 2. A constructor definition cannot return a value, not even void.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a ‘default constructor’? When is it typically used, and what happens if you don’t define any constructors in your class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a ‘parameterized constructor’? Give a conceptual example.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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) {}

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a ‘copy constructor’? What is its typical signature, and when is it automatically invoked?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens if you do not define a copy constructor for your class?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a ‘destructor’ in a C++ class? What is its primary purpose and syntax?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When would a destructor be particularly crucial for preventing resource leaks?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the this pointer in C++ class member functions?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give two reasons why you might explicitly use the this pointer within a member function.

A
  1. 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; or return this;).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the type of the this pointer within a non-const member function of a class named MyClass?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a static member variable in a class? How does it differ from a non-static member variable?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Where is a static member variable of a class typically defined (not just declared)?

A

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;.

17
Q

How are static member variables accessed or referred to?

A

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).

18
Q

What is a static member function in a class? What are its key limitations?

A

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.

19
Q

How is a static member function typically called?

A

Using the class name and the scope resolution operator: ClassName::staticFunctionName();.

20
Q

What is a common use case for static member variables and static member functions?

A

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.

21
Q

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?

A

It typically decays into a pointer to its first element.

22
Q

If int arr[5]; int *p = arr;, what is the meaning of p[i] in terms of pointer arithmetic?

A

p[i] is equivalent to *(p + i). It accesses the i-th element of the array starting from the address p points to.

23
Q

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?

A

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.

24
Q

When a function returns a pointer to dynamically allocated memory (using new[]), who is generally responsible for deallocating this memory using delete[]?

A

The caller of the function is generally responsible for deallocating the memory. This should be clearly documented as part of the function’s contract.

25
Consider the C-style string function `strchr(s1, ch)`. What does it return if `ch` is found in `s1`, and what if it's not found?
If `ch` is found in `s1`, `strchr` returns a pointer to the *first occurrence* of `ch` within `s1`. If `ch` is not found, it returns `nullptr` (or `NULL`).
26
Consider the C-style string function `strstr(s1, s2)`. What does it return if substring `s2` is found in string `s1`, and what if it's not found?
If substring `s2` is found in string `s1`, `strstr` returns a pointer to the *first occurrence* of `s2` within `s1`. If `s2` is not found, it returns `nullptr` (or `NULL`).
27
When designing a class, why is it generally good practice to make data members `private` and provide `public` member functions (getters/setters) to access or modify them?
This enforces encapsulation. It hides the internal representation of the data, allowing the class implementation to change without affecting code that uses the class. Setters can also include validation logic before modifying data, and getters control read access.
28
In the context of Sheet2, Problem 1, if `int x = 2; int *p;`, what must be done before `*p` can be safely dereferenced to access or assign a value?
The pointer `p` must be initialized to point to a valid memory location, for example, `p = &x;` or `p = new int;`. Dereferencing an uninitialized pointer leads to undefined behavior.
29
For Sheet2, Problem 2: `int sum(int (*funct)(int), int n);`. How does the `sum` function use the `funct` parameter to calculate the sum $\Sigma_{i=0}^{n-1} funct(i)$?
Inside the `sum` function, `funct` (which is a pointer to a function) is called repeatedly in a loop, for example, `total += funct(i);` for `i` from 0 to `n-1`. This allows `sum` to work with different functions passed as `funct`.
30
Regarding Sheet2, Problem 3, where `int& f(int& a){ a=a+5; return a; }`: If you have `int val = 5; f(val)++;`, explain the sequence of operations and the final value of `val`.
1. `f(val)` is called. `val` (which is 5) is passed by reference to `a`. 2. Inside `f`, `a` (and thus `val`) becomes 10. 3. `f` returns a reference to `a` (which is `val`). 4. The `++` operator is applied to the returned reference, so `val` (which is 10) is incremented to 11. The final value of `val` is 11.
31
For Sheet2, Problem 7 (`reverseDoubles`): If you have a pointer to the start of a list of doubles and its length, what's a common in-place strategy to reverse the list using pointers or indices?
A common strategy is to use two pointers (or indices): one starting at the beginning of the array and one at the end. In a loop, swap the elements these two pointers point to, then move the 'start' pointer one step forward and the 'end' pointer one step backward. Continue until the pointers meet or cross.
32
For Sheet2, Problem 10 (concatenate strings): When designing a function `concatenate(char* dest, const char* src)`, what is a critical precondition regarding the `dest` C-style string?
The `dest` character array must be large enough to hold its original contents plus all the characters of the `src` string, including the `src` string's null terminator, plus one final null terminator for the concatenated string.
33
For Sheet2, Problem 14 (QuickSort): What is the fundamental idea behind the 'pivot' element in the QuickSort algorithm?
The pivot is an element chosen from the array. The array (or sub-array) is then partitioned such that all elements smaller than the pivot are moved to its left, and all elements greater than the pivot are moved to its right. The pivot itself ends up in its final sorted position.