-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathcpp.cpp
More file actions
112 lines (92 loc) · 1.64 KB
/
cpp.cpp
File metadata and controls
112 lines (92 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Comments
// Single-line comment
/*
* Multi-line
* comment
*/
#include <iostream>
#include <memory>
#define CONST_VAL 100
// Numbers (including C++14 digit separators)
42;
3.14;
.5;
1e10;
1.5e-3;
0xff;
0xFF;
0b1010'1100;
1'000'000;
42ull;
3.14f;
// Constants
true;
false;
NULL;
nullptr;
// Strings and Characters
'c';
'\t';
"double quotes with escape: \" \n \t \\";
// Control flow keywords
int main() {
if (true) {
} else if (false) {
} else {
}
for (int i = 0; i < 10; ++i) {
if (i == 5) continue;
if (i == 8) break;
}
while (false) { }
do { } while (false);
switch (1) {
case 1: break;
default: break;
}
try {
throw std::runtime_error("oops");
} catch (const std::exception& e) {
// handle error
}
return 0;
}
// Modern C++ keywords and features
template <typename T>
concept Addable = requires(T a, T b) {
a + b;
};
class Animal {
private:
int age;
protected:
bool is_alive;
public:
Animal() : age(0), is_alive(true) {}
virtual ~Animal() = default;
virtual void speak() const = 0;
};
class Dog final : public Animal {
public:
void speak() const override {
std::cout << "Woof!" << std::endl;
}
};
constexpr int get_magic_number() {
return 42;
}
// Other keywords
namespace my_space {
enum class State { START, STOP };
inline void helper() {
auto val = get_magic_number();
decltype(val) val_copy = val;
Dog* dog = new Dog();
delete dog;
}
}
using namespace my_space;
export module my_module;
// Function calls
std::cout << "hello\n";
sizeof(int);