0% found this document useful (0 votes)
11 views

NU-Lec 3 - Functions Overloading and Recursion

The document discusses object oriented programming concepts like function overloading, which allows functions to have the same name but different parameters, and recursion, which breaks problems into smaller identical subproblems until a base case is reached. Examples of recursion are provided for calculating sums and factorials iteratively and recursively. Dynamic 2D arrays are also covered, where a 2D array is represented using a 1D array of pointers.

Uploaded by

badarfatima43
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

NU-Lec 3 - Functions Overloading and Recursion

The document discusses object oriented programming concepts like function overloading, which allows functions to have the same name but different parameters, and recursion, which breaks problems into smaller identical subproblems until a base case is reached. Examples of recursion are provided for calculating sums and factorials iteratively and recursively. Dynamic 2D arrays are also covered, where a 2D array is represented using a 1D array of pointers.

Uploaded by

badarfatima43
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Object Oriented Programming

Lecture No. 3
Functions Overloading and
Recursion
2

Pointer to Pointer
• The declaration of a pointer-to-pointer looks like
– int **ipp;
• The two asterisks indicate that two levels of pointers are
involved
– e.g.
int i = 5, j = 6; k = 7;
int *ip1 = &i, *ip2 = &j;
ipp = &ip1;
3

Dynamic 2D arrays
4

Dynamic 2D arrays
• At the first level, it points to a block of pointers,
one for each row. That first-level pointer is the
first one we allocate; it has nrows elements, with
each element big enough to hold a pointer-to-int,
or int *.
5

Dynamic 2D arrays
What is function overloading?
• Function overloading is the important characteristic of
OOP.

• Two or more functions are said to be overloaded if they


have the same name but different number of arguments.

• Also two or more functions are said to be overloaded if


they have the same name, same number of arguments but
the type of arguments are different.

• The C++ compiler can easily differentiate between such


functions depend upon the number or type or order of
arguments.
Example-01
• To understand the idea of function overloading consider the following
example.
void draw()
{
for(int i=0;i<45;i++)
cout<<‘+’;
cout<<endl;
}
void draw(char ch)
{
for(int i=0;i<45;i++)
cout<<ch;
cout<<endl;
}
void draw(char ch, int n)
{
for(int i=0;i<n; i++)
cout<<ch;
cout<<endl;
}
void draw()
void draw(char ch)
void draw(char ch, int n)
… No argument
{`
One argument

Two arguments
draw();
draw(‘=’);
draw(‘*’ , 30);

}
Recursion
• An extremely powerful problem-solving technique.

• Breaks a problem into smaller identical problems.

• An iterative solution involves loops. Recursion can be an


alternative to iteration.
Facts about Recursion-Base Case
• A recursive method calls itself.

• Once the problem to be solved is sufficiently small it can


be solved in one statement. This is a special case, the base
case.

• Base case: a known case in a recursive solution, i.e., a case


for which the result is known

• Eventually, one of the smaller problems must be this base


case. A test for the base case enables the recursive calls to
stop.
Example- Sum
Mathematical Examples
• 9 = 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 45
• 11 = 11+ 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 66

Formula
• n = n+(n-1)+(n-2)+(n-3)………3+2+1
Iterative method
int sum(int n)
{
if (n == 0)
{
return 0;
}
int value = 1;
for (int i = n; i > 1; i--)
{
value = value + i;
}
return value;
}
Example- Sum(Cont…..)
Base Case (The case where recursive call stops)
• In sum, the base case is sum(0)=0

Recursive Case
• sum(n) = n + sum(n-1)

Recursive Method
int sum(int n)
{
if (n == 0)
{
return 0;
}
else
{
return n + sum(n - 1);
}
}
Example- Factorial
Mathematical Examples
• 4! = 4 * 3 * 2 * 1 = 24
• 7! = 7 * 6 * 5 * 4 * 3 * 2 * 1 = 5040

Formula
• n! = n*(n-1)*(n-2)*(n-3)………3*2*1
Iterative method
int factorial(int n)
{
if (n <= 1)
{
return 1;
}
int value = 1;
for (int i = n; i > 1; i--)
{
value = value * i;
}
return value;
}
Example- Factorial(Cont…..)
Base Case (The case where recursive call stops)
• In factorial, the base case is factorial(0)=1

Recursive Case
• fact(n) = n * fact(n-1)

Recursive Method
int factorial(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * factorial (n - 1);
}
}
15

Example- Factorial(Cont…..)
Example- Print Reverse
Mathematical Examples
• 4=4 3 2 1
• 7=7 6 5 4 3 2 1

Formula
• ?
Base case
• ?
Recursive case
• ?
Recursive method
• ?
Example- Fibonacci sequence
Mathematical Examples
• The first ten terms in the sequence are:

1,1,2,3,5,8,13,21,34,55

To be done by students..….

You might also like