0% found this document useful (0 votes)
2 views2 pages

OOPS8 Reference

A reference variable in C++ is an alias for another variable, allowing it to be referred to by a different name. The syntax for creating a reference is 'Data type& new variable = previous variable'. Unlike pointers, references cannot be NULL, cannot be reassigned to different objects, and must be initialized at the time of creation.

Uploaded by

adarshhalse45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

OOPS8 Reference

A reference variable in C++ is an alias for another variable, allowing it to be referred to by a different name. The syntax for creating a reference is 'Data type& new variable = previous variable'. Unlike pointers, references cannot be NULL, cannot be reassigned to different objects, and must be initialized at the time of creation.

Uploaded by

adarshhalse45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

What is a reference variable?

A reference is defined as an alias for another variable. In short, it is like giving a different
name to a pre-existing variable. Once a reference is initialized to the variable, we can use
either the reference name or the variable to refer to that variable.

Creating references in C++

y
The basic syntax to create a reference is -

Data type& new variable = previous variable

u d
S t
The newly created variable will now refer to the previous variable.

For example -

D
J M
int i = 17 // The variable i is declared as 17

Creating the reference of i will be as -

int& x = i // Here x will be called as the integer variable initialised to r

C++ code

#include <iostream>

using namespace std;

int main () {

int i; // Declare variable I as int

double d; // Declare variable d as double type

MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project Training.


Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
// declare reference variables for I and d

int& r = i;// r is reference to i

double& s = d;// s is reference to d

i = 5;

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

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

d y
d = 11.7;

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

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

D
M
return 0;

J
Difference between Reference and Pointers

References Pointers

We cannot have a NULL reference. The concept of NULL pointers is allowed.

A reference assigned to a particular object Pointers, on the other hand, can point to different
cant be changed. objects at any time.
A reference is also initialized at the time of Pointers can be initialized at any time.
its creation.

JMD Study Computer Classes


Training > Project > Placement
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy