CSC/ECE 517 Fall 2010/ch5 5a KR: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 46: Line 46:


are all overloaded methods. However as Java supports this type of polymorphism via late binding, the decision as to which method to call is deferred until runtime.
are all overloaded methods. However as Java supports this type of polymorphism via late binding, the decision as to which method to call is deferred until runtime.
===<b>Constructor Overloading</b>===
Constructor is used to create instances of that class, and is a special (instance) member function having the same name as the class whose types it is to create. If a class has no explicitly defined constructors then the compiler will generate default constructor implementation for the class.


===<b>Pitfalls of Method Overloading</b>===
===<b>Pitfalls of Method Overloading</b>===

Revision as of 13:28, 2 November 2010

Overloading-Good or Bad?

Introduction

Overloading can be broken down into Operator Overloading and Method/Function Overloading. Method overloading can be considered to be a type of polymorphism in which two methods with the same name are used to perform different functions according to the parameters passed to the methods. Operator overloading can be considered to be a type a polymorphism in which a particular operator is defined to perform another action.

Method/Function Overloading

This concept is seen typically in languages such as C++, Java, Ada. It allows us to create multiple methods with the same name which differ either in the type or number of arguments passed. Consider the following example. These are two definitions of the method test written in Java.

void test()

{

System.out.println(" ");

}


void test(int a)

{

System.out.println("a: " + a);

}


Here to call the first function, we do not pass in any parameter, whereas to call the second one, we pass an integer parameter. The choice as to which of these methods to invoke is decided by looking at the parameter type and number of parameters.


Function Overloading in C++

C++ is one of the language that supports method overloading. Consider the following example.

Method Overloading in Java

The concept is very much similar as in C++. For example:

void print(boolean a) void print(char b) void print(float c)

are all overloaded methods. However as Java supports this type of polymorphism via late binding, the decision as to which method to call is deferred until runtime.


Constructor Overloading

Constructor is used to create instances of that class, and is a special (instance) member function having the same name as the class whose types it is to create. If a class has no explicitly defined constructors then the compiler will generate default constructor implementation for the class.


Pitfalls of Method Overloading

In such an environment, programs containing ambiguity will not compile. The main cause of ambiguity in C++ involves automatic type conversions. Also Consider the following example snippet

void func(int a); void func(int &a);

Here the second call will give an error. This is because the function cannot be overloaded if the only difference between the two parameters is that one is a reference and one is a value.

If overloading is used in excess, it may become difficult to determine which method is called by just reading the code. Also if a class does not have an explicitly defined default constructor and then you try to define another constructor, then you will not be able to make use of the default constructor.


Operator Overloading

References

[1] Andrew Black, Kim. B. Bruce, James Noble. Designing the Next Educational Programming Language

[2] Operator Overloading

[3] Function Overloading

[4] Programming with C++ Part 2