Your SlideShare is downloading. ×
0
Lecture 05       Overloaded Functions               and                Inline FunctionsLearn about:Some functions that are...
Overloaded Functions (page 146-148)• Overloaded functions are functions that have  similar name but each performs differen...
Function Overloading                        Example 1#include <iostream.h>       void repchar(char ch)void repchar();     ...
Function Overloading                         Example 2#include <iostream.h>      void show(double val)                    ...
Inline Functions (page 143-144)• Functions are good for structured programming  but incur runtime overhead.• An inline fun...
Inline Functions : Overhead:                             memory:                             a     10int main() {     func...
Inline Functions: Example 1#include <iostream.h>inline void lbstokg(float pounds){  0.453592 * pounds;}void main(){ float ...
Inline Functions: Example 2#include <iostream.h>inline double computeGross(double hours, double rate){  return (hours * ra...
Inline Functions Vs Macros• Inline functions are similar to macros declared  with the #define directive. The major  differ...
Inline Functions Vs Macros                          Example#include <iostream.h>#define MAX(A,B) ( (A) > (B) ? (A) : (B))i...
Default Arguments (page 144 - 146)#include <iostream.h>void compute (int length, int width, int height){  ………..}void main(...
Default Arguments (page 144 - 146)• A function can be called without specifying all  its arguments.• The function declarat...
Default Arguments: Example 1#include <iostream.h>void repchar(char=*, int=20);void main(){ repchar();        // print 20 *...
Default Arguments: Example 2#include <iostream.h>void showstring(char *str = "Hello World!"){ cout << str << endl;}void ma...
Default Arguments: Example 3#include <iostream.h>void example(int, int = 5, float = 6.78);void main(){   example(7, 2, 9.3...
Upcoming SlideShare
Loading in...5
×

Lecture05

313

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
313
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Lecture05"

  1. 1. Lecture 05 Overloaded Functions and Inline FunctionsLearn about:Some functions that are not supported in C:- Overloaded- inline functions- default arguments.Ref.: OOP using C++, Joyce Farrel, Thopson Learning 1
  2. 2. Overloaded Functions (page 146-148)• Overloaded functions are functions that have similar name but each performs different activities depending on the kind of data sent to the function.• The compiler uses the number of arguments, and their data types, to distinguish one function from another.• Function overloading is not allowed in C. 2/15
  3. 3. Function Overloading Example 1#include <iostream.h> void repchar(char ch)void repchar(); { for(int j=0; j<20; j++)void repchar(char); cout << ch;void repchar(char, int); cout << endl; }int main(){ void repchar(char ch, int n) repchar(); { for(int j=0; j<n; j++) repchar(=); cout << ch; repchar(+, 30); cout << endl; return 0; }}void repchar() Output:{ for(int j=0; j<20; j++) ******************** cout << *; ==================== cout << endl; +++++++++++++++++++++++++ 3/15}
  4. 4. Function Overloading Example 2#include <iostream.h> void show(double val) { cout << "Double: " << valvoid show(int); << endl;void show(double); }void show(char*); void show(char *val)int main() { cout << "String: " << val{ show(12); << endl; show(3.1415); } show("Hello World"); return 0; Output:}void show(int val) Integer: 12{ cout<<"Integer: "<<val Double: 3.1415<< endl; String: Hello World} 4/15
  5. 5. Inline Functions (page 143-144)• Functions are good for structured programming but incur runtime overhead.• An inline function is written like a normal function but compiles into inline code which is inserted into the program wherever a function call occurs.• Inline functions are not allowed in C. 5/15
  6. 6. Inline Functions : Overhead: memory: a 10int main() { func1(10, 20); b 20 : x 30 : func1(40,50); • passing control to the function. : : • placing argument values in a memory.} • returning to proper point in the calling program.void func1(int a, int b) { : • providing a memory for any returned x = a + b; value :} 6/15
  7. 7. Inline Functions: Example 1#include <iostream.h>inline void lbstokg(float pounds){ 0.453592 * pounds;}void main(){ float lbs; cout<<"Enter weight in pounds: "; cin>>lbs; cout<<"Your weight in kg is " << lbstokg(lbs) << endl;} 0.453592 * lbs 7/15
  8. 8. Inline Functions: Example 2#include <iostream.h>inline double computeGross(double hours, double rate){ return (hours * rate);}void main(){ double hrsWorked = 37.5, rateOfPay = 12.45, gross; gross = computeGross(hrsWorked, rateOfPay); cout<<endl<<“Gross pay is “<<gross;} hrsWorked * rateOfPay 8/15
  9. 9. Inline Functions Vs Macros• Inline functions are similar to macros declared with the #define directive. The major differences are: – Inline functions are recognized by the compiler. Macros are implemented by a simple text substitution. – Compiler performs type checking on the parameters of an inline function. Macros have unwanted side effects. 9/15
  10. 10. Inline Functions Vs Macros Example#include <iostream.h>#define MAX(A,B) ( (A) > (B) ? (A) : (B))inline int max(int a, int b) void main(){ if (a > b) { int result,x,y; return a; x = 23; y = 45; result = MAX(x++, y++); else cout << x << y << "n"; return b;} x = 23; y = 45; result = max(x++, y++); cout << x << y << "n"; }Output from the program:2447 10/152446
  11. 11. Default Arguments (page 144 - 146)#include <iostream.h>void compute (int length, int width, int height){ ………..}void main(){ …… compute (12, 7); // cause syntax error} 11/15
  12. 12. Default Arguments (page 144 - 146)• A function can be called without specifying all its arguments.• The function declaration must provide default values for those arguments that are not specified.• Default arguments are not allowed in C. 12/15
  13. 13. Default Arguments: Example 1#include <iostream.h>void repchar(char=*, int=20);void main(){ repchar(); // print 20 * repchar(=); // print 20 = repchar(+, 25); // print 25 +} Output:void repchar(char ch, int n) ********************{ ==================== for(int j=0; j<n; j++) +++++++++++++++++++++++++ cout << ch; cout << endl;} 13/15
  14. 14. Default Arguments: Example 2#include <iostream.h>void showstring(char *str = "Hello World!"){ cout << str << endl;}void main(){ showstring("Heres an explicit argument."); showstring(); // in fact this says: // showstring("Hello World!");} Output: Heres an explicit argument. Hello World! 14/15
  15. 15. Default Arguments: Example 3#include <iostream.h>void example(int, int = 5, float = 6.78);void main(){ example(7, 2, 9.3); example(7, 2); example(7);}void example(int x, int y, float z);{ cout << x << y << z << endl;} Output: 7 2 9.3 7 2 6.78 7 5 6.78 15/15
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×