SlideShare a Scribd company logo
1 of 11
Lecture 06



         Reference Variables


Learn about:
- Reference arguments
- Pass Arguments and Structures by reference
- const function arguments

Ref.: OOP using C++, Joyce Farrel, Thopson Learningv



                                                       1
Reference Variables (page 137-139)

• A reference is an alias (a different name) to a variable.
• How to declare a reference variable?
  i) By placing a type and & in front of a variable.
  ii) Assign another variable of the same type to the
  reference variable.
    For example:         double someMoney;
                         double &cash = someMoney



                                                 2/11
Reference Variables (page 137-139)

double someMoney;
double &cash = someMOney;

cash = 6.78;
cout<<cash<<endl;   // displays 6.78
cout<<someMoney;    // displays 6.78

someMOney = 111.333;
cout<<cash<<endl;    // displays 111.333
cout<<someMoney;     // displays 111.333

                                       3/11
Reference Arguments

• Passing arguments by reference allows a function to
  access variables in the calling program, as well as
  returning more than one value.


• Reference arguments are indicated by the & following
  the data type.

    (float n, float &intp, float &fracp)


                                              4/11
Reference Arguments
                         Example 1
#include <iostream.h>
void intfrac(float n, float& intp, float& fracp)
{ long temp = static_cast<long>(n); // temp = 10
  intp = static_cast<float>(temp);   // intp = 10
  fracp = n - intp;                  // fracp = 0.25
}
void main()
{ float number, intpart, fracpart;
  do {
    cout << "nEnter a real number: ";
    cin>>number;        // input: 10.25
    intfrac(number, intpart, fracpart);
    cout << "Integer part is " << intpart
         << ", fraction part is "
         << fracpart << endl;
  } while( number != 0.0 );
                                               5/11
}
Reference Arguments
                        Example 1 (cont.)


• The & indicates that intp is an alias for whatever variable is
 passed as an argument. Similarly for fracp.

• Note that the & is not used in the function call.

• Do not confuse with the address     of operator (same symbol).




                                                      6/11
Pass By Reference: Example 2

#include <iostream.h>
void order(int& numb1,   int& numb2)
{ if(numb1 > numb2) {
    int temp = numb1;
    numb1 = numb2;
    numb2 = temp; }
}
void main()
{ int n1=99, n2=11;       // not ordered
   int n3=22, n4=88;      // ordered
   order(n1, n2);
   order(n3, n4);
   cout << "n1=" << n1   <<   endl;
   cout << "n2=" << n2   <<   endl;
   cout << "n3=" << n3   <<   endl;
   cout << "n4=" << n4   <<   endl;        7/11
}
Passing Structures By Reference
                   Example
#include <iostream.h>
struct Distance {     // English distance
   int feet;
   float inches;
};

