Explain reference types, C/C++ Programming

Assignment Help:

Reference Types

The symbol "&" is interpreted as an address operator as well as AND operator. This operator is also used to declare a "reference variable". A reference is referred to an alias, serves as an alternative name for the object.

e.g.

                int ival  = 1024;

                int &rval  =  ival;  // rval is reference to ival

  • A reference variable should be initialized at time of declaration itself.

e.g.

                int &iref;              // wrong

                int &iref  =  ival  //o.k.

  • Once a reference has been made to a variable, it cannot be altered to refer to another variable.

e.g.

void main()

{

                  int num1  =  10, num2  =  200;

                  int &ref  =  num1;

  cout<< num1 << num2 << ref ;                //10,200,10;

  ref  =  num2;

  cout << num1 << num2 << ref ;               //200,200,200

                      }

It changes the contents of the original variable, which is not the desired action.

 


Related Discussions:- Explain reference types

What happens while a function throws an exception which , Q-What happens wh...

Q-What happens while a function throws an exception which was not specified through an exception specification for this function? A: Unexpected() is called, which, by default, w

Basic data type in cpp, B a s i c D a t a t y p e : ...

B a s i c D a t a t y p e : T h e r e a r e t h r e e t y p e o f d a t a t y p e i n C+ + 1.       P r

C program for the no are in ascending order , #include stdio.h> #include...

#include stdio.h> #include conio.h> #include string.h> void main() {           int i=0,j=0,k=0,l=0;           int a[3][3],temp[3][3];           clrscr();

Algorithem, algorithem of compound interest

algorithem of compound interest

Area under curve, Write a program to find the area under the curve y = f(x)...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. #include float start_point,

CptS 121 Program Development & Design program design in C, Write a program...

Write a program that performs character processing on 10 characters read in from a file, and writes the results to output files. Do NOT use loops or arrays to solve this problem. N

Advanced input - output routines, Although we can handle most I/O routines ...

Although we can handle most I/O routines with getc and putc , there are file versions of fgets, fputs, fprintf and fscanf . The syntax is       fgets(stringname,n,filename);   Th

Open and close ?les for writing, If you have computed a number (stored as a...

If you have computed a number (stored as a ?oating-point value) and want to display this on a plot (i.e. as a character string) you have to convert it into a STRING. That is we wan

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd