-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.cpp
More file actions
executable file
·65 lines (55 loc) · 1.28 KB
/
scope.cpp
File metadata and controls
executable file
·65 lines (55 loc) · 1.28 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
#include <iostream>
#include <typeinfo>
#include <string>
#include <cmath>
#define print(x) std::cout << x << std::endl
// for a static variable or function, the name can only be used in the file
static int private_var = 5;
enum Example
{
A = -10, B, C
};
namespace space1
{
enum Animal: char
{
// the value in "enum" must be an integer
SHEEP,
COW,
DONKEY,
FISH
};
int add(int a, int b)
{
return a + b;
}
}
namespace space2
{
// for a inline function, it means that whatever code defined in the function
// will be copied to the place where the function is executed instead of just
// calling the specific function saved in a memory.
inline int add(int a, int b)
{
return (a + b) * 2;
}
}
int main(int argc, char const *argv[])
{
print(space1::SHEEP);
print(space1::Animal::FISH);
print("Example A,B,C val: " << A << " " << B << " " << C);
// new assignment can be made but make sure that the value can only be
// those included in "enum" such as A, B, or C.
space1::Animal DOG = space1::FISH;
print(DOG);
Example val = B;
print(val);
// but mind that the new assigned variable does not belong to the space
// or the name of "enum". It only represents itself.
int ans1 = space1::add(1, 2);
int ans2 = space2::add(1, 2);
print(ans1);
print(ans2);
return 0;
}