void engldisp( Distance dd )
{
  cout << dd.feet << "'-"
       << dd.inches << """;
}

void scale( Distance& dd, float factor)
{
  float inches = (dd.feet*12 + dd.inches) * factor;
  dd.feet = static_cast<int>(inches / 12);
  dd.inches = inches - dd.feet * 12;       8/11
}
Passing Structures By Reference
                       Example (cont.)
     int main()
     {
        Distance d1 = { 12, 6.5 };
        Distance d2 = { 10, 5.5 };
        cout << "nd1 = "; engldisp(d1);
        cout << "nd2 = "; engldisp(d2);
        scale(d1, 0.5);
        scale(d2, 0.25);
        cout << "nd1 = "; engldisp(d1);
        cout << "nd2 = "; engldisp(d2);
        cout << endl;
        return 0;
     }
• By default, structures are passed by value (i.e. copy each member).
• Passing structure by reference is much more efficient.9/11
const Function Arguments

• Passing arguments by reference is more
  efficient and also allows the function to modify
  them directly.

• Can you pass an argument by reference for
  efficiency, but with a guarantee that the
  function cannot modify it?

• You can apply the const modifier.
                                         10/11
const Function Arguments
                          Example
#include <iostream.h>

void main()
{
  int alpha = 7;
  int beta = 11;
  aFunc(alpha, beta);
}

void aFunc(int& a, const int& b)
{
  a = 107;   // OK
  b = 111;   // error: can't modify const
}

                                            11/11

More Related Content

What's hot

Recursion in c
Recursion in cRecursion in c
Recursion in cSaket Pathak
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++LPU
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3Ali Aminian
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphismkiran Patel
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpprajshreemuthiah
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Function in c++
Function in c++Function in c++
Function in c++Kumar
 
Bca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersBca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersRai University
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
Mca 2nd sem u-5 files & pointers
Mca 2nd  sem u-5 files & pointersMca 2nd  sem u-5 files & pointers
Mca 2nd sem u-5 files & pointersRai University
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 

What's hot (19)

Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Lecture 13 - Storage Classes
Lecture 13 - Storage ClassesLecture 13 - Storage Classes
Lecture 13 - Storage Classes
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
C++ concept of Polymorphism
C++ concept of  PolymorphismC++ concept of  Polymorphism
C++ concept of Polymorphism
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Function in c++
Function in c++Function in c++
Function in c++
 
Bca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointersBca 2nd sem u-5 files & pointers
Bca 2nd sem u-5 files & pointers
 
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Mca 2nd sem u-5 files & pointers
Mca 2nd  sem u-5 files & pointersMca 2nd  sem u-5 files & pointers
Mca 2nd sem u-5 files & pointers
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
Function
FunctionFunction
Function
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 

Similar to Reference Variables (page 137-139

FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxDeepasCSE
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdfLadallaRajKumar
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)Ameer Hamxa
 
Functions in C++ (OOP)
Functions in C++ (OOP)Functions in C++ (OOP)
Functions in C++ (OOP)Faizan Janjua
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++Mukund Trivedi
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxShashiShash2
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxAbubakar524802
 
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
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming FundamentalsZohaib Sharif
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docxJoeyDelaCruz22
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdfShifatiRabbi
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)SURBHI SAROHA
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)jewelyngrace
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.pptWaheedAnwar20
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++Manzoor ALam
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping newaprilyyy
 
C++ references
C++ referencesC++ references
C++ referencescorehard_by
 

Similar to Reference Variables (page 137-139 (20)

FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)c++ pointers by Amir Hamza Khan (SZABISTIAN)
c++ pointers by Amir Hamza Khan (SZABISTIAN)
 
Functions in C++ (OOP)
Functions in C++ (OOP)Functions in C++ (OOP)
Functions in C++ (OOP)
 
85ec7 session2 c++
85ec7 session2 c++85ec7 session2 c++
85ec7 session2 c++
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
4th unit full
4th unit full4th unit full
4th unit full
 
OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)OOPS USING C++(UNIT 2)
OOPS USING C++(UNIT 2)
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
02 functions, variables, basic input and output of c++
02   functions, variables, basic input and output of c++02   functions, variables, basic input and output of c++
02 functions, variables, basic input and output of c++
 
Switch case and looping new
Switch case and looping newSwitch case and looping new
Switch case and looping new
 
C++ references
C++ referencesC++ references
C++ references
 

More from elearning_portal (14)

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

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

Reference Variables (page 137-139

  • 1. Lecture 06 Reference Variables Learn about: - Reference arguments - Pass Arguments and Structures by reference - const function arguments Ref.: OOP using C++, Joyce Farrel, Thopson Learningv 1
  • 2. Reference Variables (page 137-139) • A reference is an alias (a different name) to a variable. • How to declare a reference variable? i) By placing a type and & in front of a variable. ii) Assign another variable of the same type to the reference variable. For example: double someMoney; double &cash = someMoney 2/11
  • 3. Reference Variables (page 137-139) double someMoney; double &cash = someMOney; cash = 6.78; cout<<cash<<endl; // displays 6.78 cout<<someMoney; // displays 6.78 someMOney = 111.333; cout<<cash<<endl; // displays 111.333 cout<<someMoney; // displays 111.333 3/11
  • 4. Reference Arguments • Passing arguments by reference allows a function to access variables in the calling program, as well as returning more than one value. • Reference arguments are indicated by the & following the data type. (float n, float &intp, float &fracp) 4/11
  • 5. Reference Arguments Example 1 #include <iostream.h> void intfrac(float n, float& intp, float& fracp) { long temp = static_cast<long>(n); // temp = 10 intp = static_cast<float>(temp); // intp = 10 fracp = n - intp; // fracp = 0.25 } void main() { float number, intpart, fracpart; do { cout << "nEnter a real number: "; cin>>number; // input: 10.25 intfrac(number, intpart, fracpart); cout << "Integer part is " << intpart << ", fraction part is " << fracpart << endl; } while( number != 0.0 ); 5/11 }
  • 6. Reference Arguments Example 1 (cont.) • The & indicates that intp is an alias for whatever variable is passed as an argument. Similarly for fracp. • Note that the & is not used in the function call. • Do not confuse with the address of operator (same symbol). 6/11
  • 7. Pass By Reference: Example 2 #include <iostream.h> void order(int& numb1, int& numb2) { if(numb1 > numb2) { int temp = numb1; numb1 = numb2; numb2 = temp; } } void main() { int n1=99, n2=11; // not ordered int n3=22, n4=88; // ordered order(n1, n2); order(n3, n4); cout << "n1=" << n1 << endl; cout << "n2=" << n2 << endl; cout << "n3=" << n3 << endl; cout << "n4=" << n4 << endl; 7/11 }
  • 8. Passing Structures By Reference Example #include <iostream.h> struct Distance { // English distance int feet; float inches; }; void engldisp( Distance dd ) { cout << dd.feet << "'-" << dd.inches << """; } void scale( Distance& dd, float factor) { float inches = (dd.feet*12 + dd.inches) * factor; dd.feet = static_cast<int>(inches / 12); dd.inches = inches - dd.feet * 12; 8/11 }
  • 9. Passing Structures By Reference Example (cont.) int main() { Distance d1 = { 12, 6.5 }; Distance d2 = { 10, 5.5 }; cout << "nd1 = "; engldisp(d1); cout << "nd2 = "; engldisp(d2); scale(d1, 0.5); scale(d2, 0.25); cout << "nd1 = "; engldisp(d1); cout << "nd2 = "; engldisp(d2); cout << endl; return 0; } • By default, structures are passed by value (i.e. copy each member). • Passing structure by reference is much more efficient.9/11
  • 10. const Function Arguments • Passing arguments by reference is more efficient and also allows the function to modify them directly. • Can you pass an argument by reference for efficiency, but with a guarantee that the function cannot modify it? • You can apply the const modifier. 10/11
  • 11. const Function Arguments Example #include <iostream.h> void main() { int alpha = 7; int beta = 11; aFunc(alpha, beta); } void aFunc(int& a, const int& b) { a = 107; // OK b = 111; // error: can't modify const } 11/11