STATIC_ CLASS_ MEMBERS, MACROS-CONST-INLINEFUNCTIONS, REFERENCE_ VARIABLES-AND- PASS_BY_REFERENCE
Static class members:
a) We can create class members[common for all objects] using the keyword static.
b) We can access the static members using :: (scope resolution operator), without an object.
c) Static members need to be redefined outside the class.
d) Static functions can access only static data members.
a) We can create class members[common for all objects] using the keyword static.
b) We can access the static members using :: (scope resolution operator), without an object.
c) Static members need to be redefined outside the class.
d) Static functions can access only static data members.
class A
{
public:
static int x;
static void stat_function();
};
int A::x = 0;
void A::stat_function()
{
cout<<x<<endl;
}
Macros, Const and Inline functions:
Macro:
#define A 10
Whenever we use 'A', it is replaced with the value 10.
Macros are just replaced.
Const:
const int A = 1;
a) The value of a constant member cannot be changed.
b) If we declare a function as constant, we cannot change the values inside that.
c) If we declare a class as 'const', we cannot change its values, except mutable members.
Inline functions: (the statements are added in the call)
a) Inline functions cannot have complex operations.
b) The inline keyword is a request to the compiler,
the compiler may replace the statements in the called function or it can treat the inline function as a normal one.
Reference variables and pass by Reference:
Reference -> giving an alias name to the same variable.
e.g.
int a;
int &ref = a;
a = 1;
Reference variable needs to be initialized at the time of declaration.
Pass by Reference:
a) variables can be passed to other functions as pass by reference.
b) It doesn't create a copy of a variable, instead it refers the same memory location.
e.g.
void PassByReference(int &x, int &y)
{
x = x + 10;
y = y + 10;
}
calling Function:
int x=1, y=1;
PassByReference(x, y);
The changes made in the function is reflected in the calling function also.