1/33
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What is a function pointer?
variable that stores the memory address of a function
allows a program to call a function indirectly
act as pointers to functions
What are the common uses of function pointers?
implementing callback functions where a function is executed in response to an event
providing custom comparison functions for algorithms such as sorting
creating tables of functions to dynamically select behaviour at runtime
What is the syntax for a function pointer?
Pointer to a function with no arguments:
return_type (*ptrName)();Pointer to a function with arguments:
return_type (*ptrName)(type1, type2);How do you create and call a simple function pointer?
void sayHello()
{
std::cout << "Hello!";
}
void (*fp)() = sayHello;
fp();fp stores the address of sayHello
fp() calls the function indirectly
What is an array of function pointers?
An array that stores multiple function addresses
Allows selecting different functions dynamically at runtime
int (*ops[3])(int, int) = {add, sub, mul};What is a function callback?
when a function is executed in response to an event by passing a function pointer as an argument
void computeSum(int a, int b, void (*callback)(int))
{
int sum = a + b;
callback(sum);
}Calling it:
ComputeSum(4, 7, onDone);What is a functor (function object)?
An object that can be used like a function
Created by defining a class or struct that overloads the operator()
How do you create a functor?
class Square
{
public:
int operator()(int num)
{
return num * num;
}
};Create and call:
Square s;
s(5);What is the difference between a function pointer and a functor?
Function pointer:
Stores the address of an existing function
Functor:
Full object that can store data and behaviour together
Can maintain internal state between calls
When are functors used?
Providing custom operations to algorithms such as:
sorting
filtering
transforming data
When internal state or parameters are needed
How does a functor maintain internal state?
Because functors are objects, they can store data in member variables
class Counter
{
public:
int count;
Counter() : count(0) {}
void operator()()
{
count++;
}
};count changes between calls
What is a lambda expression?
Anonymous function that can be defined directly inside the code
Allows programmers to write short, inline functions
Can capture variables from their surrounding scope
When are lambda expressions used?
When a small function is needed temporarily
What is the syntax of a lambda expression?
[capture] -> optional_return_type
{
function body
}What does the capture list do in a lambda?
Specifies which external variables are accessible inside the lambda
Examples:
[=] capture by value
[&] capture by reference
How is a lambda used with transform?
std::transform(numbers.begin(),
numbers.end(),
squares.begin(),
[](double n)
{
return n * n;
});The lambda calculates the square of each element
Why are files used in programs?
Data stored in RAM exists only while a program is running
Files allow programs to store information permanently on disk so it can be reused later
What is the fstream library?
The fstream library allows programs to:
open
read
write
close files stored on disk
What are the three main fstream classes?
ofstream → write to files
ifstream → read from files
fstream → read and write files
How do you write to a file?
std::ofstream outFile("output.txt");
outFile << id << std::endl;
outFile.close();ofstream writes data to a file
How do you read from a file?
std::ifstream inFile("output.txt");
inFile >> id;
inFile.close();ifstream reads data from a file
Why do we check if a file opened successfully?
To prevent errors when a file cannot be opened
if (!outFile)
{
std::cout << "Error opening file.";
}What is error handling?
Object-oriented programs separate the source of errors from their handling
It uses:
try
throw
catch
What are the steps of exception handling?
A try statement is used
If an exception happens, throw an error
A catch statement handles the exception
What is the purpose of a try block?
Contains code that may cause an exception
try
{
int result = safe_division(10,0);
}What does throw do?
Throws an error when an exception happens
if (quotient == 0)
{
throw "Division by 0 is undefined!";
}What does catch do?
Handles the exception thrown by throw
catch (const char *error)
{
std::cerr << error;
}What are common uses of error handling?
Dynamic Memory Allocation
File stream I/O operations
Networking
Preventing out of bounds issues
How does division by zero error handling work?
int safe_division(int dividend, int quotient)
{
if (quotient == 0)
{
throw "Division by 0 is undefined!";
}
return dividend / quotient;
}Checks for invalid input
Throws an exception if quotient is zero
How do you handle file opening errors using exceptions?
std::ifstream inFile("output.txt");
if (!inFile)
{
throw "Error: output.txt could not be opened.";
}The catch block handles the error
What is the difference between function pointers, functors, and lambdas?
Function pointer: Stores the address of an existing function.
Functor: An object that behaves like a function and can store internal state.
Lambda: An anonymous inline function.
Which STL algorithms commonly use function pointers, functors, and lambdas?
sort
for_each
transform
Why are functors useful compared to normal functions?
Because functors can store internal state and parameters while also behaving like functions
Why are lambda expressions useful?
They make code more concise by allowing short, inline functions without creating separate functions