Reference in C++

A reference is an alternative name or alias for an existing variable/object. Just like you make Amitabh Bachchan a reference (alias) to BigB then you can refer him as either Amitabh Bachchan or BigB.
It points same memory location but it is safer then pointers of C language.
एक रिफरेन्स किसी पूर्वनिर्मित वेरिएबल/ऑब्जेक्ट को प्रदान किया गया  वैकल्पिक नाम या उपनाम होता है जैसे आप ने अमिताभ बच्चन को वैकल्पिक नाम या उपनाम बिगबी प्रदान किया है अब आप उन्हें या तो अमिताभ बच्चन या बिगबी कह सकता है। यह एक समान मेमोरी लोकेशन को पॉइंट करता है परन्तु C लैंग्वेज के पॉइंटर की तुलना में सुरक्षित होता है।       

Syntax:-
datatype& reference_name=variable_name;

Example:-
#include<iostream.h>
#include<conio.h>
void main(){
float a,b,sum=0;
//reference
float& total=sum;
clrscr();
cout<<"Enter Two Numbers"<<endl;
cin>>a>>b;
sum=a+b;
cout<<"Sum="<<sum<<endl;
total=total+10;
cout<<"Total="<<sum<<endl;
sum=sum-50;
cout<<"Sum="<<total<<endl;
getch();
}

Comments

Popular posts from this blog

OOPs using C++ Language Topics Covered

IMP Questions of OOPS using CPP