Skip to content

Commit fc0b810

Browse files
committed
Add core/basics
1 parent c6eba89 commit fc0b810

3 files changed

Lines changed: 194 additions & 1 deletion

File tree

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ set(APP_HEADERS
7676
# Core source files
7777
set(APP_SOURCES
7878
"src/DeleteMe.cpp"
79-
"src/core/1_basic.cpp"
79+
"src/core/basics/InitializeVariable.cpp"
80+
"src/core/basics/Operations.cpp"
8081
)
8182

8283
# Test files
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void initialize_variable();
5+
6+
// A struct that runs code when its object is created
7+
struct InitializeVariable
8+
{
9+
InitializeVariable()
10+
{
11+
cout << "\n"
12+
<< "\n"
13+
<< "InitializeVariable\n";
14+
initialize_variable();
15+
}
16+
};
17+
18+
// All global and static objects are constructed before main() begins.
19+
static InitializeVariable autoRunInstance;
20+
21+
struct Foo
22+
{
23+
Foo()
24+
{
25+
cout << "Default constructor/ default init\n";
26+
}
27+
28+
// explicit Foo(int)
29+
Foo(int)
30+
{
31+
cout << "Constructor called with int / copy init\n";
32+
}
33+
34+
Foo(const Foo &other)
35+
{
36+
std::cout << "Copy constructor called\n";
37+
}
38+
};
39+
40+
void initialize_variable()
41+
{
42+
cout << "\n--- Variable Initialization Examples ---\n";
43+
// There are there common ways to intialize a variable
44+
// * Default
45+
int initDefaultVar;
46+
Foo initDefaultObj;
47+
48+
// *Traditional
49+
// * copy-init: Type var = value;
50+
// 1. Compiler tries to create a temporary Foo from 2.3 by implicit conversion (calls Foo(<implicit int to double>) ).
51+
// 2. If constructor is explicit, implicit conversion is not allowed → error.
52+
// 3. Otherwise, temporary Foo is created and then copy/move into copyInitObj2.
53+
Foo copyInitObj = 2.3; // implicit 2.3(float) -> 2(int) -> call Foo(int) -> create a temporary Foo -> is copied or moved into copyInitObj
54+
55+
// * direct-init: Type var(value);
56+
Foo directInitObj(4); // call Foo(int)
57+
Foo directInitObj2(4.3); // look for constructor -> implicit 4.3(float) -> 4(int) -> call Foo(int) ->
58+
59+
// * Brace init
60+
// calls the constructor directly without allowing implicit conversions.
61+
Foo braceInit{3};
62+
// Foo braceInit2{3.3}; // ERORR => Prefer this way
63+
}

src/core/basics/Operations.cpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void arithmeticOperator();
5+
void logicalOperator();
6+
void bitWiseOperator();
7+
8+
struct Operations
9+
{
10+
Operations()
11+
{
12+
cout << "\n"
13+
<< "\n"
14+
<< "Operation\n";
15+
arithmeticOperator();
16+
logicalOperator();
17+
bitWiseOperator();
18+
}
19+
};
20+
21+
static Operations autoRunInstance;
22+
23+
void arithmeticOperator()
24+
{
25+
cout << "\n--- ArithmeticOperator Examples ---\n";
26+
int a{100}, b{200};
27+
28+
// Addition
29+
cout << "a = " << a << ", b = " << b << "\n";
30+
int sum = a + b;
31+
cout << "sum = " << sum << "\n";
32+
33+
// Subtraction
34+
cout << "a = " << a << ", b = " << b << "\n";
35+
int different = a - b;
36+
cout << "different = " << different << "\n";
37+
38+
// Multiplication
39+
cout << "a = " << a << ", b = " << b << "\n";
40+
int product = a * b;
41+
cout << "product = " << product << "\n";
42+
43+
// Division
44+
cout << "a = " << a << ", b = " << b << "\n";
45+
int quotient = a / b;
46+
cout << "quotient = " << quotient << "\n";
47+
48+
// Modulus
49+
cout << "a = " << a << ", b = " << b << "\n";
50+
int remainder = a % b;
51+
cout << "remainder = " << remainder << "\n";
52+
53+
// Increment
54+
cout << "a = " << a << "\n";
55+
int preIn = ++a; // increase a, return copy
56+
cout << "preIn = " << preIn << "\n";
57+
58+
cout << "a = " << a << "\n";
59+
int postIn = a++; // copy a to a copy, then increase a, return copy
60+
cout << "postIn = " << preIn << "\n";
61+
62+
// Decrement
63+
cout << "b = " << b << "\n";
64+
int preDe = --b;
65+
cout << "preDe = " << preDe << "\n";
66+
67+
cout << "b = " << b << "\n";
68+
int postDe = b--;
69+
cout << "postDe = " << postDe << "\n";
70+
71+
// Comma:
72+
cout << "a = " << a << ", b = " << b << "\n";
73+
int value = (a, b); // evalue a then b, return value of b
74+
cout << "Comma(a,b) = " << value << "\n";
75+
}
76+
77+
void logicalOperator()
78+
{
79+
cout << "\n--- LogicalOperator Examples ---\n";
80+
bool a = true;
81+
bool b = false;
82+
bool c = true;
83+
84+
cout << boolalpha; // show true/false instead of 1/0
85+
cout << "a = " << a << ", b = " << b << ", c = " << c << "\n\n";
86+
87+
// AND (&&)
88+
cout << "[AND] a && b = " << (a && b) << "\n";
89+
90+
// OR (||)
91+
cout << "[OR ] a || c = " << (a || c) << "\n";
92+
93+
// NOT (!)
94+
cout << "[NOT] !c = " << (!c) << "\n";
95+
}
96+
97+
#include <bitset>
98+
void bitWiseOperator()
99+
{
100+
cout << "\n--- BitWiseOperator Examples ---\n";
101+
bitset<8> bitsA{0b1111'1111};
102+
bitset<8> bitsB{0b1111'0000};
103+
104+
cout << "bitA = " << bitsA << ", bitB = " << bitsB << "\n";
105+
106+
// AND
107+
bitset<8> result = bitsA & bitsB;
108+
cout << "bitA && bitB= " << result << "\n";
109+
110+
// OR
111+
result = bitsA | bitsB;
112+
cout << "bitA | bitB= " << result << "\n";
113+
114+
// XOR
115+
result = bitsA ^ bitsB;
116+
cout << "bitA ^ bitB= " << result << "\n";
117+
118+
// NOT
119+
result = ~bitsA;
120+
cout << "~bitA = " << result << "\n";
121+
122+
// LEFT SHIFT
123+
result = bitsA << 1;
124+
cout << "bitA << 1 = " << result << "\n";
125+
126+
// RIGHT SHIFT
127+
result = bitsA >> 1;
128+
cout << "bitA >> 1 = " << result << "\n";
129+
}

0 commit comments

Comments
 (0)