Keywords With Sample Case Use
Keywords With Sample Case Use
1. asm
asm allows you to insert assembly language instructions within C++ code. This
is rarely used nowadays.
================================================
#include <iostream>
int main() {
int result;
asm("movl $42, %0" : "=r"(result)); // Move 42 into result using
assembly
std::cout << "Result: " << result << std::endl;
return 0;
}
2. auto
================================================
#include <iostream>
int main() {
auto x = 5.5; // x is deduced as double
auto y = 10; // y is deduced as int
std::cout << "x: " << x << ", y: " << y << std::endl;
return 0;
}
3. volatile
volatile tells the compiler that a variable's value may change unexpectedly
(e.g., in multithreading or hardware-interrupt scenarios).
================================================
volatile int flag = 1;
void someFunc() {
while (flag) { /* Keep checking the flag */ }
// the compiler won’t optimize out this loop
}
4. while
================================================
int main() {
int i = 0;
while (i < 5) {
std::cout << i++ << std::endl;
}
return 0;
}
5. bool
================================================
#include <iostream>
int main() {
bool isActive = true;
if (isActive) std::cout << "Active" << std::endl;
return 0;
}
6. break
================================================
#include <iostream>
int main() {
for (int i = 0; i < 10; ++i) {
if (i == 5) break; // Exit when i == 5
std::cout << i << " ";
}
return 0;
}
7. case
================================================
#include <iostream>
int main() {
int x = 2;
switch (x) {
case 1: std::cout << "One"; break;
case 2: std::cout << "Two"; break;
default: std::cout << "Other"; break;
}
return 0;
}
8. catch
================================================
#include <iostream>
int main() {
try {
throw std::runtime_error("An error occurred");
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
return 0;
}
9. char
================================================
#include <iostream>
int main() {
char letter = 'A';
std::cout << letter << std::endl;
return 0;
}
10. class
================================================
class Person {
public:
std::string name;
int age;
Person(std::string n, int a) : name(n), age(a) {}
void display() { std::cout << name << " is " << age << " years
old." << std::endl; }
};
11. const
================================================
const int PI = 3.14159;
12. const_cast
================================================
#include <iostream>
void print(const int* ptr) {
int* modifiable = const_cast<int*>(ptr);
*modifiable = 100; // Modify even though it was const
}
int main() {
int x = 10;
print(&x);
std::cout << x; // Prints 100
return 0;
}
13. continue
================================================
#include <iostream>
int main() {
for (int i = 0; i < 5; ++i) {
if (i == 2) continue; // Skip 2
std::cout << i << std::endl;
}
return 0;
}
14. default
================================================
#include <iostream>
int main() {
int x = 3;
switch (x) {
case 1: std::cout << "One"; break;
default: std::cout << "Default case"; break;
}
return 0;
}
15. delete
16. do
================================================
#include <iostream>
int main() {
int i = 0;
do {
std::cout << i << std::endl;
i++;
} while (i < 5);
return 0;
}
17. double
================================================
double pi = 3.14159;
18. dynamic_cast
================================================
class Base { virtual void func() {} };
class Derived : public Base {};
Base* basePtr = new Derived();
Derived* derivedPtr = dynamic_cast<Derived*>(basePtr); // Safe
downcast
19. else
================================================
#include <iostream>
int main() {
int x = 10;
if (x < 5) std::cout << "Less than 5";
else std::cout << "5 or more";
return 0;
}
20. enum
================================================
enum Color { Red, Green, Blue };
Color c = Red;
21. except
================================================
__try {
// SEH code block
} __except (EXCEPTION_EXECUTE_HANDLER) {
// Handle exception
}
22. explicit
================================================
class A {
public:
explicit A(int x) {}
};
A obj1 = 10; // Error: no implicit conversion
A obj2(10); // OK
23. extern
================================================
extern int x;
24. false
================================================
bool flag = false;
25. finally
================================================
try {
// Code
} finally {
// Cleanup code
}
26. float
================================================
float temperature = 36.6f;
27. for
================================================
for (int i = 0; i < 10; ++i) {
std::cout << i << std::endl;
}
28. friend
================================================
class B;
class A {
int secret;
friend class B;
};
class B {
A a;
void reveal() { std::cout << a.secret; }
};
29. goto
Jumps to a labeled statement within the same function.
================================================
int main() {
int x = 0;
start:
std::cout << x << std::endl;
x++;
if (x < 5) goto start; // Jump to label
return 0;
}
30. if
================================================
#include <iostream>
int main() {
int a = 10;
if (a > 5) {
std::cout << "a is greater than 5" << std::endl;
}
return 0;
}
31. inline
Suggests that the function be inlined, reducing function call overhead (though
the compiler ultimately decides).
================================================
inline int square(int x) {
return x * x;
}
int main() {
std::cout << square(5) << std::endl;
return 0;
}
32. int
================================================
int age = 25;
33. long
Defines an extended-size integer.
================================================
long population = 7800000000;
34. mutable
================================================
class Test {
public:
mutable int x;
void modify() const { x = 100; } // Allowed since x is mutable
};
35. namespace
================================================
namespace First {
void func() { std::cout << "In First Namespace" << std::endl; }
}
namespace Second {
void func() { std::cout << "In Second Namespace" << std::endl; }
}
int main() {
First::func();
Second::func();
return 0;
}
36. new
================================================
int* ptr = new int(100);
delete ptr;
37. operator
================================================
class Complex {
int real, imag;
public:
Complex(int r, int i) : real(r), imag(i) {}
Complex operator+(const Complex& rhs) {
return Complex(real + rhs.real, imag + rhs.imag);
}
};
38. private
Specifies that members are accessible only within the class itself.
================================================
class A {
private:
int secret;
};
39. protected
Specifies that members are accessible within the class and derived classes.
================================================
class A {
protected:
int secret;
};
class B : public A {
void reveal() { std::cout << secret; }
};
40. public
================================================
class A {
public:
int number;
};
41. register
Requests that the variable be stored in a CPU register (this is largely ignored by
modern compilers).
================================================
register int counter = 0;
42. reinterpret_cast
================================================
int x = 42;
void* ptr = &x;
int* intPtr = reinterpret_cast<int*>(ptr);
43. return
================================================
int square(int x) {
return x * x;
}
44. short
================================================
short height = 150;
45. signed
Specifies that a variable can hold both positive and negative values (explicitly
marked).
================================================
signed int balance = -100;
46. sizeof
================================================
std::cout << "Size of int: " << sizeof(int) << std::endl;
47. static
Defines a variable that retains its value across function calls or a member that is
shared among all objects of the class.
================================================
class A {
public:
static int count;
A() { ++count; }
};
int A::count = 0;
48. static_cast
================================================
float f = 3.14;
int i = static_cast<int>(f); // Convert float to int
49. struct
================================================
struct Point {
int x, y;
};
50. switch
================================================
int main() {
int day = 2;
switch (day) {
case 1: std::cout << "Monday"; break;
case 2: std::cout << "Tuesday"; break;
default: std::cout << "Other";
}
return 0;
}
51. template
52. this
================================================
class A {
int x;
public:
A(int x) : x(x) {}
void show() { std::cout << "x = " << this->x << std::endl; }
};
53. throw
Throws an exception.
================================================
#include <iostream>
void test(int value) {
if (value == 0) throw std::invalid_argument("Value cannot be
zero");
}
int main() {
try {
test(0);
} catch (const std::exception& e) {
std::cout << e.what() << std::endl;
}
return 0;
}
54. true
================================================
bool success = true;
55. try
================================================
try {
// Code that may throw
} catch (std::exception& e) {
std::cout << e.what();
}
56. type_info
================================================
#include <iostream>
#include <typeinfo>
int main() {
int x = 10;
std::cout << typeid(x).name() << std::endl; // Outputs type
information of x
return 0;
}
57. typedef
================================================
typedef unsigned long ulong;
ulong population = 7800000000;
58. typeid
================================================
#include <iostream>
#include <typeinfo>
int main() {
double d = 5.0;
std::cout << typeid(d).name() << std::endl; // Outputs "double"
return 0;
}
59. typename
Used in template definitions to specify a placeholder for a type.
================================================
template <typename T>
class MyClass {
T data;
public:
MyClass(T d) : data(d) {}
};
60. union
Defines a data structure where all members share the same memory location.
================================================
union Data {
int i;
float f;
};
Data data;
data.i = 10;
std::cout << data.i;
61. unsigned
================================================
unsigned int score = 100;
62. using
================================================
using namespace std; // Bring everything from std into scope
using myInt = int; // Alias for int
63. virtual
64. void
================================================
void printMessage() {
std::cout << "Hello World" << std::endl;
}