Skip to content

Commit 6360dc1

Browse files
authored
Merge pull request #4 from urboob21/dev_branch
2025_11_09
2 parents c4661f4 + 338025d commit 6360dc1

9 files changed

Lines changed: 660 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ set(APP_SOURCES
7979
"src/core/basics/InitializeVariable.cpp"
8080
"src/core/basics/Operations.cpp"
8181
"src/core/basics/ControlFlow.cpp"
82+
"src/core/datatypes/Fundamental.cpp"
83+
"src/core/datatypes/CArray.cpp"
84+
"src/core/datatypes/CReferences.cpp"
85+
"src/core/datatypes/CPointers.cpp"
86+
"src/core/datatypes/CEnum.cpp"
87+
"src/core/datatypes/CStruct.cpp"
88+
"src/core/datatypes/CUnion.cpp"
89+
"src/core/datatypes/TypeConVersions.cpp"
8290
)
8391

8492
# Test files

src/core/datatypes/CArray.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
3+
void arrayExamples()
4+
{
5+
std::cout << "\n--- Array Examples ---\n";
6+
7+
const int arr[5] = {1, 2, 3, 4, 5};
8+
for (int i = 0; i < 5; ++i)
9+
{
10+
std::cout << "arr[" << i << "] = " << arr[i] << "\n";
11+
}
12+
13+
double matrix[2][3] = {{1.1, 2.2, 3.3}, {4.4, 5.5, 6.6}};
14+
for (int i = 0; i < 2; ++i)
15+
{
16+
for (int j = 0; j < 3; ++j)
17+
{
18+
std::cout << "matrix[" << i << "][" << j << "] = " << matrix[i][j] << "\n";
19+
}
20+
}
21+
}
22+
23+
struct CArray
24+
{
25+
CArray() { arrayExamples(); }
26+
};
27+
28+
static CArray autoRunArray;

src/core/datatypes/CEnum.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <iostream>
2+
#include <cstdint> // for uint8_t
3+
4+
using namespace std;
5+
// A type defined in terms of other types
6+
7+
// *0. Define some enums
8+
// Unscope enum
9+
enum BasicEnum
10+
{
11+
enumratorA,
12+
enumratorB,
13+
enumratorC
14+
};
15+
16+
// Scoped enum - enumrators are inside enum's scope
17+
enum class ScopeEnumClass
18+
{
19+
enumratorA,
20+
enumratorB,
21+
enumratorC
22+
};
23+
24+
// Scoped enum inside a namespace
25+
namespace EnumNameSpace
26+
{
27+
enum class ScopeEnumClass
28+
{
29+
enumratorA,
30+
enumratorB,
31+
enumratorC
32+
};
33+
}
34+
35+
// Scoped enum with explicit base type
36+
enum class ScopeEnumClassB : uint8_t
37+
{
38+
enumratorA = 0,
39+
enumratorB = 1,
40+
enumratorC = 2
41+
};
42+
43+
void enums()
44+
{
45+
cout << "\n--- Enum Type Examples ---\n";
46+
// *1. Using unscope enum
47+
[[maybe_unused]] BasicEnum unscope_e = enumratorA;
48+
49+
// *2. Using scoped enum
50+
[[maybe_unused]] ScopeEnumClass scope_e_c = ScopeEnumClass::enumratorA;
51+
52+
// *3. Using scoped enum namespace
53+
[[maybe_unused]] EnumNameSpace::ScopeEnumClass scope_e_c_n = EnumNameSpace::ScopeEnumClass::enumratorA;
54+
55+
// *4. Using scoped enum with base type
56+
[[maybe_unused]] ScopeEnumClassB st = ScopeEnumClassB::enumratorA;
57+
}
58+
59+
// A struct that runs code when its object is created
60+
struct CEnum
61+
{
62+
CEnum()
63+
{
64+
cout << "\n"
65+
<< "\n"
66+
<< "Compound type: Enum\n";
67+
68+
enums();
69+
}
70+
};
71+
72+
// All global and static objects are constructed before main() begins.
73+
static CEnum autoRunInstance;

