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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 10: Line 10:


=Method/Function Overloading=
=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.
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()
void test()
Line 16: Line 16:
{
{


System.out.println("No parameters");
System.out.println(" ");


}
}
Line 30: Line 30:




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 at compile time by looking at the parameter type and number.
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 at compile time by looking at the parameter type and number of parameters.


A type of method overloading is Constructor Overloading
A type of method overloading is '''Constructor Overloading'''
 
Let us consider Constructor Overloading in different languages:
 
In C++
 
A Constructor is used to create instances of a class. It is an instance member function having the same name as the class whose types it is to create. If there is no constructor explicitly defined in a class, the compiler generates default constructor implementation for the class.


=References=
=References=

Revision as of 12:33, 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 at compile time by looking at the parameter type and number of parameters.

A type of method overloading is Constructor Overloading

Let us consider Constructor Overloading in different languages:

In C++

A Constructor is used to create instances of a class. It is an instance member function having the same name as the class whose types it is to create. If there is no constructor explicitly defined in a class, the compiler generates default constructor implementation for the class.

References

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

[2] Operator Overloading

[3] Function Overloading