y
nl
O
se
U
tre
en
Session: 7 C
h
ec
Overloading
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited.
y
nl
Explain Overloading
O
se
List the different forms of Overloading
U
Explain Method Overloading
tre
en
Explain Constructor Overloading
C
h
Explain Operator Overloading
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 7 2
y
nl
O
Overloading describes the way in which a single
se
function can be implemented in several ways
U
tre
en
Meaning or behavior of a function or method can be
changed based on the context in which it is used
C
h
ec
pt
Different forms of overloading are Method overloading,
rA
Constructor overloading, and Operator overloading
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 7 3
y
nl
It is a technique in which a method with the same
O
name can have several implementations by changing
se
its signature.
U
tre
Changing the signature involves change in:
en
Number of parameters
C
h
Type of parameters
ec
Sequence of parameters
pt
rA
Fo
The figure shows class and object.
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 7 4
y
nl
O
It is the ability to define several methods with the
se
same name but with different parameters.
U
tre
The advantage is that it allows methods with similar
en
functionality to be used by a common name.
C
h
ec
pt
The figure shows an object c1 of class Calc accessing
rA
the add(int, float, int) method by passing appropriate
parameters.
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 7 5
y
nl
It is required, to create multiple constructors with
O
different arguments
se
U
tre
en
C
To initialize selected members only, it is required
h
ec
to have a constructor with parameters
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 7 6
y
nl
It is a technique used for extending the functionality of
O
an existing operator to allow them to be used with
se
user-defined types.
U
tre
It is used to:
en
Add the values of two vectors or two complex numbers
C
Multiply matrices or non-arithmetic functions
h
ec
Combine contents of two arrays
pt
The figure depicts that operator overloading can be
rA
used to add strings and objects of a class.
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 7 7
y
nl
This enables the user to define exactly how the
O
operator behaves when used with his own class and
se
other data types.
U
Syntax
tre
en
public static <result-type>
operator <operator> (op-type1
operand1, op-type2 operand2)
C
h
ec
{
pt
// processing statements
rA
}
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 7 8
y
nl
Overloading is a technique in which a method with the same
O
name can have several implementations by changing the
se
number, type, or sequence of parameters.
U
A method in which only the return type or the names of the
tre
parameters have been changed cannot be considered
overloaded.
en
C
A constructor is a special method that has the same name as the
class name and is used to initialize the data members of the
h
ec
class.
pt
Operator overloading is a technique used for extending the
rA
functionality of an existing operator so as to allow them to be
used with user-defined types such as a class.
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 7 9
y
nl
The keyword ‘operator’ is used to implement operator
O
overloading in C#.
se
Operator overloading enables a user to convert data to and from
U
one type to another.
tre
Operator overloading enables a user to perform arithmetic and
en
logical operations on a type with itself or other types.
C
h
ec
pt
rA
Fo
Version 1.0 © 2012 Aptech Limited. Object-Oriented Programming Concepts / Session 7 10