C++ Keyword

Keyword is reserved word by compiler. when we use keyword in our source code. then compiler are provide special meanings. normally keyword are use to defined statement and instruction. Specialty of keyword are there is not depend of any header file. we can directly use any keyword in our program. but there certain rules and regulations. c++ are provide 95 unique keywords. we can use those keywords in our program. so let start.

List of keywords

using namespace char
int float
double char long
unsigned signed short
void return extern
static register auto
const volatile typedef
struct enum union
for while do
continue break switch
case default if
else class public
private protected friend
virtual new delete
bool try throw
catch goto true
false and and_eq
bitand bitor compl
not not_eq or
or_eq reinterpret_cast sizeof
static_cast dynamic_cast explicit
template typeid typename
xor xor_eq wchar_t
asm this inline
alignas (since C++11) alignof (since C++11) atomic_cancel (TM TS)
atomic_commit (TM TS) atomic_noexcept (TM TS) static_assert (since C++11)
thread_local(since C++11) char16_t (since C++11) char32_t (since C++11)
module (modules TS) mutable concept (since C++20)
noexcept (since C++11) constexpr (since C++11) nullptr (since C++11)
co_await(coroutines TS) co_return(coroutines TS) co_yield (coroutines TS)
decltype (since C++11) const_cast export
operator requires (since C++20) synchronized (TM TS)

Example of cpp keywords

Here given of basic examples and information of all the cpp keywords.

using namespace

using namespace is single statement but here exist two keywords. using and another is namespace. namespace are used to resolve ambiguity. and using keyword are allowing to use ability of namespace in our program.

/*Basic example of namespace keyword.*/
#include<iostream>
//use predefined namespace std
using namespace std;
//create user defined namespace
namespace Book{
  /*---------
  +  Here defined namespace variables
    and function
  -----------*/  
  int myBook; //integer variable
}
//use user defined namespace
using namespace Book;
int main(){
  //Directly access namespace Book variable
    myBook=100;
    //display variable value
    cout<<myBook;
  return 0;
}
Output
100

In this program used one predefined namespace std. and also created one user defined namespace Book. if we are not used using namespace Book in this code. then we cannot access directly Book namespace variable in our program in this situation we are need to the namespace name (::) scope resolution operator and variable name. look at this example.

 /*Basic example of namespace keyword*/
#include<iostream>
//use predefined namespace std
using namespace std;
//create user defined namespace
namespace Book{
  /*---------
  +  Here defined namespace variables
    and function
  -----------*/  
  int myBook; //integer variable
}
int main(){
  //namespace-name ::(Scope resolution operator) variable-name
    Book::myBook=100;
    //display variable value
    cout<<Book::myBook;
  return 0;
}
Output
100

basic data type (char,int,float,double) keywords

There is an built in data type. they are normally used in c++ program to create variables. see the example.

/*Declared varaibles*/
int age=21; //create integer variable
float salary; //create float variable
double amount; //create double variable
char status='T'; //create char. variable

Type Modifiers (long, short, signed, unsigned) keywords

This are normally used to change the behaviour of inbuilt data type. look at this example.

#include<iostream>
using namespace std;
int main(){
  short int age=21;
  unsigned int auxiliary=-7893;
  signed int candy=10000;
  long int salary=9048999;
  cout<<"short int age : "<<age<<endl;
  cout<<"unsigned int auxiliary : "<<auxiliary<<endl;
  cout<<"signed int candy : "<<candy;
  cout<<"long int salary : "<<salary; 
  return 0;
}
Output
short int age : 21
unsigned int auxiliary : 77893
signed int candy : -10000
long int salary : 9048999

return vs void keywords

return is statement. normally they are used to return value and reference by function. And void is data type this are used in two places. when function are not return any value. and other one creates of pointer variable. for example.

#include<iostream>
using namespace std;
//void is return type of this function
void config(){
  
  int age=21;
  //assign address of integer variable
  void *void_ptr=&age;
  /*directly access (*void_ptr) is produced some 
  c++ compiler are produce error
  like('void*' is not a pointer-to-object type)
    then need to typecasting. 
    */
  cout<<"Age :"<<*(int *)void_ptr<<endl;
}
/*
int: return type
divide : name of function
x,y : integer parameter
*/
int divide(int x,int y){

  if(y==0){
    cout<<"Divide by zero"<<endl;

    return 0; //back to main function
  }
  return (x/y); //return result
}
//int is return type of function
int main(){
  config();
  cout<<"(10/5): "<<divide(10,5);
  return 0;
}
Output
Age :21
(10/5): 2

storage class (auto,static,extern,register)

