forked from faranakR/full-cpp-tutorial-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter_24_templates.cpp
More file actions
159 lines (125 loc) · 4.32 KB
/
chapter_24_templates.cpp
File metadata and controls
159 lines (125 loc) · 4.32 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*=============================================================================
* CHAPTER 24: TEMPLATES
* =============================================================================
*
* C++ Development Course - Consolidated Examples
* Original Author: Faranak Rajabi
*
* This file contains 8 code example(s) from Chapter 24.
*
* USAGE:
* - Review each example section
* - Uncomment the code you want to test
* - Compile: g++ -std=c++17 -Wall thisfile.cpp -o program
* - Run: ./program
*
* NOTE:
* Some examples are code snippets designed to illustrate specific
* concepts and may need additional context to compile.
*
=============================================================================*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
// ============================================================================
// EXAMPLE 1: Old way
// ============================================================================
int main () {
// Old way
std :: cout << maxInt (10 , 20) << ’\ n ’;
std :: cout << maxDouble (3.14 , 2.71) << ’\ n ’;
// ============================================================================
// EXAMPLE 2: Without our helper function
// ============================================================================
int main () {
Color shirtColor { blue };
// Without our helper function
std :: cout << " Shirt color code : " << static_cast < int >( shirtColor ) << ’\ n ’;
// ============================================================================
// EXAMPLE 3: Better : Use const to prevent modification
// ============================================================================
/*
}
// Better : Use const to prevent modification
void p ri nt Ar r ay Co ns t ( const int arr [] , int size ) {
for ( int i = 0; i < size ; ++ i ) {
std :: cout << arr [ i ] << " " ;
}
std :: cout << ’\ n ’;
17 }
// Arrays are passed by reference ( not copied )
void modifyArray ( int arr [] , int size ) {
for ( int i = 0; i < size ; ++ i ) {
arr [ i ] *= 2; // Double each element
}
24 }
*/
// ============================================================================
// EXAMPLE 4: 2 D array : 3 rows , 4 columns
// ============================================================================
int main () {
// 2 D array : 3 rows , 4 columns
int matrix [3][4]{
{1 , 2 , 3 , 4} ,
// Row 0
{5 , 6 , 7 , 8} ,
// Row 1
{9 , 10 , 11 , 12}
// Row 2
};
// ============================================================================
// EXAMPLE 5: Iterate through 2 D array
// ============================================================================
/*
// Iterate through 2 D array
std :: cout << " Matrix contents :\ n " ;
for ( int row = 0; row < 3; ++ row ) {
for ( int col = 0; col < 4; ++ col ) {
std :: cout << matrix [ row ][ col ] << " \ t " ;
}
std :: cout << ’\ n ’;
}
*/
// ============================================================================
// EXAMPLE 6: Iterate using pointer
// ============================================================================
/*
// Iterate using pointer
std :: cout << " Using pointer : " ;
for ( int i = 0; i < 5; ++ i ) {
std :: cout << *( ptr + i ) << " " ;
}
std :: cout << ’\ n ’;
*/
// ============================================================================
// EXAMPLE 7: Get size at runtime
// ============================================================================
int main () {
// Get size at runtime
std :: cout << " How many scores ? " ;
int count ;
std :: cin >> count ;
// Allocate array on heap
int * scores = new int [ count ]{};
// ============================================================================
// EXAMPLE 8: Calculate average
// ============================================================================
/*
// Calculate average
int total = 0;
for ( int i = 0; i < count ; ++ i ) {
total += scores [ i ];
}
double average = static_cast < double >( total ) / count ;
std :: cout << " Average : " << average << ’\ n ’;
*/
// ============================================================================
// MAIN FUNCTION
// ============================================================================
// Uncomment the examples above and add your test code here
int main() {
std::cout << "Chapter 24: Templates" << std::endl;
// Your test code here
return 0;
}