Overloading in C++

There are two types of Overloading in C++

1.Function Overloading2.Operator Overloading

Function Overloading in C++

C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as the parameter types or the number of parameters or the order of the parameters of the order of the parameter types are concerned). This capability is called function overloading. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types, and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types. For example, many functions in the math library are overloaded for different numeric data types

Overloading functions that perform closely related tasks can make programs more readable and understandable

The overloaded function is distinguished by their signatures a signature is a combination of a function’s name and its parameter types. The compiler encodes each function identifier with the number and types of its parameters to enable type-safe linkage. Type-safe linkage ensures that the proper overloaded function is called and that the types of arguments conform to the types of the parameters.

Examples for Overloading function in C++

#include<iostream>
using std::cout;
using std::endl;
int square(int x)
{
cout<<"Called square with int argument:"<<x<<endl;
return x*x;
}
double square(double y)
{
cout<<"Called square with double argument:"<<y<<endl;
return y*y;
}
int main()
{
int intResult=square(7);
double doubleResult=square(7.5);
cout<<"\nThe square of integer 7 is"<<intResult
    <<"\nThe square of double 7.5 is"<<doubleResult
    <<endl;
return 0;
}

OUTPUT:

Called square with int argument:7
Called square with double argument:7.5

The square of integer 7 is 49
The square of double 7.5 is 56.25 

Creating overloaded functions with identical parameter lists and different return types is a syntax error

Name mangling to enable type-safe linkage

int square(int x)
{
return x*x;
}
double square(double y)
{
return y*y;
}
void nothing1(int a,float b,char c,int *d)
{
//empty function body
}
char *nothing2(char a,int b,float *c,double *d)
{
return 0;
}
int main()
{
return 0;
}

2.Function volume() is overloaded three times

#include<iostream>
using namespace std;
//Declaration(prototype)
int volume(int);
double volume(double,int);
long volume(long,int,int);
int main()
{
count<<volume(10)<<"\n";
count<<volume(2.5,8)<<"\n";
count<<volume(100,75,15)<<"\n";
return 0;
}
//Function definitions
int volume(int s) //cube
{
return(sss);
}
double volume(double r,int h)   //cylinder
{
return(3.14519rrh); } long volume(long l,int b,int h)  //rectangular box { return(lb*h);
}

OUTPUT:

1000
157.26
112500

Overloading Operator in C++

An overloaded operator built into C++ is operator <<, which is used both as the stream-insertion operator and as the bitwise left-shift operator. Similarly, >> is also overloaded; it is used both as the stream-extraction operator and as the bitwise right-shift operator. Both of these operators are overloaded in the C++ class library. The C++ language itself overloads + and -. These operators perform differently, depending on their context in integer arithmetic, floating-point arithmetic, and pointer arithmetic.

C++ enables the programmer to overload most operators to be sensitive to the context in which they are used the compiler generates the appropriate code based on the context. Some operators are overloaded frequently, especially the assignment operator and various arithmetic operators such as + and -.The jobs performed by explicit function calls, but the operator notation is often clearer and more familiar to programmers.

The syntax for operator overloading in C++ is given below :

ReturnType classname :: Operator OperatorSymbol(argument list)
{
//Function body
}

Fundamentals of Operator Overloading in C++

C++ programming is a type-sensitive and type-focused process. Programmers can use the built-in types and can define new types. The built-in types can be used with C++’s rich collection of operators. Operators provide programmers with a concise notation for expressing the manipulation of objects of built-in types.

Programmers can use operators with user-defined types as well. Although C++ does not allow new operators to be created, it does allow most existing operators to be overloaded so that, when these operators are used with objects, the operators have to mean appropriate to those objects. This is one of C++’s most powerful features

Operator overloading contributes to C++’s extensibility, one of the language’s most appealing attributes.Use operator overloading when it makes a program clearer than accomplishing the same operations with explicit function calls. Overloaded operators should mimic the functionality of their built-in counterparts for-examples, the + operator overloading, as this can make a program cryptic and difficult to read.

Although the operator overloading sounds like an exotic capability, most programmers implicitly use overloaded operators regularly. For example, the addition operator (+) operators quite differently on integers, floats, and doubles. But addition nevertheless works fine with variables of type int, float, double and a number of other built-in types, because the addition operator(+) has been overloaded in the C++ language itself.

Operators are overloaded by writing a function definition(with a header and body) as you normally would, except that the function name now becomes the keyword operator followed by the symbol for the operator being overloaded. For example, the function named operator+ would be used on class objects, that operator must be overloaded with two exceptions. The assignment operator(=) may be used with every class without explicit overloading. The default behavior of the assignment operator is a memberwise assignment of the data members of the class. The address operator (&) may also be used with objects in any class without overloading; it simply returns the address of the object in memory. The address operator can also be overloaded.

The operator overloading is not automatic, however; the programmer must write operator-overloading functions to perform the desired operations. Sometimes these functions are best-made member functions; sometimes they are best as friend functions; occasionally they can be made non-member, non-friend function

Restrictions on Operator Overloading in C++

Operators that can be overloaded

Operators can be overloaded

Operators that cannot be overloaded

Operators that cannot be overloaded

The precedence of an operator cannot be changed by overloading. This can lead to awkward situations in which an operator is overloaded in a manner for which its fixed precedence is inappropriate. However, parentheses can be used to force the order of evaluation of overloaded operators in an expression. The associativity of an operator cannot be changed by overloading.

Assuming that overloading an operator such as + overloads related operators such as += or that overloading == overloads a related operator like != can lead to errors. Operators can be overloaded only explicitly; there is no implicit overloading

Translate »