auto are used in block level local variable. normally all local variable is by default in auto.

static variable are special type variable. they are create only one time in a program. that means only one time memory allocates.Used of those variable (local or locally ) and (global static globally).

const volatile and typedef keywords

const are used to create constant variable. that means cannot modify variable value during program execution. see example.

#include<iostream>
using namespace std;
int main(){
  const int week_day=7;
  const float PI=3.1415f;
  cout<<"week_day :"<<week_day<<endl;
  cout<<"PI value :"<<PI<<endl;
  //not allowed to change constant variables
  //week_day=6;
  return 0;
}
Output
week_day :7
PI value :3.1415

note that in this program cannot change variable value.

volatile Declare of any type of variable (include of constant) are volatile. This are provide the facility to modified the variable value during the program execution. see example.

#include<iostream>
using namespace std;
int main(){
  const volatile int working_day=7;
  cout<<"Before Working Days :"<<working_day<<endl;
  
  int *ptr=(int*)&working_day;
  //allowed to change constant variables
  *ptr=6;
  cout<<"After Working Days :"<<working_day<<endl;
  return 0;
}
Output
Before Working Days :7
After Working Days :6

typedef are use to provide new name of predefined and user defined data type. for example.

#include<iostream>
using namespace std;
//given a readable name of char
typedef char alphabet;

int main(){
  //create variables
  alphabet capital='A';
  alphabet small='a';
  //display values
  cout<<"capital : "<<capital<<endl;
  cout<<"small   : "<<small<<endl;
  return 0;
}
Output
capital : A
small   : a

C++ struct enum union keywords

This keyword are used to create user defined data types.

structure are used to combine collection of multiple and single type of data. most of data structures are used to structures.

struct tag_name{
  //data_members;
  //functions(){}
}variable_list;

let as view an example to store students record using structure.

//example of (structure) strcut keyword
#include<iostream>
#include<string> //for string
using namespace std;
struct student{
  int id;
  string name;
  char gender;
  float marks;
};
/*
  display student variable info
*/
void display(student &info){
  cout<<" name  :"<<info.name<<endl;
  cout<<" id    :"<<info.id<<endl;
  cout<<" gender:"<<info.gender<<endl;
  cout<<" marks :"<<info.marks<<endl;
}
/*
  function : setValue
  parameter : 5 values 
  Assign initial value to student variables
*/
void setValue(student &var,
              int id,
              string name,
              char gender,
              float marks){
    //assign values
    var.id=id;
    var.name=name;
    var.gender=gender;
    var.marks=marks;
}
int main(){
  //create variable of student structure
  student s1,s2;

  setValue(s1,1,"lusen",'F',400.23f);
  setValue(s2,2,"heri ",'M',410.13f);

  //display values
  display(s1);
  display(s2);
  return 0; 
}
Output
 name  :lusen
 id    :1
 gender:F
 marks :400.23
 name  :heri
 id    :2
 gender:M
 marks :410.13

Note that here create two variables of structure. those variable can access data member value using dot operator. learn more about this in structure post.

C++ Enumeration (enum)This are used to create multiple constant integer value in our program. enum is an keyword followed by enum name. enumeration are simplest user defined data type. There are defined by following syntax.

//Declaration of enumeration
enum tag_name{
  //enum body
  //constant_variables
}variable_list;

for example

#include<iostream>
using namespace std;
enum days{
  monday,//default 0
  tuesday,//1
  wednesday,//2 
  thursday,//3  
  friday,//4  
  saturday,//5  
  sunday//6
};
int main(){
  //access value
  cout<<"sunday : "<<sunday; //

  return 0; 
}
Output
sunday : 6

Note that enumeration are work only integer value. when not assign any value to those variable then so default value are assign those variable. First variable are assign 0 and remaining are increment by one. learn more about enumeration in enumeration post.

union are capable to define multiple data member (data type ) within the union. But union data member variable memory are shared. so help of union variable can utilize only one member.

union tag_name{
 // data_members;
 }variable_list;

For example

//union example in C++
#include<iostream>
using namespace std;
union Sim4G{
  //data ,member
  int airtel;
  float jio;
  double docomo;
};
int main(){

  Sim4G u1,u2,u3;
  //assign values
  u1.airtel=10;
  u2.jio=30;
  u3.docomo=5;
  //display values
  cout<<" u1 airtel :"<<u1.airtel<<endl;
  cout<<" u2 jio :"<<u2.jio<<endl;
  cout<<" u3 docomo :"<<u3.docomo<<endl;
  return 0; 
}
Output
 u1 airtel :10
 u2 jio :30
 u3 docomo :5

c++ and keyword

