Question

HI PLEASE EXPLAIN IN DETAIL THE SYNAX FOR HOW TO DECLARE REFERENCES IN C++, HOW TO...

HI PLEASE EXPLAIN IN DETAIL THE SYNAX FOR HOW TO DECLARE REFERENCES IN C++, HOW TO USE ONE, AND HOW REFERENCES ARE USED WITH FUNCTIONS IN C++, EXPLAIN SO BASIC STUDENT UNDERSTAND. THANKS!

References in C++

•   Know how to declare one

•   Know how to use one

•   Know how they are used with functions

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Reference:

A reference variable is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to that variable.

you can access the contents of the variable through either the original variable name or the reference.

Declaration of reference variable:

                 int n=10;

                 int& t=10;

                 Here “t” is an integer reference variable initialized to “i”.

Example Program:

#include <iostream>

using namespace std;

int main () {

   // declare simple variables

   int    n;

   double d;

    // declare reference variables

   int&    t = n;

   double& p = d;

      n = 10;

   cout << "Value of i : " << n << endl;

   cout << "Value of i reference : " << t << endl;

    d = 89.99;

   cout << "Value of d : " << d << endl;

   cout << "Value of d reference : " << p << endl;

return 0;

}

References are usually used for function argument lists and function return values.            passing references as function parameter more safely than parameters.   

Example for functions taking References Parameters:

void swap (int& first, int& second)

{

        int temp = first;

        first = second;

        second = temp;

}    

C shell #include iostream» 2 using namespace std; 3int main ) 4 // declare simple variables int n; double d; /I declare refer           

Add a comment
Know the answer?
Add Answer to:
HI PLEASE EXPLAIN IN DETAIL THE SYNAX FOR HOW TO DECLARE REFERENCES IN C++, HOW TO...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT