You are on page 1of 9

UNIT 2

Function Overloading:
Function overloading is a feature of object-oriented programming where two or
more functions can have the same name but different parameters.
When a function name is overloaded with different jobs it is called Function
Overloading.
In Function Overloading “Function” name should be the same and the arguments
should be different.
Function overloading can be considered as an example of a polymorphism feature in
C++.
The parameters should follow any one or more than one of the following conditions for
Function overloading:
 Parameters should have a different type (but same number of parameters)
add(int a, int b)
add(double a, double b)
 Parameters should have a different number (but can have same type)
add(int a, int b)
add(int a, int b, int c)

Example:
#include <iostream.h>

void add(int a, int b)


{
cout << "sum = " << (a + b);
}

void add(int a, int b, int c)


{
cout << endl << "sum = " << (a + b + c);
}

void main()
{
add(10, 2);
add(5, 6, 4);
}

Output
sum = 12
sum = 15
How does Function Overloading work?
 Exact match:- (Function name and Parameter)
 If a not exact match is found:–
->Char, Unsigned char, and short are promoted to an int.
->Float is promoted to double
 If no match is found:
->C++ tries to find a match through the standard conversion.
 ELSE ERROR

Default Arguments
A default argument is a value provided in a function declaration that is
automatically assigned by the compiler if the calling function doesn’t provide a value for
the argument.
Example:
#include <iostream.h>

void sum(int x, int y, int z = 0, int w = 0)


{
return (x + y + z + w);
}

void main()
{

cout << sum(10, 15) << endl;


cout << sum(10, 15, 25) << endl;
cout<<sum(10,20,30,20)<<endl;
}

Output
25
50
80
Advantages of Default Arguments:
 Default arguments are useful when we want to increase the capabilities of an existing
function as we can do it just by adding another default argument to the function.
 It helps in reducing the size of a program.
 It provides a simple and effective programming approach.
 Default arguments improve the consistency of a program.

Constructor Overloading
 We can have more than one constructor in a class with same name, as long as each has a
different list of arguments.
 This concept is known as Constructor Overloading and is quite similar to function
overloading.

 Overloaded constructors essentially have the same name (exact name of the class) and
different by number and type of arguments.
 A constructor is called depending upon the number and type of arguments passed.
 While creating the object, arguments must be passed to let compiler know, which
constructor needs to be called.

Example:
#include <iostream.h>
class add
{

public:
float sum;
construct()
{
sum=0
cout<<sum;
}

construct(int a, int b)
{
sum = a +b;
cout<<sum;
}
};

void main()
{
construct o;
construct o( 10, 20);
}

Output:
0
200

Operator Overloading

 Operator overloading is a compile-time polymorphism.


 C++ has the ability to provide the operators with a special meaning for a data type,
this ability is known as operator overloading.
To overload an operator, we use a special operator function.
class className
{
... .. ...
public
returnType operator symbol (arguments) {
... .. ...
}
... .. ...
};

Here,

 returnType is the return type of the function.


 operator is a keyword.
 symbol is the operator we want to overload. Like: +, <, -, ++, etc.
 arguments is the arguments passed to the function.

#include <iostream>
using namespace std;

class Complex {
private:
int real, imag;

public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
Complex operator+(Complex const& obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print()
{
cout << real << " + i" << imag << '\n'; }
};

void main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
}

Output:

12 + i9

Operators that can be Overloaded in C++


We can overload
 Unary operators
 Binary operators
 Special operators ( [ ], (), etc)
Operators that can be overloaded Examples

Binary Arithmetic +, -, *, /, %

Unary Arithmetic +, -, ++, —

Assignment =, +=,*=, /=,-=, %=

Bitwise & , | , << , >> , ~ , ^

De-referencing (->)

Dynamic memory allocation,


New, delete
De-allocation

Subscript []

Function call ()

Logical &, | |, !
Operators that can be overloaded Examples

Relational >, < , = =, <=, >=

Overloading an operator as a friend function


In this approach, the operator overloading function must be preceded by
the friend keyword, and declare the function in the class scope. Keeping in mind, the friend
operator function takes two parameters in a binary operator and varies one parameter in a
unary operator.
 The Friend function in C++ using operator overloading offers better flexibility to the
class.
 The Friend functions are not a member of the class and hence they do not have ‘this’
pointer.
 When we overload a unary operator, we need to pass one argument.
 When we overload a binary operator, we need to pass two arguments.
 The friend function in C++ can access the private data members of a class directly.

Output
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int img;
public:
Complex (int r = 0, int i = 0)
{
real = r;
img = i;
}
void Display ()
{
cout << real << "+i" << img;
}
friend Complex operator + (Complex c1, Complex c2);
};
Complex operator + (Complex c1, Complex c2)
{
Complex temp;
temp.real = c1.real + c2.real;
temp.img = c1.img + c2.img;
return temp;
}
int main ()
{
Complex C1(5, 3), C2(10, 5), C3;
C1.Display();
cout << " + ";
C2.Display();
cout << " = ";
C3 = C1 + C2;
C3.Display();
}

Explanation: The operator function is implemented outside of the class scope by declaring
that function as the friend function.
In these ways, an operator can be overloaded to perform certain tasks by changing the
functionality of operators.

Overloading the Comma Operator:


we can overload the comma operator using Operator Overloading. For
Example: For “Send the query X to the server Y and put the result in variable Z”,
the “and” plays the role of the comma. The comma operator (, ) is used to isolate two or
more expressions that are included where only one expression is expected. When the set of
expressions has to be solved for operands, only the rightmost expression is considered.

Examples:
Input: x = (y = 5, y + 2)
Output: x = 7, y = 5

Explanation:
In the above expression:
It would first assign the value 5 to y, and then assign y + 2 to variable x.
So, at the end ‘x’ would contain the value 7 while variable ‘y’ would contain value 7.

C++ Type Conversion


C++ allows us to convert data of one type to that of another. This is known as type
conversion.

There are two types of type conversion in C++.

1. Implicit Conversion

2. Explicit Conversion (also known as Type Casting)

Implicit Type Conversion

The type conversion that is done automatically done by the compiler is known as implicit
type conversion. This type of conversion is also known as automatic conversion.
Example:

include <iostream>

void main() {

int a = 9;

double b;

b = a;

cout << "num_int = " << a << endl;

cout << "num_double = " << b << endl;

return 0;

Output
num_int = 9
num_double = 9.99

Explicit Conversion
When the user manually changes data from one type to another, this is known as explicit
conversion. This type of conversion is also known as type casting.
The syntax for this style is:

(data_type)expression;

Example:

#include <iostream>
void main() {
double a;
cout << "num_double = " << a << endl;
int b = (int)a;
cout << "num_int = " << b << endl;
}

Output:
num_double = 3.56
num_int = 3

You might also like