Classes function overloading

OOPS



   DONE BY:
   Ankush Kumar
Function Overloading
   C++ permits the use of two function with the same name.
 However such functions essentially have different argument list.
The difference can be in terms of number or type of arguments or
                              both.
   The biggest advantage of overloading is that it helps us to
 perform same operations on different datatypes without having
         the need to use separate names for each version.

This process of using two or more functions with the same name
  but differing in the signature is called function overloading.

But overloading of functions with different return types are not
                           allowed.

  In overloaded functions , the function call determines which
              function definition will be executed.
Function Overloading
Example:
#include<iostream>
using namespace std;

int abslt(int );
long abslt(long );
float abslt(float );
double abslt(double );

int main()
{
  int intgr=-5;
  long lnt=34225;
  float flt=-5.56;
  double dbl=-45.6768;
  cout<<" absoulte value of "<<intgr<<" = "<<abslt(intgr)<<endl;
   cout<<" absoulte value of "<<lnt<<" = "<<abslt(lng)<<endl;
cout<<" absoulte value of "<<flt<<" = "<<abslt(flt)<<endl;
cout<<" absoulte value of "<<dbl<<" = "<<abslt(dbl)<<endl;
}
int abslt(int num)
{
if(num>=0)
return num;
else
 return (-num);
}
long abslt(long num)
{
if(num>=0)
return num;
else return (-num);
}
float abslt(float num)
{
if(num>=0)
return num;
else return (-num);
}
double abslt(double num)
if(num>=0)
return num;
else return (-num);
}

OUTPUT
absoulte value of -5 = 5
absoulte value of 34225 = 34225
absoulte value of -5.56 = 5.56
absoulte value of -45.6768 = 45.6768


The above function finds the absolute value of any number int, long, float ,double.


The use of overloading may not have reduced the code complexity /size but has
definitely made it easier to understand and avoided the necessity of remembering
different names for each version function which perform identically the same task.
Call by Value & Call by Reference
   In C ++ programming language, variables can be
   referred differently depending on the context. For
   example, if you are writing a program for a low
   memory system, you may want to avoid copying
   larger sized types such as structs and arrays when
   passing them to functions. On the other hand,
   with data types like integers, there is no point in
   passing by reference when a pointer to an integer
   is the same size in memory as an integer itself.

   Now, let us learn how variables can be passed in
                     a C program.
Call By Value
When you use pass-by-value, the compiler copies the value of an
argument in a calling function to a corresponding non-pointer or non-
reference parameter in the called function definition. The parameter in the
called function is initialized with the value of the passed argument. As long
as the parameter has not been declared as constant, the value of the
parameter can be changed, but the changes are only performed within the
scope of the called function only; they have no effect on the value of the
argument in the calling function.

In the following example, main passes func two values: 5 and 7. The
function func receives copies of these values and accesses them by the
identifiers a and b. The function func changes the value of a. When control
passes back to main, the actual values of x and y are not changed.
Sample Program
#include <stdio.h>

void func (int a, int b)
{
  a += b;
  printf("In func, a = %d b = %dn", a, b);
}

int main(void)
{
  int x = 5, y = 7;
  func(x, y);
  printf("In main, x = %d y = %dn", x, y);
  return 0;
}
The output of the program is:

In func, a = 12 b = 7
In main, x = 5 y = 7
Call By Reference
There are two instances where a variable is passed by reference:

When you modify the value of the passed variable locally and also the
value of the variable in the calling function as well.

To avoid making a copy of the variable for efficiency reasons.
Passing by by reference refers to a method of passing the address of an
argument in the calling function to a corresponding parameter in the
called function.

 In C, the corresponding parameter in the called function must be
declared as a pointer type.

 In C++, the corresponding parameter can be declared as any reference
type, not just a pointer type.

In this way, the value of the argument in the calling function can be
modified by the called function.
Sample Program
The following example shows how arguments are passed by reference. In C++,
the reference parameters are initialized with the actual arguments when the
function is called. In C, the pointer parameters are initialized with pointer values
when the function is called.
   #include <stdio.h>

   void swapnum(int &i, int &j) {
     int temp = i;
     i = j;
     j = temp;
   }

   int main(void) {
    int a = 10;
    int b = 20;

       swapnum(a, b);
       printf("A is %d and B is %dn", a, b);
       return 0;
   }
