Week 7 - Gemini Flashcards

(30 cards)

1
Q

What is ‘operator overloading’ in C++?

A

Operator overloading allows you to redefine the behavior of existing C++ operators (like +, -, *, /, <<, ==, []) for user-defined types (classes). This means you can make operators work intuitively with objects of your classes.

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

What is the general syntax for declaring an overloaded operator function?

A

ReturnType operator@(parameters); where @ is the operator symbol being overloaded (e.g., operator+, operator<<).

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

When overloading a binary operator (e.g., + for Complex a + b;), how many arguments does the member function version take, and how many does the non-member (friend) function version take?

A

Member function version: Takes one argument (the right-hand operand, e.g., Complex Complex::operator+(const Complex& other)). The left-hand operand is the this object. Non-member/friend function version: Takes two arguments (both operands, e.g., Complex operator+(const Complex& lhs, const Complex& rhs)).

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

Why must the stream insertion (<<) and extraction (>>) operators typically be overloaded as non-member (often friend) functions?

A

Because the left-hand operand is an ostream object (e.g., cout) or istream object (e.g., cin), which are not part of your class. A member function would require the class object to be on the left (e.g., myObject << cout; which is unnatural).

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

What is the typical signature for an overloaded stream insertion operator << for a class MyClass?

A

std::ostream& operator<<(std::ostream& os, const MyClass& obj); It takes an ostream reference and a const reference to the class object, and returns an ostream reference to allow chaining.

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

Which operators in C++ cannot be overloaded?

A

