In this article scope resolution operator we give the information about where both global and local variables have the same name in a program, in which case the scope resolution operator (::) is used to access the vale of global variable.

Reference Variables in C++:-

C++ introduces a new kind of variables knows as the reference variable. A reference variable provides an alias (alternative name) for a previously defined variable.

For example:-

If we make the variable product a reference to the multiply, then product and multiply can be used interchangeably to represent that variable.

A reference variable is created as follows:

data_type & reference_name = variable_name

Example:

int product=2000;

int & multiply = product;

product is a int type variable that has already been declared, multiply is the alternative name declared to represent the variable product. Both the variables refer to the same data object in the memory. Now, the statements,

cout<< product;

and

cout<<multiply;

both print the value 2000. The statement

product = product * 2;

will change the value of both product and multiply to 4000.

Note: A reference variable must be initialized at the time of declaration.

// Reference variables in C++

#include<iostream.h>

#include<conio.h>

void main()

{

int a, b, product;

clrscr();

cout<<“\n\n\n Enter the two numbers : “;

cin>>a>>b;

product=a*b;

cout<<“\n Product of A into B = “<<product;

int & multiply = product;

cout<<“\n Product of A into B = “<<multiply;

getch();

}

O/P:

Enter the two numbers : 5

8

Product of A into B = 40

Product of A into B = 40

Scope resolution operator C++

C++ scope resolution operator:

This operator is used for global variables, where both global and local variables have the same name in a program, in which case the scope resolution operator (::) is used to access the global variable.

You can understand it from the program given below-

Scope resolution operator example in C++

#include<conio.h>

#include<iostream.h>

int num=35; // global varaible

void main()

{

clrscr();

int n;

int num = 65; //local variable

cout<<“local num : “<<num<<endl; // for local variable

cout<<“global ::num: “<<::num; // global variable

getch();

}

OUTPUT:-

local num : 65

global :: num: 35

It is also used in the outside definition of the member function in the class, it is further explained in the program.

#include<iostream.h>

#include<conio.h>

class stud

{

// private by default

int roll_no;

char name[20];

public:                                        // public member

void getdata( );

void display();

};                                                       //class closed

//input record from the user

void stud::getdata( )

{

cout<<“Enter roll no: “;

cin>>roll_no;

cout<<“Enter Name : “;

gets(name);

}

//display record

void stud::display()

{

cout<<“\nRoll no: “<<roll_no;

cout<<“\nName   : “<<name;

}

// main program start

void main()

{

clrscr();

stud p;

p.getdata();

p.display();

getch();

}

OUTPUT:-

Enter roll no: 1

Enter Name : Yashraj

Roll no: 1

Name : Yashraj

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *