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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(45 intermediate revisions by the same user not shown)
Line 5: Line 5:
=Introduction=
=Introduction=


Overloading can be broken down into  
[[Image:pic_1.jpg]]
[http://en.wikipedia.org/wiki/Operator_overloading/ Operator Overloading] and  
 
[http://en.wikipedia.org/wiki/Function_overloading/ 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.
Overloading is a type of polymorphism [[#References|[1]]] which allows a method or operator to have different meanings, depending on the context. It thus leads to more readable and usable code. Overloading can be broken down into Operator overloading [[#References|[2]]] and Method/Function Overloading [[#References|[3]]]. 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 of polymorphism in which a particular operator can be defined in different ways to perform different actions.


=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 written in Java.
This concept is seen typically in languages such as C++, Java [[#References|[4]]] and Ada [[#References|[5]]]. To overload a method, the different methods must differ either in the number or types of arguments passed, or both. Consider the following example. There are three definitions of the method test written in Java.


void test()
void test()
Line 26: Line 26:
}
}


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.
double test(double a)
{
 
System.out.println("double a: " + a);


===Function Overloading in C++ ===
return 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 third method has a different return type (double as opposed to void). However, the return type alone is not enough to differentiate between two overloaded methods. The choice as to which of these methods to invoke is decided by looking at the parameter type and number of parameters. Overloading by return type of the methods alone may cause ambiguity in languages such as C++. However, it may be possible to resolve this by using a cast of some sort.
 
 
Consider the following example in C++


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




Line 66: Line 75:
Product of two whole numbers: 42
Product of two whole numbers: 42


===<b>Method Overloading in Java</b>===


The concept is very much similar as in C++. For example:
In Java, the concept is very much similar as in C++. For example:


void print(boolean a)
void print(boolean a)
Line 81: Line 89:
===<b>Constructor Overloading</b>===
===<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. One can also overload constructors. We shall leave that for further reading.
Constructor[[#References|[6]]] 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. One can also overload constructors i.e have constructors with the same name but different type or number of arguments. We shall leave that for further reading[[#References|[7]]].


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


It is always convenient to have same names for similar method capabilities rather than having long, complicated ones. Also, it allows to keep interface consistency and logically call the same method regardless of the data types passed in.
It is always convenient to have same names for similar method capabilities rather than having long, complicated ones.
Other advantages include saving memory space, consistency and readability.
 
Also, it allows to keep interface consistency and logically call the same method regardless of the data types passed in.
 
• Internal processing of the system is hidden from the external user.
 
• Allows the user to achieve compile time polymorphism
 
Other advantages include saving memory space, consistency and readability.


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


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
Programs containing ambiguity will not compile. The main cause of ambiguity involves automatic type conversions. Also Consider the following example snippet


void func(int a);
void func(int a);
Line 98: Line 113:
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.
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.
If overloading is used in excess, it may become difficult to determine which method is called by just reading the code.
 
• 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=
=Operator Overloading=
Line 104: Line 121:
This concept is seen in languages such as C++.  Observation of its use in C++ has proved that operator overloading makes code almost impossible to maintain. Also consider the following example.
This concept is seen in languages such as C++.  Observation of its use in C++ has proved that operator overloading makes code almost impossible to maintain. Also consider the following example.


b.set(1, 0);  
b.set(1, 0);
a = b;  
a = b;  
b.set(2, 0);
b.set(2, 0);
assert( !a.Equals(b) );
assert( !a.Equals(b) );


C++ copies the value so the assert will result in ' not equal'. On the other hand Java does a reference copy, as is defined for the operator '='. So a and b are now referring to the same value.  Hence the assert will result in 'equal', which is confusing.
C++ copies the value so the assert will result in ' not equal'. On the other hand Java does a reference copy, as is defined for the operator '='. So a and b are now referring to the same value.  Hence the assert will result in 'equal', which is confusing.
Line 113: Line 134:
Due to these facts, operator overloading was chosen to be absent in Java.  
Due to these facts, operator overloading was chosen to be absent in Java.  


===<b>Operator Overloading in C++</b>===
The following figure shows some of the operators that can be overloaded in C++


Here most of the commonly used operators can be overloaded by defining functions for the same. For this purpose, we create what are known as operator functions. The following example overloads the '+' operator to add complex numbers.
 
[[Image:operator_1.jpg]]
 
 
In C++ most of the commonly used operators can be overloaded by defining functions for the same. For this purpose, we create what are known as operator functions. The following example overloads the '+' operator to add complex numbers.


struct Complex {
struct Complex {
Line 143: Line 168:
===<b>Benefits of Operator Overloading</b>===
===<b>Benefits of Operator Overloading</b>===


Operators can be defined in such a way that they resemble built in types
• A single operator can be used with different classes of objects, which can simplify a program's code.


• Operators can be defined in such a way that they resemble built in types.
• An operator is not limited to work only with primitive data types.


===<b>Drawbacks of Operator Overloading</b>===
===<b>Drawbacks of Operator Overloading</b>===


Poor use leads to confusing code.
Poor use leads to confusing code (Say we overload the '+' operator to perform '-' operation)


Classes in general do not have these overloaded semantics. So some of these overloaded operators may be ambiguous.
Classes in general do not have these overloaded semantics. So some of these overloaded operators may be ambiguous.


=Conclusion=
=Conclusion=


Using the above mentioned points, one cannot certainly claim that operator overloading and method overloading are indeed beneficial or harmful. Both can lead to confusion is not used in the right manner. It may be the decision the developers of an Object Oriented Language and depending on how they intend it to work, whether to include these features in a particular Language.
Using the above mentioned points, one cannot certainly claim that operator overloading and method overloading are indeed beneficial or harmful. Both can lead to confusion if not used in the right manner. It may be the decision of the developers of an Object Oriented Language and depending on how they intend it to work, whether to include these features in a particular Language.


=References=
=References=


[http://gracelang.org/documents/panel.pdf ] Andrew Black, Kim. B. Bruce, James Noble. Designing the Next Educational Programming Language
[[#References|[1]]] [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming Polymorphism in Object Oriented Programming]
 
[[#References|[2]]] [http://en.wikipedia.org/wiki/Operator_overloading Operator Overloading]
 
[[#References|[3]]] [http://en.wikipedia.org/wiki/Function_overloading Function Overloading]
 
[[#References|[4]]] [http://www.java.com Java]
 
[[#References|[5]]] [http://en.wikipedia.org/wiki/Ada_%28programming_language%29 Ada]
 
[[#References|[6]]] [http://en.wikipedia.org/wiki/Constructor_%28object-oriented_programming%29 Constructors]
 
[[#References|[7]]] [http://en.wikipedia.org/wiki/Function_overloading#Constructor_overloading Constructor Overloading]


[http://en.wikipedia.org/wiki/Operator_overloading] Operator Overloading
[[#References|[8]]] [http://gracelang.org/documents/panel.pdf  Andrew Black, Kim. B. Bruce, James Noble. Designing the Next Educational Programming Language]


[http://en.wikipedia.org/wiki/Function_overloading] Function Overloading
[[#References|[9]]] [http://www.dauniv.ac.in/downloads/c++/C++2.pdf  Programming with C++ Part 2]


[http://www.dauniv.ac.in/downloads/c++/C++2.pdf] Programming with C++ Part 2
[[#References|[10]]] [http://www.java-samples.com/showtutorial.php?tutorialid=284 Method Overloading in Java]


[http://www.java-samples.com/showtutorial.php?tutorialid=284] Method Overloading in Java
[[#References|[11]]] [http://en.allexperts.com/q/C-1040/constructor-overloading.htm C++:constructor overloading]


[http://www.wellho.net/resources/ex.php4?item=c234/cube.cpp] Overloading Operator C++
[[#References|[12]]] [http://www.wellho.net/resources/ex.php4?item=c234/cube.cpp Overloading Operator C++]

Latest revision as of 14:38, 23 November 2010

Overloading-Good or Bad?

Introduction

Overloading is a type of polymorphism [1] which allows a method or operator to have different meanings, depending on the context. It thus leads to more readable and usable code. Overloading can be broken down into Operator overloading [2] and Method/Function Overloading [3]. 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 of polymorphism in which a particular operator can be defined in different ways to perform different actions.

Method/Function Overloading

This concept is seen typically in languages such as C++, Java [4] and Ada [5]. To overload a method, the different methods must differ either in the number or types of arguments passed, or both. Consider the following example. There are three definitions of the method test written in Java.

void test()

{ System.out.println(" "); }

void test(int a) {

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

}

double test(double a) {

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

return 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 third method has a different return type (double as opposed to void). However, the return type alone is not enough to differentiate between two overloaded methods. The choice as to which of these methods to invoke is decided by looking at the parameter type and number of parameters. Overloading by return type of the methods alone may cause ambiguity in languages such as C++. However, it may be possible to resolve this by using a cast of some sort.


Consider the following example in C++


class arith {

public:

void calc(int num1)

{ cout<<"Square of a given number: " <<num1*num1 <<endl; }

void calc(int num1, int num2 )

{ cout<<"Product of two whole numbers: " <<num1*num2 <<endl; } };


int main() {

   arith a;
   a.calc(5);
   a.calc(6,7);

}

In the above example, the function calc is overloaded. In the class arith there are two functions ‘calc’ with the same name. The first one takes an integer as parameter and prints the square of the number. The second calc function takes two integer numbers as parameters, multiplies the numbers and prints the product. So the output of the above example would be

Square of a given number: 25

Product of two whole numbers: 42


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[6] 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. One can also overload constructors i.e have constructors with the same name but different type or number of arguments. We shall leave that for further reading[7].

Advantages of Method Overloading

• It is always convenient to have same names for similar method capabilities rather than having long, complicated ones.

• Also, it allows to keep interface consistency and logically call the same method regardless of the data types passed in.

• Internal processing of the system is hidden from the external user.

• Allows the user to achieve compile time polymorphism

• Other advantages include saving memory space, consistency and readability.

Pitfalls of Method Overloading

Programs containing ambiguity will not compile. The main cause of ambiguity 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.

• 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

This concept is seen in languages such as C++. Observation of its use in C++ has proved that operator overloading makes code almost impossible to maintain. Also consider the following example.

b.set(1, 0);

a = b;

b.set(2, 0);

assert( !a.Equals(b) );


C++ copies the value so the assert will result in ' not equal'. On the other hand Java does a reference copy, as is defined for the operator '='. So a and b are now referring to the same value. Hence the assert will result in 'equal', which is confusing.

Due to these facts, operator overloading was chosen to be absent in Java.

The following figure shows some of the operators that can be overloaded in C++



In C++ most of the commonly used operators can be overloaded by defining functions for the same. For this purpose, we create what are known as operator functions. The following example overloads the '+' operator to add complex numbers.

struct Complex {

  Complex( double r, double i ) : re(r), im(i) {}
  Complex operator+( Complex &other );
  void Display( ) {   cout << re << ", " << im << endl; }

private:

  double re, im;

};


Complex Complex::operator+( Complex &other ) {

  return Complex( re + other.re, im + other.im );

}

int main() {

  Complex a = Complex( 1.2, 3.4 );
  Complex b = Complex( 5.6, 7.8 );
  Complex c = Complex( 0.0, 0.0 );
  c = a + b;
  c.Display();

}


Benefits of Operator Overloading

• A single operator can be used with different classes of objects, which can simplify a program's code.

• Operators can be defined in such a way that they resemble built in types.

• An operator is not limited to work only with primitive data types.

Drawbacks of Operator Overloading

• Poor use leads to confusing code (Say we overload the '+' operator to perform '-' operation)

• Classes in general do not have these overloaded semantics. So some of these overloaded operators may be ambiguous.

Conclusion

Using the above mentioned points, one cannot certainly claim that operator overloading and method overloading are indeed beneficial or harmful. Both can lead to confusion if not used in the right manner. It may be the decision of the developers of an Object Oriented Language and depending on how they intend it to work, whether to include these features in a particular Language.

References

[1] Polymorphism in Object Oriented Programming

[2] Operator Overloading

[3] Function Overloading

[4] Java

[5] Ada

[6] Constructors

[7] Constructor Overloading

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

[9] Programming with C++ Part 2

[10] Method Overloading in Java

[11] C++:constructor overloading

[12] Overloading Operator C++