Call by Value vs Call by Reference
 The process of calling          The process of calling
  function by actually sending     function using pointers to
  or passing the copies of         pass the address of
  data.                            variables .
 At most one value at a time     Multiple values can be
  can be returned to the           returned to calling function
  calling function with an         and explicit return
  explicit return statement.       statement is not required .
 Here formal parameters are      Here formal parameters are
  normal variable names that       pointer variables that can
  can receive actual               receive actual parameter or
  parameters/argument              arguments as address of
  value’s copy.                    variables .
Calling a Function using a Pointer
     In C++ you call a function using a function
 pointer by explicitly dereferencing it using the *
 operator. Alternatively you may also just use the
     function pointer's instead of the funtion's
 name. In C++ the two operators .* resp. ->* are
    used together with an instance of a class in
   order to call one of their (non-static) member
  functions. If the call takes place within another
 member function you may use the this-pointer.
Calling a function using a pointer
EXAMPLE:-
main()
{
TMyClass instance1;
int result3 = (instance1.*pt2Member)(12, 'a', 'b'); // C++
int result4 = (*this.*pt2Member)(12, 'a', 'b');   // C++ if this-pointer can
                                                     be used

TMyClass* instance2 = new TMyClass;
int result4 = (instance2->*pt2Member)(12, 'a', 'b'); // C++, instance2 is a
                                                        pointer
delete instance2;
return 0;
}
Pass Object As An Argument
Like any other data type,an object may be used as
   a function argument.This can be done in two
                      ways:

 ->  A copy of the entire object is passed to the
                     function
 -> Only address of the object is transferred to
                   the function
The pass by referrence method is more efficient
since it requires to pass only the address of the
         object and not the entire object
Sample Program
/*C++ PROGRAM TO PASS OBJECT AS AN ARGUMEMT. The program Adds the
two heights given in feet and inches. */

#include< iostream.h>
#include< conio.h>

class height
{
int feet,inches;
public:
void getht(int f,int i)
{
feet=f;
inches=i;
}
void putheight()
{
cout< < "nHeight is:"< < feet< < "feett"< < inches< < "inches"< < endl;
}
void sum(height a,height b)
{
height n;
n.feet = a.feet + b.feet;
n.inches = a.inches + b.inches;
if(n.inches ==12)
{
n.feet++;
n.inches = n.inches -12;
}
cout< < endl< < "Height is "< < n.feet< < " feet and "< < n.inches< < endl;
}
};
void main()
{
height h,d,a;
clrscr();
h.getht(6,5);
a.getht(2,7);
h.putheight();
a.putheight();
d.sum(h,a);
getch();
}
Classes function overloading
1 de 17

Recomendados

Function overloading por
Function overloadingFunction overloading
Function overloadingAshish Kelwa
430 vistas4 diapositivas
3 Function Overloading por
3 Function Overloading3 Function Overloading
3 Function Overloadingpraveenjigajinni
1.4K vistas27 diapositivas
Function overloading por
Function overloadingFunction overloading
Function overloadingSelvin Josy Bai Somu
15.7K vistas25 diapositivas
Function overloading por
Function overloadingFunction overloading
Function overloadingSudeshna Biswas
163 vistas10 diapositivas
Function overloading(C++) por
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
467 vistas21 diapositivas
Function C++ por
Function C++ Function C++
Function C++ Shahzad Afridi
2.1K vistas58 diapositivas

Más contenido relacionado

La actualidad más candente

Function overloading and overriding por
Function overloading and overridingFunction overloading and overriding
Function overloading and overridingRajab Ali
4.3K vistas16 diapositivas
C++ Function por
C++ FunctionC++ Function
C++ FunctionHajar
5.5K vistas17 diapositivas
Functions in C++ por
Functions in C++Functions in C++
Functions in C++Sachin Sharma
13.7K vistas8 diapositivas
Function overloading in c++ por
Function overloading in c++Function overloading in c++
Function overloading in c++Learn By Watch
1.6K vistas7 diapositivas
Types of function call por
Types of function callTypes of function call
Types of function callArijitDhali
397 vistas14 diapositivas
Functions in c language por
Functions in c language Functions in c language
Functions in c language tanmaymodi4
2.7K vistas30 diapositivas

