0% found this document useful (0 votes)
16 views

Chapter 13 Overloading

This document discusses overloading functions and operators in C++. It explains that function overloading allows multiple definitions of the same function name as long as the parameters are different. Operator overloading allows redefining operators like + and - to work on user-defined types. It provides examples of overloading unary and binary operators. The document also discusses the difference between overloading and overriding functions. Finally, it explains the "this" pointer, which allows member functions to access the object they are called on.

Uploaded by

Raksa Ma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Chapter 13 Overloading

This document discusses overloading functions and operators in C++. It explains that function overloading allows multiple definitions of the same function name as long as the parameters are different. Operator overloading allows redefining operators like + and - to work on user-defined types. It provides examples of overloading unary and binary operators. The document also discusses the difference between overloading and overriding functions. Finally, it explains the "this" pointer, which allows member functions to access the object they are called on.

Uploaded by

Raksa Ma
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

OVERLOADING

Chapter 13
C++ Programming
OBJECTIVES:
 Learn about overloading
 Become familiar with the restrictions on operator overloading
 Examine the pointer "this"
 Learn about "friend" functions
 learn how to overload operators as members and nonmembers of a class
 Discover how to overload various operators
 Become familiar with the requirements for classes with pointer member variables
 Learn about templates
 Explore how to construct function templates and class templates
 Become aware of c++ 11 random number generators
OVERLOADING

 Function Overloading: More than one function’s definition in the same


scope.
 Operator Overloading: More than one name for operator in the same scope.
 Declaration statement with the same name but different arguments.

 What’s happen when you call an overloaded function or operator?


Overload Resolution: Find the right definition (depending on the given
arguments).
FUNCTION OVERLOADING

 Definition of overloaded functions should show different


datatype and/or parameters.
 Example:
 Develop data printing function and use it for printing
different values:
OPERATOR OVERLOADING

 Redefine (overload) operators.

 examples of overloaded operators are +, -, /, <<,>>, <=, ==, etc.

 Operator function: The function that overloads an operator.

 The result of an operation is a value.

 Therefore, the operator function is a value returning function.


OPERATOR OVERLOADING

 The syntax of the heading for an operator function is:

returnType operator symbol (arguments) {….}


 “operator” is a reserved word.
 returnType is the return type of the function.
 symbol such as (+, <, -, ++, etc).
 Arguments(parameters) plugged to the function.
OPERATOR OVERLOADING

 C++ consists of both binary and unary operators.

 It also has a Ternary operator (?, :) which cannot be overloaded.

 Unary Operators such as Increment (++) and Decrement (--).

 Unary operators work on single operand (--a, ++a, !true, etc).

 Example: ++ Operator
FUNCTION OVERLOADING VS FUNCTION OVERRIDING:

1. Inheritance: functions Overriding implements when a class derived


from another. Overloading implements without inheritance.
2. Function Structure: Overloaded functions must differ in function
structure, by either number of parameters or type of parameters
should differ. In overriding, function signatures must be same.
3. Scope of functions: Overridden functions are in different scopes;
whereas overloaded functions are in same scope.
POINTER “THIS”

 Sometimes, it is necessary for a member function to refer to the


object as a whole, rather than the object’s individual member
variables.
 Every object of a class maintains a (hidden) pointer to itself, and the
name of this pointer is “this”.
 “this” is a reserved word.
 When an object invokes a member function, the member function
references the pointer “this” of the object.
POINTER “THIS” EXAMPLE
POINTER “THIS” EXAMPLE….CONT..

 Suppose that the definition of the member function doubleDimensions is:


POINTER “THIS” EXAMPLE….CONT..
oldYard.doubleDimensions(20.00, 10.00);
 Consider the following function main:

You might also like