Scope resolution (::), conditional (?:), sizeof, member selector (.), member pointer selector (.*), and preprocessor symbols (#, ##).

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

Can you change the precedence, associativity, or number of operands of an operator when overloading it?

A

No. The original precedence, associativity, and arity (number of operands) of the operator are preserved.

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

What is a const member function in a C++ class (e.g., double getValue() const;)? What does it guarantee?

A

A const member function guarantees that it will not modify any non-static data members of the object on which it is called. It can be invoked on both const and non-const objects of the class.

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

What happens if a const member function attempts to modify a member variable or call a non-const member function?

A

The compiler will generate an error, as this violates the const guarantee of the function.

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

When should a member function be declared as const?

A

Any member function that does not alter the state of the object (i.e., does not change its member variables) should be declared const. This is good practice for API design and allows the function to be used with const objects.

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

What is an ‘inline function’ in C++? How can a member function be made inline?

A

An inline function is a request to the compiler to replace the function call with the actual function code at the call site, potentially reducing call overhead for small functions. Member functions defined within the class declaration are implicitly inline. Functions defined outside can be made inline by using the inline keyword.

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

What is a friend function of a class? What special privilege does it have?

A

A friend function is a non-member function that is granted special permission to access the private and protected members of the class in which it is declared as a friend. Declaration: friend ReturnType functionName(parameters); inside the class.

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

Give a common use case for declaring a non-member function as a friend of a class.

A

Overloading binary operators (like +, -, *, /, <<, >>) where you want a symmetric feel or when the class object is not the left-hand operand (e.g., std::cout << myObject).

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

When working with classes in multiple files, what typically goes into the header (.h or .hpp) file?

A

The class declaration (including member variable declarations and member function prototypes or declarations), typedefs, constants related to the class, and include guards (#pragma once or #ifndef/#define/#endif).

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

When working with classes in multiple files, what typically goes into the source (.cpp) file?

A

The definitions (implementations) of the class’s member functions (those not defined inline in the header), and definitions of static member variables. The source file must #include its corresponding header file.

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

What is the purpose of #pragma once or include guards (#ifndef HEADER_NAME_H ... #define HEADER_NAME_H ... #endif) in a header file?

A

To prevent the contents of the header file from being included multiple times within a single translation unit, which would lead to redefinition errors during compilation.

17
Q

Is it good practice to put using namespace std; in a header file? Why or why not?

A

No, it’s generally bad practice. Header files can be included in many other files, and putting a using namespace directive in a header pollutes the global namespace for every file that includes it, potentially leading to name collisions.

18
Q

If a class manages a dynamically allocated resource (e.g., an array using new[] in its constructor), what is the crucial role of the destructor?

A

The destructor must deallocate the dynamically allocated resource using delete[] (or delete for single objects) to prevent memory leaks when objects of the class are destroyed.

19
Q

What is the ‘Rule of Three’ (or Five/Zero) in C++ when a class manages resources like dynamic memory? What three functions are typically needed?

A

When a class manages a resource that requires explicit deallocation (like dynamic memory), you usually need to define/delete/default: 1. A destructor (to free the resource). 2. A copy constructor (to handle correct copying of the resource, often a ‘deep copy’). 3. A copy assignment operator (operator=) (to handle correct assignment, also often a ‘deep copy’). If these are not correctly handled, issues like shallow copies, double frees, or memory leaks can occur.

20
Q

Explain ‘deep copy’ versus ‘shallow copy’ in the context of a class with a pointer member that manages dynamic memory.

A

Shallow copy: Only the pointer member is copied, so both the original and copied object point to the same dynamically allocated memory. This is problematic as deleting one object’s memory affects the other, leading to double frees or dangling pointers. Deep copy: New memory is allocated for the copied object, and the contents of the original dynamic memory are copied to this new memory. Each object manages its own distinct resource.

21
Q

What is the purpose of overloading the subscript operator operator[] for a custom class like YourVector? (Sheet 4, Problem 2)

A

It allows objects of the class to be used with array-like syntax for element access. For example, myVector[i] could return a reference to the i-th element, enabling both reading and writing.

22
Q

How might you provide both const and non-const versions of the overloaded subscript operator [] for a class?

A

Provide two overloads: ValueType& operator[](int index); (for non-const objects, allows modification) and const ValueType& operator[](int index) const; (for const objects, allows read-only access).

23
Q

What is std::vector in C++ and which header is required to use it?

A

std::vector is a dynamic array container from the C++ Standard Template Library (STL). It can grow or shrink in size automatically. Header: <vector>.

24
Q

How do you add an element to the end of a std::vector named myVec?

A

Using the push_back() member function: myVec.push_back(value);.

25
How can you get the current number of elements in a `std::vector` named `myVec`?
Using the `size()` member function: `int count = myVec.size();`.
26
For the `DefInt` class in Practical3-2 Problem 1, which takes a function pointer `double (*func)(double)` in its constructor, how would this function pointer be used inside the `ByTrapezoid(int N)` method?
The `ByTrapezoid` method would call the function pointed to by `func` at various x-coordinates within the integration range. For example, if `f_ptr` is the member function pointer, it would use `f_ptr(x_i)` to get the function's value at each $x_i$ for the trapezoidal sum.
27
In the `CartesianPoint` class (Practical3-2 Problem 2), how would the `distanceTo(const CartesianPoint& other_point) const` member function calculate the distance?
It would use the coordinates of the `this` object (`this->x`, `this->y` or simply `x`, `y`) and the coordinates of `other_point` (`other_point.Get_x()`, `other_point.Get_y()`) in the distance formula: $\sqrt{((x_1-x_2)^2 + (y_1-y_2)^2)}$.
28
For the `Complex` class (Practical3-2 Problem 3), how would you implement the overloaded addition operator `Complex operator+(const Complex& a, const Complex& b)` as a non-member (friend) function?
It would create a new `Complex` object. The real part of the new object would be `a.Get_Re() + b.Get_Re()`, and the imaginary part would be `a.Get_Im() + b.Get_Im()`. This new object would then be returned.
29
For the `Polynomial` class (Practical3-3 Problem 1) that stores coefficients in a vector, how would the `evaluate(double x)` function work?
It would iterate through the coefficients $a_i$, calculating each term $a_i \cdot x^i$ (e.g., using Horner's method for efficiency: $a_0 + x(a_1 + x(a_2 + ...)))$ and summing them up to return the polynomial's value at `x`.
30
When creating `YourVector` class with dynamic memory (Practical3-3 Problem 2), why is a copy constructor like `YourVector(const YourVector& other)` essential?
To perform a deep copy. If not provided, the default member-wise copy would just copy the pointer to the internal array, leading to both `YourVector` objects pointing to the same memory. This would cause issues when one object is destroyed (freeing the memory) and the other still tries to use it, or tries to free it again.