La actualidad más candente(19)

Function overloading and overriding por Rajab Ali
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
Rajab Ali4.3K vistas
C++ Function por Hajar
C++ FunctionC++ Function
C++ Function
Hajar5.5K vistas
Function overloading in c++ por Learn By Watch
Function overloading in c++Function overloading in c++
Function overloading in c++
Learn By Watch1.6K vistas
Types of function call por ArijitDhali
Types of function callTypes of function call
Types of function call
ArijitDhali397 vistas
Functions in c language por tanmaymodi4
Functions in c language Functions in c language
Functions in c language
tanmaymodi42.7K vistas
Functions in C++ por home
Functions in C++Functions in C++
Functions in C++
home 3.1K vistas
Inline function por Tech_MX
Inline functionInline function
Inline function
Tech_MX14.3K vistas
16717 functions in C++ por LPU
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU5K vistas
Inline Functions and Default arguments por Nikhil Pandit
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit6K vistas
Lecture#6 functions in c++ por NUST Stuff
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
NUST Stuff519 vistas

Destacado

Being functional in PHP por
Being functional in PHPBeing functional in PHP
Being functional in PHPDavid de Boer
812 vistas59 diapositivas
PHP Functions & Arrays por
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & ArraysHenry Osborne
3.2K vistas46 diapositivas
Functions in php por
Functions in phpFunctions in php
Functions in phpMudasir Syed
1.6K vistas20 diapositivas
Oops concepts in php por
Oops concepts in phpOops concepts in php
Oops concepts in phpCPD INDIA
3.6K vistas44 diapositivas
Php string function por
Php string function Php string function
Php string function Ravi Bhadauria
5.5K vistas11 diapositivas
Housekeeping importance and function por
Housekeeping importance and functionHousekeeping importance and function
Housekeeping importance and functionZahedul Islam
83.4K vistas12 diapositivas

Destacado(7)

Being functional in PHP por David de Boer
Being functional in PHPBeing functional in PHP
Being functional in PHP
David de Boer812 vistas
PHP Functions & Arrays por Henry Osborne
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne3.2K vistas
Functions in php por Mudasir Syed
Functions in phpFunctions in php
Functions in php
Mudasir Syed1.6K vistas
Oops concepts in php por CPD INDIA
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA3.6K vistas
Housekeeping importance and function por Zahedul Islam
Housekeeping importance and functionHousekeeping importance and function
Housekeeping importance and function
Zahedul Islam83.4K vistas
Php tutorial por Niit
Php tutorialPhp tutorial
Php tutorial
Niit7.3K vistas

Similar a Classes function overloading

Functions in C++.pdf por
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdfLadallaRajKumar
36 vistas19 diapositivas
C function por
C functionC function
C functionthirumalaikumar3
495 vistas26 diapositivas
Functionincprogram por
FunctionincprogramFunctionincprogram
FunctionincprogramSampath Kumar
44 vistas24 diapositivas
Chapter 5 por
Chapter 5Chapter 5
Chapter 5Amrit Kaur
199 vistas7 diapositivas
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx por
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxvekariyakashyap
13 vistas27 diapositivas
Functions por
FunctionsFunctions
FunctionsPragnavi Erva
222 vistas25 diapositivas

Similar a Classes function overloading(20)

Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx por vekariyakashyap
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap13 vistas
Functions1 por DrUjwala1
Functions1Functions1
Functions1
DrUjwala135 vistas
UNIT3.pptx por NagasaiT
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
NagasaiT6 vistas
Functions and pointers_unit_4 por Saranya saran
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran82 vistas
Reference Parameter, Passing object by reference, constant parameter & Defaul... por Meghaj Mallick
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Meghaj Mallick22 vistas
Function in c program por umesh patil
Function in c programFunction in c program
Function in c program
umesh patil1.4K vistas
FUNCTIONS, CLASSES AND OBJECTS.pptx por DeepasCSE
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE2 vistas
CH.4FUNCTIONS IN C_FYBSC(CS).pptx por SangeetaBorde3
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde332 vistas
User Defined Functions in C por RAJ KUMAR
User Defined Functions in CUser Defined Functions in C
User Defined Functions in C
RAJ KUMAR78 vistas

