Вы находитесь на странице: 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.

Вам также может понравиться