C++ Operator Overloading and Templates Flashcards
(14 cards)
What is a friend function in C++?
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
What are the key properties of friendship in C++?
Friendship is NOT inherited
Friendship is NOT transitive
A friend function can be:
- A standalone function
- A member function of another class
What is operator overloading in C++?
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
What are the key restrictions for operator overloading?
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 can you overload binary operators like +?
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 do you overload the «_space;operator for output?
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 «_space;dt.day «_space;’/’ «_space;dt.month «_space;’/’ «_space;dt.year;
return os; // Allow chaining of output
}
What is a function template in C++?
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>
What is a class template in C++?
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 are template instances created?
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
What are the four main components of the C++ Standard Template Library?
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 do you use a vector in C++?
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 do you use a stack in C++?
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
What are some best practices for operator overloading?
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
What should you consider when designing templates?
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