Más de ankush_kumar

Social Networking por
Social NetworkingSocial Networking
Social Networkingankush_kumar
937 vistas32 diapositivas
mathematical induction por
mathematical inductionmathematical induction
mathematical inductionankush_kumar
11.5K vistas50 diapositivas
mathematical induction por
mathematical inductionmathematical induction
mathematical inductionankush_kumar
455 vistas37 diapositivas
mathematical induction por
mathematical inductionmathematical induction
mathematical inductionankush_kumar
1.2K vistas37 diapositivas
mathematical induction por
mathematical inductionmathematical induction
mathematical inductionankush_kumar
850 vistas48 diapositivas
Inheritance por
InheritanceInheritance
Inheritanceankush_kumar
4.3K vistas80 diapositivas

Más de ankush_kumar(14)

mathematical induction por ankush_kumar
mathematical inductionmathematical induction
mathematical induction
ankush_kumar11.5K vistas
mathematical induction por ankush_kumar
mathematical inductionmathematical induction
mathematical induction
ankush_kumar455 vistas
mathematical induction por ankush_kumar
mathematical inductionmathematical induction
mathematical induction
ankush_kumar1.2K vistas
mathematical induction por ankush_kumar
mathematical inductionmathematical induction
mathematical induction
ankush_kumar850 vistas
Propositional And First-Order Logic por ankush_kumar
Propositional And First-Order LogicPropositional And First-Order Logic
Propositional And First-Order Logic
ankush_kumar41.3K vistas
Soacial networking 3 por ankush_kumar
Soacial networking  3Soacial networking  3
Soacial networking 3
ankush_kumar386 vistas
Soacial networking 1 por ankush_kumar
Soacial networking  1Soacial networking  1
Soacial networking 1
ankush_kumar340 vistas
Memory organisation por ankush_kumar
Memory organisationMemory organisation
Memory organisation
ankush_kumar32.9K vistas
Social networking 2 por ankush_kumar
Social networking 2Social networking 2
Social networking 2
ankush_kumar316 vistas
Set theory and relation por ankush_kumar
Set theory and relationSet theory and relation
Set theory and relation
ankush_kumar5.8K vistas

Último

Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptx por
Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptxGuidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptx
Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptxNiranjan Chavan
42 vistas48 diapositivas
Interaction of microorganisms with vascular plants.pptx por
Interaction of microorganisms with vascular plants.pptxInteraction of microorganisms with vascular plants.pptx
Interaction of microorganisms with vascular plants.pptxMicrobiologyMicro
58 vistas33 diapositivas
REFERENCING, CITATION.pptx por
REFERENCING, CITATION.pptxREFERENCING, CITATION.pptx
REFERENCING, CITATION.pptxabhisrivastava11
41 vistas26 diapositivas
JRN 362 - Lecture Twenty-Two por
JRN 362 - Lecture Twenty-TwoJRN 362 - Lecture Twenty-Two
JRN 362 - Lecture Twenty-TwoRich Hanley
39 vistas157 diapositivas
DISTILLATION.pptx por
DISTILLATION.pptxDISTILLATION.pptx
DISTILLATION.pptxAnupkumar Sharma
75 vistas47 diapositivas
Meet the Bible por
Meet the BibleMeet the Bible
Meet the BibleSteve Thomason
81 vistas80 diapositivas

