Within the realm of C++ programming, reserved words are terms having predetermined functions and meanings. They serve as the cornerstone of C++ syntax and grammar, defining the functions and relationships between the code. These terms cannot be used as identifiers, such as variable names, function names, or class names, because they are components of the core language.
Table of Contents
1. Data Type Keywords
These terms specify the kinds of data that can be stored in a variable.
- Int: Stands for integer numbers; for example, int a = 10;..
- float: For single-precision floating-point numbers, use the operator float (for example, float b = 3.14;).
- double: Applied to floating-point numbers with double precision (for example, double c = 3.1415;).
- char: Denotes a single character (for example, char d = ‘A’)
- bool: Stands for true or false boolean values.
2. Control Flow Keywords
These keywords regulate a program’s flow of execution.
- if: Introduces a conditional statement (e.g.,
if (x > y) {...}
). - else: Provides an alternative path for the
if
statement (e.g.,if (...) {...} else {...}
). - switch: Used for multi-way branching (e.g.,
switch (x) {...}
). - case: Defines a block within a
switch
statement (e.g.,case 1: ...
). - for: Initiates a loop that runs a specific number of times (e.g.,
for (int i = 0; i < n; i++) {...}
). - while: Initiates a loop that continues while a condition is true (e.g.,
while (x > 0) {...}
). - do: Used with
while
to run a block at least once (e.g.,do {...} while (x > 0);
). - break: Exits from a loop or switch (e.g.,
break;
). - continue: Skips the rest of the loop’s current iteration (e.g.,
continue;
).
3. Class and Object Keywords
Classes and objects are defined and managed with the aid of these keywords in the object-oriented programming language C++.
- class: Defines a user-defined data type (e.g.,
class MyClass {...};
). - public, private, protected: Control access to members of a class.
- public members are accessible outside the class.
- private members are only accessible within the class
- protected members are accessible in derived classes.
- this: Refers to the current object instance (e.g.,
this->variable
). - new: Allocates memory dynamically (e.g.,
int* ptr = new int;
). - delete: Deallocates memory allocated with
new
(e.g.,delete ptr;
).
Learn on Youtube:
What are Keywords and Identifiers?
4. Function Keywords
In C++, these keywords define and control functions.
- void: Specifies that a function does not return a value (e.g.,
void functionName() {...}
). - return: Exits a function and optionally returns a value (e.g.,
return 0;
). - inline: Suggests that the compiler replaces the function call with the function code itself (e.g.,
inline int sum(int a, int b) {...}
).
5. Memory Management Keywords
In C++, these keywords are used to directly manipulate memory.
- new: Allocates memory for an object dynamically (e.g.,
int* ptr = new int;
). - delete: Deallocates memory that was previously allocated (e.g.,
delete ptr;
). - sizeof: Returns the size of a data type or object in bytes (e.g.,
sizeof(int)
).
6. Type Modifiers
These keywords alter the variable type to define particular attributes.
- const: Defines a variable whose value cannot change (e.g.,
const int x = 10;
). - static: Keeps a variable or function’s value persistent across function calls (e.g.,
static int count = 0;
). - volatile: Tells the compiler that the value of a variable can change at any time, preventing optimization (e.g.,
volatile int sensorValue;
).
7. Other Important Keywords
Other crucial terms that are worth mentioning include:
- namespace: Defines a scope to prevent name conflicts (e.g.,
namespace myNamespace {...}
). - try, catch, throw: Used for exception handling (e.g.,
try {...} catch (exception e) {...}
). - enum: Defines a set of named integer constants (e.g.,
enum Color { RED, GREEN, BLUE };
). - typedef: Creates a new name for an existing type (e.g.,
typedef int MyInteger;
).
Conclusion
The language’s building pieces are C++ keywords. They offer the framework and syntax required to write effective and efficient code. Learning each keyword’s function and usage is crucial to becoming an expert C++ programmer and creating reliable apps.
You may utilise these keywords effectively to fully utilise C++ and develop applications that are robust and scalable.
Learn More:
Comments in C++