Access Specifiers in C++ Programming

Access Specifiers

  • Access modifiers define the access control rules.
  • It is used to set boundaries for availability of members of class.
Following are the three access specifiers in C++:
1. Public
2. Private
3. Protected

Access SpecifierDescription
PublicIt is accessible from anywhere outside the class but within a program.
PrivateIt cannot be accessed or viewed from outside the class.
ProtectedIt is similar to a private member but it can be accessed in child classes which are called derived classes.

Preprocessor Directives

  • Preprocessor directives contain special instructions which indicate how the program is prepared for compilation.
  • Common preprocessor command is 'include' that tells the compiler to execute a program where some information is required from the specified header file.
  • It starts with '#' character.
It can be categorize into following directives:
1. Inclusion Directives
2. Macro Definition Directives
3. Conditional Compilation Directives
4. Other Directives

1. Inclusion Directives

  • Inclusion category has only one directive called #include.
  • It is used to include files into the current file.
It can be used as follows:

#include<stdio.h>It includes stdio.h from include folder.
#include<iostream>It includes cpp class library header input output stream. Stream is an object with properties that are defined by a class.
#include<my.cpp>It includes my .cpp file from include folder.
#include"my.h"It includes my.h file from current working folder.
#include"myfolder/abc.h"It includes abc.h file from the myfolder which is available in current working folder.

2. Macro Definition Directives

It is used to define macros, which are one or more program statements like functions.

It includes two directives for macro definition:

1. #define : It is used to define a macro.
2. #undef : It is used to undefine a macro. The macro cannot be used after it is undefined.

3. Conditional Compilation Directives

  • It is used to execute statements conditionally for debugging purpose, evaluating codes etc.

  • It includes following directives:
    i. #if
    ii. #elif
    iii. #endif
    iv. #ifdef
    v. #ifndef

  • The above macros are evaluated on compile time. Most compilers do not support the use of variables with these directives.

4. Other Directives

It includes following directives:

1. #error : If the #error directive is found, the program will be terminated.
2. #line : This directive is used to change the value _ LINE _ and _ FILE _ macros.
3. #pragma : This directive is used to allow suppression of specific error messages, manage heap and stack debugging etc.