Último(20)

Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptx por Niranjan Chavan
Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptxGuidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptx
Guidelines & Identification of Early Sepsis DR. NN CHAVAN 02122023.pptx
Niranjan Chavan42 vistas
Interaction of microorganisms with vascular plants.pptx por MicrobiologyMicro
Interaction of microorganisms with vascular plants.pptxInteraction of microorganisms with vascular plants.pptx
Interaction of microorganisms with vascular plants.pptx
MicrobiologyMicro58 vistas
JRN 362 - Lecture Twenty-Two por Rich Hanley
JRN 362 - Lecture Twenty-TwoJRN 362 - Lecture Twenty-Two
JRN 362 - Lecture Twenty-Two
Rich Hanley39 vistas
Creative Restart 2023: Leonard Savage - The Permanent Brief: Unearthing unobv... por Taste
Creative Restart 2023: Leonard Savage - The Permanent Brief: Unearthing unobv...Creative Restart 2023: Leonard Savage - The Permanent Brief: Unearthing unobv...
Creative Restart 2023: Leonard Savage - The Permanent Brief: Unearthing unobv...
Taste62 vistas
Guess Papers ADC 1, Karachi University por Khalid Aziz
Guess Papers ADC 1, Karachi UniversityGuess Papers ADC 1, Karachi University
Guess Papers ADC 1, Karachi University
Khalid Aziz105 vistas
Career Building in AI - Technologies, Trends and Opportunities por WebStackAcademy
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy47 vistas
What is Digital Transformation? por Mark Brown
What is Digital Transformation?What is Digital Transformation?
What is Digital Transformation?
Mark Brown41 vistas
ANGULARJS.pdf por ArthyR3
ANGULARJS.pdfANGULARJS.pdf
ANGULARJS.pdf
ArthyR352 vistas
EILO EXCURSION PROGRAMME 2023 por info33492
EILO EXCURSION PROGRAMME 2023EILO EXCURSION PROGRAMME 2023
EILO EXCURSION PROGRAMME 2023
info33492208 vistas
Education of marginalized and socially disadvantages segments.pptx por GarimaBhati5
Education of marginalized and socially disadvantages segments.pptxEducation of marginalized and socially disadvantages segments.pptx
Education of marginalized and socially disadvantages segments.pptx
GarimaBhati547 vistas
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE... por Nguyen Thanh Tu Collection
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (FRIE...
JQUERY.pdf por ArthyR3
JQUERY.pdfJQUERY.pdf
JQUERY.pdf
ArthyR3107 vistas

Classes function overloading

  • 1. OOPS DONE BY: Ankush Kumar
  • 2. Function Overloading C++ permits the use of two function with the same name. However such functions essentially have different argument list. The difference can be in terms of number or type of arguments or both. The biggest advantage of overloading is that it helps us to perform same operations on different datatypes without having the need to use separate names for each version. This process of using two or more functions with the same name but differing in the signature is called function overloading. But overloading of functions with different return types are not allowed. In overloaded functions , the function call determines which function definition will be executed.
  • 3. Function Overloading Example: #include<iostream> using namespace std; int abslt(int ); long abslt(long ); float abslt(float ); double abslt(double ); int main() { int intgr=-5; long lnt=34225; float flt=-5.56; double dbl=-45.6768; cout<<" absoulte value of "<<intgr<<" = "<<abslt(intgr)<<endl; cout<<" absoulte value of "<<lnt<<" = "<<abslt(lng)<<endl;
  • 4. cout<<" absoulte value of "<<flt<<" = "<<abslt(flt)<<endl; cout<<" absoulte value of "<<dbl<<" = "<<abslt(dbl)<<endl; } int abslt(int num) { if(num>=0) return num; else return (-num); } long abslt(long num) { if(num>=0) return num; else return (-num); } float abslt(float num) { if(num>=0) return num; else return (-num); } double abslt(double num)
  • 5. if(num>=0) return num; else return (-num); } OUTPUT absoulte value of -5 = 5 absoulte value of 34225 = 34225 absoulte value of -5.56 = 5.56 absoulte value of -45.6768 = 45.6768 The above function finds the absolute value of any number int, long, float ,double. The use of overloading may not have reduced the code complexity /size but has definitely made it easier to understand and avoided the necessity of remembering different names for each version function which perform identically the same task.
  • 6. Call by Value & Call by Reference In C ++ programming language, variables can be referred differently depending on the context. For example, if you are writing a program for a low memory system, you may want to avoid copying larger sized types such as structs and arrays when passing them to functions. On the other hand, with data types like integers, there is no point in passing by reference when a pointer to an integer is the same size in memory as an integer itself. Now, let us learn how variables can be passed in a C program.
  • 7. Call By Value When you use pass-by-value, the compiler copies the value of an argument in a calling function to a corresponding non-pointer or non- reference parameter in the called function definition. The parameter in the called function is initialized with the value of the passed argument. As long as the parameter has not been declared as constant, the value of the parameter can be changed, but the changes are only performed within the scope of the called function only; they have no effect on the value of the argument in the calling function. In the following example, main passes func two values: 5 and 7. The function func receives copies of these values and accesses them by the identifiers a and b. The function func changes the value of a. When control passes back to main, the actual values of x and y are not changed.
  • 8. Sample Program #include <stdio.h> void func (int a, int b) { a += b; printf("In func, a = %d b = %dn", a, b); } int main(void) { int x = 5, y = 7; func(x, y); printf("In main, x = %d y = %dn", x, y); return 0; } The output of the program is: In func, a = 12 b = 7 In main, x = 5 y = 7
  • 9. Call By Reference There are two instances where a variable is passed by reference: When you modify the value of the passed variable locally and also the value of the variable in the calling function as well. To avoid making a copy of the variable for efficiency reasons. Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function.  In C, the corresponding parameter in the called function must be declared as a pointer type.  In C++, the corresponding parameter can be declared as any reference type, not just a pointer type. In this way, the value of the argument in the calling function can be modified by the called function.
  • 10. Sample Program The following example shows how arguments are passed by reference. In C++, the reference parameters are initialized with the actual arguments when the function is called. In C, the pointer parameters are initialized with pointer values when the function is called. #include <stdio.h> void swapnum(int &i, int &j) { int temp = i; i = j; j = temp; } int main(void) { int a = 10; int b = 20; swapnum(a, b); printf("A is %d and B is %dn", a, b); return 0; }
  • 11. Call by Value vs Call by Reference  The process of calling  The process of calling function by actually sending function using pointers to or passing the copies of pass the address of data. variables .  At most one value at a time  Multiple values can be can be returned to the returned to calling function calling function with an and explicit return explicit return statement. statement is not required .  Here formal parameters are  Here formal parameters are normal variable names that pointer variables that can can receive actual receive actual parameter or parameters/argument arguments as address of value’s copy. variables .
  • 12. Calling a Function using a Pointer In C++ you call a function using a function pointer by explicitly dereferencing it using the * operator. Alternatively you may also just use the function pointer's instead of the funtion's name. In C++ the two operators .* resp. ->* are used together with an instance of a class in order to call one of their (non-static) member functions. If the call takes place within another member function you may use the this-pointer.
  • 13. Calling a function using a pointer EXAMPLE:- main() { TMyClass instance1; int result3 = (instance1.*pt2Member)(12, 'a', 'b'); // C++ int result4 = (*this.*pt2Member)(12, 'a', 'b'); // C++ if this-pointer can be used TMyClass* instance2 = new TMyClass; int result4 = (instance2->*pt2Member)(12, 'a', 'b'); // C++, instance2 is a pointer delete instance2; return 0; }
  • 14. Pass Object As An Argument Like any other data type,an object may be used as a function argument.This can be done in two ways: -> A copy of the entire object is passed to the function -> Only address of the object is transferred to the function The pass by referrence method is more efficient since it requires to pass only the address of the object and not the entire object
  • 15. Sample Program /*C++ PROGRAM TO PASS OBJECT AS AN ARGUMEMT. The program Adds the two heights given in feet and inches. */ #include< iostream.h> #include< conio.h> class height { int feet,inches; public: void getht(int f,int i) { feet=f; inches=i; } void putheight() { cout< < "nHeight is:"< < feet< < "feett"< < inches< < "inches"< < endl; }
  • 16. void sum(height a,height b) { height n; n.feet = a.feet + b.feet; n.inches = a.inches + b.inches; if(n.inches ==12) { n.feet++; n.inches = n.inches -12; } cout< < endl< < "Height is "< < n.feet< < " feet and "< < n.inches< < endl; } }; void main() { height h,d,a; clrscr(); h.getht(6,5); a.getht(2,7); h.putheight(); a.putheight(); d.sum(h,a); getch(); }