src/core/datatypes/CPointers.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include <iostream>
2+
using namespace std;
3+
// A type defined in terms of other types
4+
5+
// Simple function to demonstrate function pointers
6+
int foo(int x)
7+
{
8+
cout << "x = " << x << endl;
9+
return x;
10+
}
11+
12+
void byPtr(int *x)
13+
{
14+
(*x)++;
15+
cout << "x = " << *x << endl;
16+
}
17+
18+
void byConstPtr(const int *const x)
19+
{
20+
// *x++; // error
21+
cout << "x = " << *x << endl;
22+
}
23+
24+
void byPtrConst(const int *x)
25+
{
26+
// *x++; // error
27+
cout << "x = " << *x << endl;
28+
}
29+
30+
void byConstPtrConst(const int *const x)
31+
{
32+
// *x++; // error
33+
cout << "x = " << *x << endl;
34+
}
35+
36+
static int global = 42;
37+
int *returnPtr()
38+
{
39+
return &global; // the object must outlive the reference.
40+
}
41+
42+
void pointers()
43+
{
44+
cout << "\n--- Pointers Type Examples ---\n";
45+
int a = 10;
46+
cout << "a = " << a << "\n";
47+
48+
// * 1. Pointer basics
49+
const int *null_ptr = nullptr;
50+
cout << "Address of nullptr (null_ptr): " << null_ptr << "\n";
51+
52+
// '&' gives the address of a variable
53+
int *ptr_a = &a;
54+
cout << "Address of a (&a): " << &a << "\n";
55+
cout << "Value stored in pointer (ptr_a): " << ptr_a << "\n";
56+
57+
// '*' dereferences a pointer (accesses the value at that address)
58+
cout << "Value of a via *ptr_a: " << *ptr_a << "\n";
59+
60+
// Change value of a through its pointer
61+
*ptr_a = 2;
62+
cout << "Value of a after *ptr_a = 2: " << a << "\n";
63+
64+
// * 2. Pointer to const
65+
[[maybe_unused]] const int const_var = 100;
66+
const int *ptr_const_var = &const_var;
67+
// *ptr_const_var = 10; // cannot modify the value through pointer
68+
cout << "Value of ptr_const_var " << *ptr_const_var << "\n";
69+
ptr_const_var = &a; // can point somewhere else
70+
cout << "Value of ptr_const_var " << *ptr_const_var << "\n";
71+
72+
// * 3. Const pointer
73+
int *const const_ptr_a = &a;
74+
*const_ptr_a = 10; // can change value
75+
// const_ptr_a = nullptr; //cannot point to another variable
76+
77+
// * 4. Const pointer to const
78+
[[maybe_unused]] const int *const const_ptr_const_var = &a;
79+
// *const_ptr_const_var = 10; // cannot modify the value through pointer
80+
// const_ptr_const_var = nullptr; //cannot point to another variable
81+
82+
// * 5. Pointer to pointer
83+
int **ptr_ptr_a = new int *[10]; // dynamically allocate an array of 10 int*
84+
ptr_ptr_a[0] = &a;
85+
cout << "Value via pointer-to-pointer (*ptr_ptr_a[0]): " << *ptr_ptr_a[0] << "\n";
86+
delete[] ptr_ptr_a; // always free heap memory
87+
88+
// * 6. Void pointer (generic pointer)
89+
// void *void_ptr = static_cast<int*>(&a);
90+
void *void_ptr = &a; // C-style pointer casting [cstyleCast]
91+
92+
cout << "Value via void pointer (after casting): " << *static_cast<int *>(void_ptr) << "\n";
93+
94+
// * 7. Function pointer
95+
int (*fcn_ptr)(int) = &foo;
96+
(*fcn_ptr)(5); // call via dereference
97+
fcn_ptr(10); // call directly
98+
99+
// 8. Passing pointers
100+
int in = 10;
101+
byPtr(&in);
102+
byConstPtr(&in);
103+
byPtrConst(&in);
104+
byConstPtrConst(&in);
105+
106+
// * 9. Return by ptr(address)
107+
const int *b_ptr = returnPtr();
108+
std::cout << "By ptr: " << *b_ptr << '\n';
109+
}
110+
111+
// A struct that runs code when its object is created
112+
struct CPointers
113+
{
114+
CPointers()
115+
{
116+
cout << "\n"
117+
<< "\n"
118+
<< "Compound type: Pointers\n";
119+
120+
pointers();
121+
}
122+
};
123+
124+
// All global and static objects are constructed before main() begins.
125+
static CPointers autoRunInstance;

