with C++ Lecture 15
Introduction to Overloading
1. Concept of Overloading
o Overloading refers to using the same name for more than one
operation or function. In C++, this includes function
overloading and operator overloading.
Example:
2. Difference Between Overloading and Overriding
o Overloading: Same name, different parameter types or
number of parameters.
o Overriding: Same name, same parameters in a base and
derived class, using the virtual keyword.
Operator Overloading
1. Definition and Syntax
o Operators can be overloaded to work with user-defined types,
such as classes. The syntax for overloading an operator
involves defining a function with the keyword operator
followed by the operator symbol.
Example:
, 2. Overloadable Operators
o Most operators in C++ can be overloaded, including
arithmetic operators (+, -, *, /), relational operators (==, <,
>), and more. However, certain operators, like . (member
access), :: (scope resolution), and ?: (ternary), cannot be
overloaded.
Example: The CTwoInts Class
1. Class Definition
o The CTwoInts class includes member variables mX and mY,
representing two integers. It demonstrates overloading
assignment and arithmetic operators.
Example:
2. Overloading the Assignment Operator (=)
o The assignment operator is overloaded to assign the values of
one CTwoInts object to another.
Example: