This document contains comprehensive notes from Lecture 15 of the CO2402 course on Advanced Programming with C++. The lecture covers the concepts of function and operator overloading, essential features in C++ that allow multiple functions or operators to have the same name with different parameter...
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:
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller BpoBpo. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for £2.99. You're not tied to anything after your purchase.