src/core/datatypes/CReferences.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <iostream>
2+
using namespace std;
3+
// A type defined in terms of other types
4+
5+
void increment(int &x)
6+
{
7+
x += 1;
8+
}
9+
10+
void incrementConst(const int &x)
11+
{
12+
// s cannot be modified here
13+
// x += 1;
14+
}
15+
16+
static int global = 42;
17+
int &returnRef()
18+
{
19+
return global; // the object must outlive the reference.
20+
}
21+
22+
void references()
23+
{
24+
cout << "\n--- References Type Examples ---\n";
25+
int a = 10;
26+
cout << "a = " << a << "\n";
27+
28+
// *1. Reference basics
29+
// int &ref_error; // reference must be initialized
30+
int &ref_a = a;
31+
32+
ref_a = 20; // modifies 'a', since ref is just an alias
33+
cout << "ref_a =20, a = " << a << "\n"; // prints 20
34+
35+
// Cannot reseat: once 'ref' is bound to 'a', it cannot be bound to another variable
36+
// int b = 30;
37+
// ref = &b; invalid, would assign value instead of rebinding
38+
39+
// *2. Pass by reference
40+
increment(a); // avoids making a copy
41+
std::cout << "a after increment = " << a << "\n"; // prints 21
42+
43+
// *3. Pass by const reference
44+
incrementConst(a);
45+
46+
// *4. Return by reference
47+
int const &b = returnRef();
48+
std::cout << "By reference: " << b << '\n';
49+
}
50+
51+
// A struct that runs code when its object is created
52+
struct CReferences
53+
{
54+
CReferences()
55+
{
56+
cout << "\n"
57+
<< "\n"
58+
<< "Compound type: References\n";
59+
60+
references();
61+
}
62+
};
63+
64+
// All global and static objects are constructed before main() begins.
65+
static CReferences autoRunInstance;

src/core/datatypes/CStruct.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
// A type defined in terms of other types
6+
7+
// *0. Define a struct:
8+
// structure alignment, padding, decreasing order of size
9+
// public access
10+
// In C, structs can only hold data members, not functions.
11+
struct StructDataType
12+
{
13+
/* data */
14+
double voltage{0.0}; // Default initialization
15+
int id{0};
16+
char status{'N'};
17+
string label{"Unknown"};
18+
19+
/* function */
20+
void print() const
21+
{
22+
cout << "Size of struct = " << sizeof(StructDataType) << " bytes" << endl;
23+
cout << "Sensor " << id
24+
<< " [" << label << "] "
25+
<< "Voltage: " << voltage
26+
<< " Status: " << status << endl;
27+
}
28+
};
29+
30+
void updateVoltageRef(StructDataType &data, double newV)
31+
{
32+
data.voltage = newV;
33+
}
34+
35+
void updateVoltagePtr(StructDataType *data, double newV)
36+
{
37+
// (*data).voltage = newV;
38+
if (data)
39+
data->voltage = newV;
40+
}
41+
42+
StructDataType makeSensor(int id, double v, const string &label)
43+
{
44+
return {v, id, 'N', label};
45+
}
46+
47+
void structs()
48+
{
49+
cout << "\n--- Struct Type Examples ---\n";
50+
// *1. Using struct
51+
[[maybe_unused]] StructDataType data0; // default init
52+
StructDataType data1{3.3, 1, 'N', "Temp1"};
53+
StructDataType data2 = {6.3, 2, 'N', "Temp2"};
54+
55+
// *2. Access members
56+
data1.print();
57+
const StructDataType *ptr_data1 = &data1;
58+
ptr_data1->print();
59+
60+
// *3. Passing by reference
61+
updateVoltageRef(data2, 100);
62+
data2.print();
63+
// *4. Passing by pointer
64+
updateVoltagePtr(&data2, 200);
65+
data2.print();
66+
67+
// *5. Return a temporary struct
68+
StructDataType s2 = makeSensor(2, 5.0, "Pressure");
69+
s2.print();
70+
}
71+
72+
// A struct that runs code when its object is created
73+
struct CStruct
74+
{
75+
CStruct()
76+
{
77+
cout << "\n"
78+
<< "\n"
79+
<< "Compound type: Struct\n";
80+
81+
structs();
82+
}
83+
};
84+
85+
// All global and static objects are constructed before main() begins.
86+
static CStruct autoRunInstance;

0 commit comments

Comments
 (0)