1/25
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What is function overloading?
Multiple functions with the same name but different parameter lists
How can overloaded functions differ?
Different number of parameters and/or different parameter types
What type of polymorphism is function overloading?
Compile-time (static) polymorphism
Is the return type part of a function signature?
No
What does the compiler use to distinguish overloaded functions?
Their function signatures (name + parameter types)
What is operator overloading?
Redefining operators so they work with user-defined types
Why is operator overloading used?
To make objects behave more like built-in types and improve readability
What keyword is used to overload an operator?
operator
Which object calls an overloaded operator member function?
The object on the left side of the operator
What expression is equivalent to a + b?
a.operator+(b)
What is the general syntax for an overloaded operator member function?
ReturnType operatorOP(const ClassName& other) const
{
// OP = operator(+, -, *)
}What is the syntax for overloading +?
ClassName operator+(const ClassName& other) const
{
return ClassName(member + other.member, member2 + other.member);
}What is the syntax for overloading -?
ClassName operator-(const ClassName& other) const
{
return ClassName(member - other.member, member2 - other.member2);
}What is the syntax for overloading * (object × scalar)?
ClassName operator*(double scalar) const
{
return ClassName(member * scalar, member2 * scalar);
}What is usually returned by overloaded arithmetic operators (+, -, *,)?
A new object containing the result
What is a friend function?
A non-member function that can access private and protected members of a class
How is a friend function declared?
friend ReturnType functionName(...);Where is a friend function declared?
Inside the class declaration
Is friendship reciprocal?
no
Why are friend functions commonly used with operator overloading?
To allow non-member operators to access private data
Why is operator<< usually implemented as a friend function?
Because the left operand is std::ostream, not the class object
What is the syntax for overloading <<?
friend std::ostream& operator<<(
std::ostream& os,
const ClassName& obj)
{
// output members
return os;
}Why must operator<< return std::ostream&?
To allow chained output operations
std::cout << a << b;
Why might multiplication be overloaded as a friend function?
To allow a scalar on the left side
2.0 * v
What is the syntax for scalar × object multiplication?
friend ClassName operator*(
float scalar,
const ClassName& obj)
{
return ClassName(...);
}What are the rules of function overloading?
must share the exact same name
must have different parameter lists
cannot differ only by return type