SlideShare a Scribd company logo
1 of 15
Lecture 05



       Overloaded Functions
               and
                Inline Functions
Learn about:
Some functions that are not supported in C:
- Overloaded
- inline functions
- default arguments.

Ref.: OOP using C++, Joyce Farrel, Thopson Learning
                                                      1
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
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
}
Function Overloading
                         Example 2
#include <iostream.h>      void show(double val)
                           { cout << "Double: " << val
void 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
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
Inline Functions : Overhead

:                             memory
:                             a     10
int 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
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
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
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
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/15
2446
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
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
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
Default Arguments: Example 2

#include <iostream.h>

void showstring(char *str = "Hello World!")
{ cout << str << endl;
}

void main()
{   showstring("Here's an explicit argument.");
    showstring(); // in fact this says:
                   // showstring("Hello World!");
}
 Output:
    Here's an explicit argument.
    Hello World!
                                              14/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

More Related Content

What's hot

C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th StudyChris Ohk
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2zindadili
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Sumant Tambe
 
Advanced functional programing in Swift
Advanced functional programing in SwiftAdvanced functional programing in Swift
Advanced functional programing in SwiftVincent Pradeilles
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimizedWoody Pewitt
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programsharman kaur
 
All VLSI programs
All VLSI programsAll VLSI programs
All VLSI programsGouthaman V
 
Functions in C++ (OOP)
Functions in C++ (OOP)Functions in C++ (OOP)
Functions in C++ (OOP)Faizan Janjua
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloadingRai University
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator OverloadingHadziq Fabroyir
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type ConversionsRokonuzzaman Rony
 

What's hot (20)

C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
C++20 features
C++20 features C++20 features
C++20 features
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
Advanced functional programing in Swift
Advanced functional programing in SwiftAdvanced functional programing in Swift
Advanced functional programing in Swift
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Is your C# optimized
Is your C# optimizedIs your C# optimized
Is your C# optimized
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
 
All VLSI programs
All VLSI programsAll VLSI programs
All VLSI programs
 
Functions in C++ (OOP)
Functions in C++ (OOP)Functions in C++ (OOP)
Functions in C++ (OOP)
 
functions of C++
functions of C++functions of C++
functions of C++
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
C++ functions
C++ functionsC++ functions
C++ functions
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
C++ functions
C++ functionsC++ functions
C++ functions
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 

Viewers also liked

Lecture 5, c++(complete reference,herbet sheidt)chapter-15
Lecture 5, c++(complete reference,herbet sheidt)chapter-15Lecture 5, c++(complete reference,herbet sheidt)chapter-15
Lecture 5, c++(complete reference,herbet sheidt)chapter-15Abu Saleh
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Jenish Patel
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to classHaresh Jaiswal
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++Ilio Catallo
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Friend functions
Friend functions Friend functions
Friend functions Megha Singh
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++M Hussnain Ali
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Learn By Watch
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend classAbhishek Wadhwa
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator OverloadingNilesh Dalvi
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 

Viewers also liked (20)

Lecture5
Lecture5Lecture5
Lecture5
 
Best javascript course
Best javascript courseBest javascript course
Best javascript course
 
Lecture 5, c++(complete reference,herbet sheidt)chapter-15
Lecture 5, c++(complete reference,herbet sheidt)chapter-15Lecture 5, c++(complete reference,herbet sheidt)chapter-15
Lecture 5, c++(complete reference,herbet sheidt)chapter-15
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
02. functions & introduction to class
02. functions & introduction to class02. functions & introduction to class
02. functions & introduction to class
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
Inline function
Inline functionInline function
Inline function
 
Friend functions
Friend functions Friend functions
Friend functions
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 

Similar to CPP05-Inline Functions and Function Overloading

Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answerssheibansari
 
power point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions conceptspower point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions conceptsbhargavi804095
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxbradburgess22840
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALPrabhu D
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
C++totural file
C++totural fileC++totural file
C++totural filehalaisumit
 

Similar to CPP05-Inline Functions and Function Overloading (20)

C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
power point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions conceptspower point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions concepts
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 
Function C++
Function C++ Function C++
Function C++
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
Cs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUALCs2312 OOPS LAB MANUAL
Cs2312 OOPS LAB MANUAL
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Managing console
Managing consoleManaging console
Managing console
 
Functions
FunctionsFunctions
Functions
 
C++ functions
C++ functionsC++ functions
C++ functions
 
C++totural file
C++totural fileC++totural file
C++totural file
 

More from elearning_portal (14)

Lecture21
Lecture21Lecture21
Lecture21
 
Lecture19
Lecture19Lecture19
Lecture19
 
Lecture18
Lecture18Lecture18
Lecture18
 
Lecture17
Lecture17Lecture17
Lecture17
 
Lecture16
Lecture16Lecture16
Lecture16
 
Lecture10
Lecture10Lecture10
Lecture10
 
Lecture09
Lecture09Lecture09
Lecture09
 
Lecture07
Lecture07Lecture07
Lecture07
 
Lecture06
Lecture06Lecture06
Lecture06
 
Lecture20
Lecture20Lecture20
Lecture20
 
Lecture03
Lecture03Lecture03
Lecture03
 
Lecture02
Lecture02Lecture02
Lecture02
 
Lecture01
Lecture01Lecture01
Lecture01
 
Lecture04
Lecture04Lecture04
Lecture04
 

CPP05-Inline Functions and Function Overloading

  • 1. Lecture 05 Overloaded Functions and Inline Functions Learn about: Some functions that are not supported in C: - Overloaded - inline functions - default arguments. Ref.: OOP using C++, Joyce Farrel, Thopson Learning 1
  • 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. 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. Function Overloading Example 2 #include <iostream.h> void show(double val) { cout << "Double: " << val void 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. 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. Inline Functions : Overhead : memory : a 10 int 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. 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. 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. 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. 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/15 2446
  • 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. 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. 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. Default Arguments: Example 2 #include <iostream.h> void showstring(char *str = "Hello World!") { cout << str << endl; } void main() { showstring("Here's an explicit argument."); showstring(); // in fact this says: // showstring("Hello World!"); } Output: Here's an explicit argument. Hello World! 14/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