C++ Operator Overloading and Templates Flashcards

(14 cards)

1
Q

What is a friend function in C++?

A

A non-member function

Has access to private and protected members of a class

Declared inside the class with the friend keyword

Not a member of the class it can access

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

What are the key properties of friendship in C++?

A

Friendship is NOT inherited

Friendship is NOT transitive

A friend function can be:
- A standalone function
- A member function of another class

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

What is operator overloading in C++?

A

Providing a new meaning to an existing operator for user-defined types

Allows operators to work with custom classes

Goal: Make code more intuitive and easier to understand

Implemented using special member functions or friend functions

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

What are the key restrictions for operator overloading?

A

At least one operand must be a user-defined type

Cannot change:
- Associativity of operators
- Precedence rules
- Short-circuit evaluation for logical operators

Some operators cannot be overloaded:
., sizeof, ?:, ::, .*

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

How can you overload binary operators like +?

A

class MyInteger {
public:
// Overload + between two MyInteger objects
int operator+(MyInteger other);

// Overload + between MyInteger and int
int operator+(int other);

// Use a friend function to handle int + MyInteger
friend int operator+(int value, MyInteger obj); };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you overload the &laquo_space;operator for output?

A

class MyDate {
private:
int year, month, day;
public:
// Declare as a friend function
friend ostream& operator«(ostream& os, MyDate& dt);
};

// Implementation
ostream& operator«(ostream& os, MyDate& dt) {
os &laquo_space;dt.day &laquo_space;’/’ &laquo_space;dt.month &laquo_space;’/’ &laquo_space;dt.year;
return os; // Allow chaining of output
}

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

What is a function template in C++?

A

A blueprint for creating functions that can work with different data types

Allows writing generic functions that work with multiple types

Syntax:
template<typename>
void myFunction(T x, T y) {
// Generic implementation
}</typename>

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

What is a class template in C++?

A

A blueprint for creating classes that can work with different data types

Allows creating generic data structures

Syntax:
template<typename>
class Stack {
private:
T elements[SIZE];
int top;
public:
void push(T element);
T pop();
};</typename>

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

How are template instances created?

A

Stack<int> intStack; // Creates a stack of integers
Stack<string> stringStack; // Creates a stack of strings</string></int>

intStack.push(42);
stringStack.push(“Hello”);

The compiler generates specific code for each type used

Different template instantiations are treated as separate classes/functions

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

What are the four main components of the C++ Standard Template Library?

A

Containers
Examples: vector, stack, queue
Provide data storage and organization

Iterators
Objects that traverse containers
Allow uniform access to container elements

Algorithms
Generic functions for common operations
Examples: sorting, searching, modifying

Functions
Utility functions and function objects
Provide additional generic programming support

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

How do you use a vector in C++?

A

include <vector></vector>

std::vector<int> v;
v.push_back(43); // Add elements
v.push_back(99);
std::cout << v.size(); // Get number of elements
std::cout << v[1]; // Access second element</int>

Dynamically resizable array
Provides easy element addition and access
Uses operator[] for element access

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

How do you use a stack in C++?

A

include <stack></stack>

std::stack<int> s;
s.push(43); // Add element to top
s.push(99);
std::cout << s.top(); // Access top element
s.pop(); // Remove top element
std::cout << s.size(); // Get number of elements</int>

Last-In-First-Out (LIFO) data structure
Provides basic stack operations
Template-based, works with any data type

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

What are some best practices for operator overloading?

A

Only overload operators when they make logical sense

Maintain expected behavior of operators

Keep the meaning intuitive

Follow the “law of least surprise”

Example good practices:
- myString + yourString could concatenate strings
- myDate++ could increment a date
- a[i] could access array elements

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

What should you consider when designing templates?

A

Create generic implementations that work with multiple types

Be aware of potential performance implications

Ensure type compatibility

Use constraints (in modern C++) to limit acceptable types

Prefer templates over multiple similar implementations

Use the Standard Template Library when possible

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