Function Overloading
• These functions having the same name but different arguments are known as
overloaded functions.
For example:
// same name different arguments
int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
• Here, all 4 functions are overloaded functions.
• Notice that the return types of all these 4 functions are not the same.
Overloaded functions may or may not have different return types but they
must have different arguments.
• Function pointer:
The function pointer is used to point functions, similarly, the pointers are
used to point variables.
• Syntax:
• return_type (*FuncPtr) (parameter type, ....);
• Ambiguity
Definition: Ambiguity occurs when the compiler cannot determine which
overloaded function to call based on the arguments provided. This can
happen in several scenarios, such as:
• When Types Are Mixed: If you call an overloaded function with arguments
that could match more than one version.
Resolving Ambiguity
• To resolve ambiguity in function overloading:
• Use Different Parameter Types: Ensure that the function signatures differ
sufficiently to avoid confusion.
• Explicit Type Casting: When calling a function, you can cast the arguments
to clarify which overload you intend to use.
Default Function Arguments
• The default arguments are used when you provide no arguments or only few
arguments while calling a function.
• The default arguments are used during compilation of program.
Operator overloading
• What are Operators?
• An operator is a symbol that tells the compiler to perform specific
mathematical, logical calculations or some other special operations.
• What is Operator Overloading in C++?
• In C++, we can make operators work for user-defined classes. This means
C++ has the ability to provide the operators with a special meaning for a
data type, this ability is known as operator overloading. For example, we
can overload the + operator in a class like String so that we can
concatenate two strings by just using +.
• Using operator overloading in C++, we can specify more than one meaning
for an operator in one scope. The purpose of operator overloading is to
provide a special meaning of an operator for a user-defined data type.
Syntax:
Here,
• returnType is the return type of the function.
•the operator is a keyword.
• the symbol is the operator that we want to overload.
Like: +, <, -, ++, etc.
• arguments are the arguments passed to the function.
• The below operators are some built-in operators and these
operators are operates on built-in data types or primitive data
types available in C++. Like additions can be performed on
integer, float, and so on. If we are defining our own data type
like if we are writing a class Matrix.
• Can we overload all operators in C++?
• In C++, almost all operators can be overloaded except a few. Following is
the list of operators that cannot be overloaded in C++.
. (dot)
::
?:
sizeof
• Operator Overloading in Unary Operators (++, –)
• The increment operator ++ and decrement operator — are examples of
unary operators.
• To make ++ work as a postfix we need to use the following syntax.
• void operator ++ (int) {
// code
}
Operator Overloading using Friend Function in C++
• Points to Remember While Overloading Operator using
Friend Function:
• We need to remember the following pointers while
working with Operator Overloading in C++ Using Friend
Function.
• The Friend function in C++ using operator overloading
offers better flexibility to the class.
• The Friend functions are not a member of the class and
hence they do not have ‘this’ pointer.
• When we overload a unary operator, we need to pass one
argument.
• When we overload a binary operator, we need to
pass two arguments.
• The friend function in C++ can access the private
data members of a class directly.
• An overloaded operator friend could be declared
in either the private or public section of a class.
• When redefining the meaning of an operator by
operator overloading the friend function, we
cannot change its basic meaning. For example,
we cannot redefine minus operator + to multiply
two operands of a user-defined data type.
class Complex{
private:
int real; int img;
public:
friend Complex operator + (Complex C1,
Complex C2); };
• Complex operator + (Complex C1, Complex C2)
{
• Complex t;
• t.real = C1.real + C2.real;
• t.img = C1.img + C2.img;
• return t; }
Overloading New and Delete