0% found this document useful (0 votes)
41 views8 pages

Reference Variable

Reference variables provide an alias for previously defined variables, allowing two variables to reference the same memory location. A reference variable is declared using the "&" symbol when defining the data type. A major application of reference variables is passing arguments to functions by reference, allowing the function to modify the original variable. C++ introduces several new operators including scope resolution "::" for defining member functions outside a class and resolving scopes, pointer to member "::*" for declaring pointers to members, and memory management "new" and "delete" for dynamic memory allocation and release.

Uploaded by

Anuj Agarwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

Reference Variable

Reference variables provide an alias for previously defined variables, allowing two variables to reference the same memory location. A reference variable is declared using the "&" symbol when defining the data type. A major application of reference variables is passing arguments to functions by reference, allowing the function to modify the original variable. C++ introduces several new operators including scope resolution "::" for defining member functions outside a class and resolving scopes, pointer to member "::*" for declaring pointers to members, and memory management "new" and "delete" for dynamic memory allocation and release.

Uploaded by

Anuj Agarwal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Reference Variable

• Reference variable provides an alias for the previously


defined variable.
• E.g. if we make the variable Sum a reference to the variable
Total then Sum and Total can b used interchangeably to
present that variable.
• Syntax :-
data_type & reference_name = variable_name
E.g. float total = 100;
float & sum =total;
Reference variable should be initialized at the time of
declaration
• Float & means reference to float.
• A major application of reference variable is in passing arguments to the
functions
• E.g void f(int &x) // uses references
{
x=x+10; // x is incremented ; so also m
}
Int main()
{
int m =10;
f(m); // function call
……………..
…………….
}
Operators in c++

C++ contains the all the operators used in c and


new other operators are introduced:-
• :: scope resolution operator.
• ::* pointer to member declarator.
• ->* pointer to member operator.
• New Memory allocation operator
• Delete Memory release operator
• Endl line feed operator.
C++ Scope Resolution Operator ::

• A scope resolution operator (::), can be used to define the


member functions of a class outside the class.
• The other uses of the resolution operator is to resolve the
scope of the variables if the same variable name is used for
the global, local, and the data member of the class.
• If the resolution operator is placed between the class name
and the data member belonging to the class then the data
name belonging to the particular class is affected.
• If the resolution operator is placed in front of the variable
name then the global variable is affected.
• If no resolution operator is placed then the local variable is
affected.
Scope resolution operator
#include<iostream.h>
int m = 10; // global variable
int main()
{
int k =m;
int m=30; // m declared again
// local to inner block
Cout <<“this is inner block\n”;
Cout <<“k=“<<k<<“\n”;
Cout<<“m=“<<m<<“\n”;
Cout<<“::m”<<::m<<“\n”;
}
Cout<<“in outer block”;
Cout<“m=“<<m<<“\n”;
Cout<<“::m=“<<::m<<“\n”;
return 0;
}
Scope resolution in classes
class ABC
{
public:
static void fun()
{
cout<<"ABC"<<endl;
}
};
class XYZ
{
public:
static void fun()
{
cout<<"XYZ"<<endl;
}
};
int main()
{
ABC::fun();
XYZ::fun();
return 0;
}
Member Dereferencing Operator

::* To declare a pointer to a member of a class

* To access a member using object name and a


pointer to that member

->* To access a member using a pointer to the object and a


pointer to that member
Memory Management Operator

• New operator an object can be created by


using new operator.
• Delete operator releases the memory
occupied by the object.

You might also like