This are and alternative for (&&) operator. this are mostly use to check conditional statement. let see the example

/*
  Example of and keyword
*/
#include<iostream>
using namespace std;

int main(){

  int value1=10,value2=20;
  /*
   "and" operator are check both Left are Right condition
   are valid or not.
  */
  if(value1>5 and value2 < 30){
    cout<<" Condition True";
  }else{
    cout<<" Condition False";
  } 
}
Output
 Condition True

c++ and_eq keyword

and_eq are perform special task first they are check both operand and perform and operation then result will assign to left operand. this are alternate of (&=).

/*
  Example of and_eq keyword
*/
#include<iostream>
using namespace std;

int main(){
  int binary_1 = 0b1010; //binary of 10
  int binary_2 = 0b1000; //binary of 8
  cout<<"binary_1 :"<<binary_1<<endl;
  cout<<"binary_2 :"<<binary_2<<endl;
  /*
    Result like this

    1 0 1 0
  & 1 0 0 0
    -------
  = 1 0 0 0 
    //result as and operation of two binary
  */
  binary_2 and_eq binary_1;
  cout<<"After binary_2 :"<<binary_2;
}
Output
binary_1 :10
binary_2 :8
After binary_2 :8

C++ bitand Keyword

this is alternative to bitwise (&) operator. see example how it will work.

/*
  Example of bitand keyword
*/
#include<iostream>
using namespace std;

int main(){
  int byte1 = 0b1010; //binary of 10
  int byte2 = 0b1100; //binary of 12
  cout<<"byte1 :"<<byte1<<endl;
  cout<<"byte2 :"<<byte2<<endl;
  
  /*******************
  //   Result like this
  //
  //   10 = 1 0 1 0 
  //&  12 = 1 1 0 0
  //  -------------
  //   8  = 1 0 0 0 
  //  result as and operation of two binary
  *********************/
  int result=byte1 bitand byte2;
  cout<<"result :"<<result;
}
Output
byte1 :10
byte2 :12
result :8

C++ bitor keyword

This is alternative to bitwise (|) operator. view example.

/*
  Example of bitor keyword
*/
#include<iostream>
using namespace std;

int main(){
  int byte1 = 0b1010; //binary of 10
  int byte2 = 0b1100; //binary of 12
  cout<<"byte1 :"<<byte1<<endl;
  cout<<"byte2 :"<<byte2<<endl;

  /*******************
  //   Result like this
  //
  //   10 = 1 0 1 0 
  // | 12 = 1 1 0 0
  //  -------------
  //   14 = 1 0 0 0 
    //  result as  (| bitwise or) operation of two binary
  *********************/
  int result=byte1 bitor byte2;
  cout<<"result:"<<result;
}
Output
byte1 :10
byte2 :12
result:14

C++ compl keyword

This is alternative of Bitwise complement (~) operator.

/*
  Example of coml keyword
*/
#include<iostream>
using namespace std;

int main(){
  int value=3;
  cout<<"[  ~  ] :"<<~value<<endl;
  cout<<"[compl] :"<<compl value<<endl;
}
Output
[  ~  ] :-4
[compl] :-4


c++ reinterpret_cast keyword

This keyword are used to typecasting of pointer variable and object of class. there are no matter which type of pointer are given by program. see example.

/*
  Example of reinterpret_cast keyword
*/
#include<iostream>
using namespace std;

int main(){
  //status is integer variable
  int status=97;

  /*------->
  auxiliary is integer pointer variable. 
  assign the status variable address to this variable
  <--------*/
  int *auxiliary=&status;
  
  /*----->
    reinterpret_cast is keyword. 
    In this case they are typecast of integer pointer
    Variable into another char pointer variable.
   <------*/
  char *ptr=reinterpret_cast<char*>(auxiliary);

  //display results
  cout<<"status     :"<<status<<endl;
  cout<<"*auxiliary :"<<*auxiliary<<endl;
  cout<<"*ptr       :"<<*ptr;
}
Output
status     :97
*auxiliary :97
*ptr       :a

Note that reinterpret_cast are accept one parameter. this parameter we can provide predefined and user defined pointer variable. see the syntax.


reinterpret_cast (pointer_variable_name)

view another example.

#include<iostream>
using namespace std;

class student{
  public:
  void attendance(){
    cout<<"student class function";
  }
  
};
class teacher{
  public:
    void inform(){
    cout<<"teacher class function";
  }
  
};
int main(){
  //create object to student class
  student *ptr=new student();

  teacher *meeting=reinterpret_cast<teacher *>(ptr);
    //calling function
  meeting->inform();
  
}
teacher class function

© 2019, regularcodes.com, All rights reserved