slide1
Download
Skip this Video
Download Presentation
Object Oriented Programming Spring - 2012

Loading in 2 Seconds...

play fullscreen
1 / 27

Object Oriented Programming Spring - 2012 - PowerPoint PPT Presentation


  • 76 Views
  • Uploaded on

COMSATS Institute of Information Technology. Object Oriented Programming Spring - 2012. Functions OOP in C++ by Robert Lafore - Chapter#5 Kaleem Ullah [email protected] Overloaded functions. Same function name Perform different operations for different types of data

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Object Oriented Programming Spring - 2012' - carson


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1
COMSATS Institute of Information Technology

Object Oriented ProgrammingSpring - 2012

Functions

OOP in C++ by Robert Lafore - Chapter#5

KaleemUllah

[email protected]

overloaded functions
Overloaded functions
  • Same function name
  • Perform different operations for different types of data
  • different data passed
  • Example: same name, different number of arguments
    • void repchar(); //declarations
    • void repchar(char);
    • void repchar(char, int);
overloaded functions1
Overloaded functions

int main()

{

repchar();

repchar(‘=’);

repchar(‘+’, 30);

return 0;

}

overloaded functions2
Overloaded functions

// repchar()

// displays 45 asterisks

void repchar()

{

for(int j=0; j<45; j++) // always loops 45 times

cout << ‘*’; // always prints asterisk

cout << endl;

}

overloaded functions3
Overloaded functions
  • // displays 45 copies of specified character
  • void repchar(char ch)
  • {
  • for(int j=0; j<45; j++) // always loops 45 times
  • cout << ch; // prints specified character
  • cout << endl;
  • }
overloaded functions4
Overloaded functions
  • // displays specified number of copies of specified character
  • void repchar(char ch, int n)
  • {
  • for(int j=0; j
  • cout << ch; // prints specified character
  • cout << endl;
  • }
overloaded functions different kinds of arguments
Overloaded functions: Different kinds of arguments
  • struct Distance
  • {
  • int feet;
  • float inches;
  • };

Void disp( Distance ); //declarations

Void disp( float );

overloaded functions different kinds of arguments1
Overloaded functions: Different kinds of arguments
  • struct Distance
  • {
  • int feet;
  • float inches;
  • };

Void disp( Distance ); //declarations

Void disp( float );

recursion
Recursion

What is recursion?

Recursion

See “Recursion”.

Recursion

If you still don't get it, see "Recursion"

recursion1
Recursion

Void fun()

{

cout<<“this is fun”<

fun();

}

recursion2
Recursion

Void fun()

{

cout<<“this is fun”<

fun();

}

Example of program to print numbers in descending order?

When will this end?

recursion3
Recursion

Example of program to print numbers in descending order?

int print (int n)

{

cout<< n<

if (n>1)

print(--n);

else

return 1;

}

void main ()

{

print (10); // start recursion by calling recursive function

}

properties of recursive functions
Properties of recursive functions
  • Base Case(s): condition for terminating the recursive process
  • Recursive Case(s): set of rules to break the problem into smaller sub-problems to reach base case
    • Divide the problem into smaller sub-problems
    • Solve the sub-problems
    • Combine results to get answer

Sub-problems solved as a recursive call to the same function

slide15
Need of Base Case and Recursive Case

int loop(int x)

{

return (1 + loop(x))

}

  • Recursive function with
  • no base case
  • not a valid recursive case
  • Trace Table with x=5

Problem not being divided into smaller problems – no termination

loop 5

1 + loop 5

1 + loop 5

infinite loop – no termination

power function
Power function
  • Lets figure out a recursive function for calculating the Powers of a given number
  • 2nd power function
  • Square of x = x*x
  • 3rd power function
  • Cube of x = x*x*x
  • 4th power function
  • Fourth Power of x = x*x*x*x
power function1
Power function

x4= x*x*x*x = x*( x*x*x ) =x*x3

x5= x*x*x*x*x = x*(x*x*x*x ) =x*x4

x6= x*x*x*x*x*x = x*(x*x*x*x*x ) =x*x5

In general

  • xn = x*xn-1

Int power (int x, int n)

{

return x * power (x, n-1)

}

power function2
Power Function

Int power (int x, int n)

{

return x * power (x, n-1)

}

Trace table

x * power (x, n-1)

We know 20=1

Base case: if n==0 return 1

We need to stop here

When does it stop ?

slide19
Revised Power Function

Int power (int x, int n)

{

If (n==0)

return 1;

else

return x * power (x, n-1)

}

Base case

Trace table: Calc 23: x=2, n=3

Power (2,3)

2* power(2,2)

2* 2* power(2,1)

2*2* 2*power(2,0)

=8

4

2

1

Recursive case

Result: 8

sub-problems must be “smaller” than the original problem otherwise the recursion never terminates.

factorial function
Factorial function

Factorial 0! = 1

1! = 1

2! = 2 * 1 = 2

3! = 3 * 2 * 1 = 6

4! = 4 * 3 * 2 * 1 = 24

factorial function1
Factorial function

0! = 1

1! = 1

2! = 2 * 1 = 2

3! = 3 * 2 * 1 = 6

4! = 4 * 3 * 2 * 1 = 24

1*0!

2*1!

  • 3*2!
  • 4*3!
  • ……
  • In general: n!=n*(n-1)!

Recursive case: Factorial(n)=n*factorial(n-1)

Base case:

0!=1 i.e; if (n==0) return 1

factorial function2
Factorial function
  • Int factorial (int n)
  • {
  • If (n==0)
  • return 1;
  • else
  • return n * factorial (n-1)
  • }

Trace table: Calc 4! here n=4

factorial (4)

4* factorial (3)

4* 3* factorial (2)

4*3* 2* factorial (1)

4*3*2* 1* factorial (0)

=24

6

2

1

1

factorial function3
Factorial function

Version Action Argument or Return Value

1 Call 5

2 Call 4

3 Call 3

4 Call 2

5 Call 1

5 Return 1

4 Return 2

3 Return 6

2 Return 24

1 Return 120

fibonacci sequence
Fibonacci sequence

The first ten terms in the sequence are:

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

Each value, except for first two, is sum of last two values

Simply saying:

Fib(n)= fib(n-1)+fib(n-2) except for when n=0 and n=1

Base case:

if (n==0 or n==1)

Return 1

Recursive case:

Fib(n)= fib(n-1)+fib(n-2)

function for fibonacci sequence
Function for fibonacci sequence
  • Int fib (int n)
  • {
  • If (n==0 or n==1)
  • return 1;
  • else
  • return fib(n-1) +fib (n-2);
  • }
trace of fibonacci 5
Trace of Fibonacci(5)
  • If (n==0 or n==1)
  • return 1;
  • else
  • return fib(n-1) +fib (n-2);

= 8

fib 5

5

3

fib 4 + fib 3

3

2

1

fib 3 + fib 2

fib 2 + fib 1

2

fib 1 + fib 0

fib 1 + fib 0

fib 2 + fib 1

2

1

1

1

1

1

1

fib 1 + fib 0

1

why recursion
Why recursion?
  • Recursion makes the program faster?
  • Recursion uses less memory?
  • Recursion makes the code much simpler and Easy to read
ad