Unit 2 - C++
Unit 2 - C++
Unit 2 - C++
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 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 main()
{
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
Here,
#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
Binary Arithmetic +, -, *, /, %
De-referencing (->)
Subscript []
Function call ()
Logical &, | |, !
Operators that can be overloaded Examples
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.
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.
1. Implicit 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;
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