1 / 15

Object Oriented Programming Spring - 2012

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

janet
Télécharger la présentation

Object Oriented Programming Spring - 2012

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMSATS Institute of Information Technology Object Oriented ProgrammingSpring - 2012 Functions OOP in C++ by Robert Lafore - Chapter#5 KaleemUllah kaleemullah@ciitvehari.edu.pk

  2. 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);

  3. Overloaded functions int main() { repchar(); repchar(‘=’); repchar(‘+’, 30); return 0; }

  4. 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; }

  5. 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; • }

  6. Overloaded functions • // displays specified number of copies of specified character • void repchar(char ch, int n) • { • for(int j=0; j<n; j++) // loops n times • cout << ch; // prints specified character • cout << endl; • }

  7. Overloaded functions

  8. Overloaded functions: Different kinds of arguments • struct Distance • { • int feet; • float inches; • }; Void disp( Distance ); //declarations Void disp( float );

  9. Overloaded functions: Different kinds of arguments • struct Distance • { • int feet; • float inches; • }; Void disp( Distance ); //declarations Void disp( float );

  10. Recursion What is recursion? Recursion See “Recursion”. Recursion If you still don't get it, see "Recursion"

  11. Recursion Void fun() { cout<<“this is fun”<<endl; fun(); }

  12. Recursion Void fun() { cout<<“this is fun”<<endl; fun(); } Example of program to print numbers in descending order? When will this end?

  13. Recursion Example of program to print numbers in descending order? int print (int n) { cout<< n<<endl; if (n>1) print(--n); else return 1; } void main () { print (10); // start recursion by calling recursive function }

  14. 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

  15. 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 …

More Related