Week 9 - Gemini Flashcards

(17 cards)

1
Q

What is the primary purpose of using ‘templates’ in C++ (both function and class templates)?

A

Templates allow for ‘generic programming’ by enabling the creation of functions and classes that can operate with different data types without having to rewrite the code for each type. They act as blueprints.

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

What is a ‘function template’? Describe its basic syntax.

A

A function template is a blueprint for creating a family of functions that perform the same operations but on different data types. Syntax: template <typename T> return_type function_name(T parameter1, T parameter2, ...){ /* function body */ } (or template <class T>).

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

In a function template like template <typename T> T myMin(T a, T b), what does T represent?

A

T is a ‘template parameter’ or ‘type parameter’. It acts as a placeholder for an actual data type (e.g., int, double, std::string) that will be specified or deduced when the function is called.

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

How does the compiler typically determine the actual data type for a template parameter T when a function template is called (e.g., myMin(3, 5))?

A

The compiler deduces the type T from the types of the arguments passed to the function. In myMin(3, 5), T would be deduced as int.

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

What happens if you call a function template with arguments of mixed types that the template cannot resolve to a single T, e.g., myMin(3, 5.0) for template <typename T> T myMin(T a, T b)?

A

This can lead to a compilation error because the compiler cannot deduce a single consistent type for T. T would be ambiguous (is it int or double?). You might need explicit template arguments or ensure arguments are of the same type.

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

Can a function template have multiple type parameters? Give a conceptual syntax.

A

Yes. Conceptual syntax: template <typename T1, typename T2> ReturnType function_name(T1 param1, T2 param2) { /* ... */ }.

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

What is a ‘class template’? Describe its basic purpose.

A

A class template is a blueprint for creating a family of classes that have the same structure and member functions but can operate on different data types specified as template parameters. It allows for generic data structures and classes.

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

What is the basic syntax for declaring a class template named MyCollection that can hold elements of a generic type T?

A

template <typename T> class MyCollection { private: T* data; int size; public: /* constructors, methods using T */ };

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

How do you instantiate an object of a class template MyCollection to hold integers?

A

MyCollection<int> intCollection; The actual type (int) is specified within angle brackets.

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

If a member function of a class template is defined outside the class declaration, what special syntax is required for its definition?

A

The member function definition must also be a template and be prefixed with template <typename T> (or whatever template parameters the class uses), and the class name must be qualified with the template parameter, e.g., template <typename T> void MyCollection<T>::add(T item) { /* ... */ }.

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

Why are the definitions of template classes and their member functions (if not defined inline) usually placed entirely in header (.h or .hpp) files?

A

Because the compiler needs the full template definition (not just the declaration) at the point where the template is instantiated with a specific type. If definitions were in a .cpp file, the compiler wouldn’t see them when compiling another .cpp file that uses the template, leading to linking errors.

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

Consider a class template BinLattice<Type> designed to store a 2D lattice where Type can be double for prices or bool for a stopping policy. How does this demonstrate the power of class templates?

A

It allows the same underlying data structure and access logic (e.g., SetNode, GetNode) to be reused for different data types without duplicating the class code. One generic BinLattice definition serves multiple purposes.

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

If BinLattice<Type> has a member std::vector<std::vector<Type>> Lattice;, how does Type affect this member?

A

Type determines the kind of data stored in the innermost vectors. If Type is double, it’s a vector of vectors of doubles. If Type is bool, it’s a vector of vectors of bools.

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

If BinLattice<Type> has a function void SetNode(int n, int i, Type x);, how does the template parameter Type influence this function’s usage?

A

The third parameter x must be of the same type as Type with which the BinLattice object was instantiated. For BinLattice<double> prices;, x must be a double. For BinLattice<bool> policy;, x must be a bool.

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

Can std::vector itself be considered an example of a class template? Explain briefly.

A

Yes, std::vector is a class template from the C++ Standard Library. You use it like std::vector<int>, std::vector<double>, or std::vector<MyClass>, where the type in the angle brackets is the template argument specifying what type of elements the vector will hold.

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

What is ‘generic programming’ and how do templates facilitate it?

A

Generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters. Templates are the C++ mechanism for achieving this, allowing code to be written once and used with various data types.

17
Q

When designing a function template that performs an operation (e.g., comparison with < as in myMin), what is an implicit assumption about the types T that can be used with it?

A

The template assumes that the operations used within its body (like < for myMin, or + if it were a sum template) are valid and defined for any type T with which the template is instantiated. If T doesn’t support those operations, a compile error will occur.