-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
535 lines (402 loc) · 9.76 KB
/
Source.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
This is a Learning documents for C++ Programming and Game Development
*/
#include <iostream>
#include <string>
using namespace std;
//Function Declare and Body will be in below
void aWelComeFunc();//welcome message print
int myFuncAdd(int aFirstVal, int aSecondVal); // Add 2 Number
void printData(int aData); //Print a Number
void printData(float aData); //Print a Number
void printData(char aData); //Print a Number
void printData(string aData); //Print a Number
void aExampleOfConditionalUse(); //Different example of Condition
char getStringFromUser();// Function to Get any input from User
void printResponse(char aVal1);
void exampleOfNumberOperator();//Numeric OPeration
void diffLoops(); //Example of Different Loop
void funcWithPassingByValue(int aVal); //By Value Function
void funcWithPassingByRef(int & aVal); //By Ref Function
void funcStringExamples(); //Different types of String operations
void funcConstantExamples(); //Different types of Constant operations
void funcArrayExamples();// Example of Different Types of Array
void funcEnumExamples(); //Example of Enum data types
void funcStructExamples();//Example of Different type of St
void funcPointerExamples(); //Example of Different types of Pointers
//Main Funtion
int main()
{
// Clear the Screen
system("cls");
//Print WelCome Message
aWelComeFunc();
// Start Other Function implementation -----------------
//Example of Different types of Pointers
funcPointerExamples();
//Example of Differet type of St
funcStructExamples();
//Example of Enum data types
funcEnumExamples();
//Example of Different types of Array
funcArrayExamples();
//If Else Example Function
aExampleOfConditionalUse();
//Example of number Addition
printData(myFuncAdd(2, 6));
//Number Example
exampleOfNumberOperator();
//Loop
diffLoops();
//Call Function with Value or Refrence
int aCheckNum1 = 1;
int aCheckNum2 = 1;
funcWithPassingByValue(aCheckNum1);
printData(aCheckNum1);
funcWithPassingByRef(aCheckNum2);
printData(aCheckNum2);
//
//Example of String operations
funcStringExamples();
funcConstantExamples();
//Get String from User and print
//printResponse(getStringFromUser());
//Hold the system for user Keypress.
system("Pause");
//Scope of the Variable, which is declare inside the Cury Bras, will scope with the Curly Bras
}
//Example of Different types of Pointers
struct Container
{
string Name;
int x;
int y;
int z;
};
void funcPointerExamples()
{
//Pointer Example 1
int myint1 = 122;
int* ptr1;
ptr1 = & myint1;
cout << "Pointer: " << ptr1 << endl;//Pointer of the Variable
cout << "MyInt = : " << myint1 << endl; //Direct Value access
cout << "& MyInt = " << &myint1 << endl; // Pointer of the Variable
cout << "* ptr1 = " << * ptr1 << endl; // De Refrencing the pointer to access the variable.
//Pointer Example 2
Container cnt1 = { "Raghu", 1, 2, 3 };
Container* ptrCnt1 = &cnt1;
cout << "1- Struct Pointer=" << ptrCnt1->Name << endl;
cout << "2- Struct Pointer=" << (*ptrCnt1).Name << endl;
}
//Example of Differet type of Structure
struct LocationVect
{
float x;
float y;
float z;
};
struct Player
{
string Name;
float Health;
int Level;
float Damage;
float Stamina;
void Attack()
{
cout << Name << " Does " << Damage << " Damage !" << endl;
}
int getLevel()
{
return Level;
}
LocationVect Location = { 0.f, 0.f, 0.f };
void displayLocationVect()
{
cout << "Location.X = " << Location.x << endl;
cout << "Location.Y = " << Location.y << endl;
cout << "Location.Z = " << Location.z << endl;
}
};
void funcStructExamples()
{
//
Player pl1;
pl1.Name = "JOHN";
pl1.Health = 100.f;
pl1.Level = 1;
pl1.Damage = 2.5f;
pl1.Stamina = 20.f;
pl1.Attack();
//Another Player Example
Player pl2 = { "Reeta ", 100.f, 3, 15.f };
cout << "Player: " << pl2.Name << ", Level: " << pl2.getLevel();
pl2.Attack();
pl2.displayLocationVect();
cout << "Player: " << pl1.Name << ", Level: " << pl1.getLevel();
pl1.Location = { 12.5, 34.2, 73.2 };
pl1.displayLocationVect();
}
//End of Struct
//Example of Enum data types
enum playerLevel
{
PL_low,
PL_medium,
PL_High
};
enum gameLevel
{
GL_low,
GL_medium,
GL_High
};
void funcEnumExamples()
{
//Example - 1
playerLevel PL1;
PL1 = PL_High;
switch (PL1)
{
case PL_High:
cout << "PL_High=" << PL_High << endl;
break;
case PL_medium:
cout << "PL_medium=" << PL_High << endl;
break;
case PL_low:
cout << "PL_low=" << PL_High << endl;
break;
}
//Example - 2
gameLevel GL1 = GL_medium;
switch (GL1)
{
case GL_High:
cout << "GL_High=" << GL_High << endl;
break;
case GL_medium:
cout << "GL_medium=" << GL_medium << endl;
break;
case GL_low:
cout << "GL_low=" << GL_low << endl;
break;
}
}
// Example of Different Types of Array
void funcArrayExamples()
{
//Example - 1
int aIntArray1[3];
aIntArray1[0] = 22;
aIntArray1[1] = 11;
aIntArray1[2] = 55;
cout << aIntArray1[2] << endl;
//Example - 2
float aflArray1[10];
for (int i = 0; i < 10; i++)
{
aflArray1[i] = i + 20;
}
cout << aflArray1[0] << endl;
//Example - 3
int aIntArr3[3] = { 1, 3, 5 };
for (int i = 0; i < 3; i++)
{
cout << "Arra[" << i << "] = " << aIntArr3[i] << endl;
}
}
//Different types of String operations
void funcConstantExamples()
{
const int aNum1 = 120;
//## Three Error Condition
//aNum1++; // This line will show error because constant variable can not be changed.
//funcWithPassingByRef(aNum1); //This Line will also show the error because constant variale can not be sent as refrence type in a function
//const int aNum2; //This line will also show the error because without value constant variable can not be declared.
cout << aNum1 << endl;
}
//Example of String Operations
void funcStringExamples()
{
//Example - 1
string str1, str2;
str1 = "Raghu";
str2 = "Bhandari";
string strFullName = str1 + " " + str2;
cout << strFullName << endl;
//Example - 2
string customerFirstName("Raghu");
string customerLastName("Bhandari");
string strSpace(" ");
string customerFullName = "Customer Full Name: ";
customerFullName += customerFirstName;
customerFullName += strSpace + customerLastName;
cout << strFullName << endl;
}
//Example of Different Loop
void diffLoops()
{
int aNum1 = 0;
int aNum2 = 10;
//Different types of While - 1
cout << endl;
cout << "Different types of While Loop - 1 \n";
while (aNum2 > aNum1)
{
cout << "," << aNum1;
aNum1 ++;
}
//Different types of While - 2
cout << endl;
cout << "Different types of While Loop - 2 \n";
aNum1 = 0;
while (aNum1 <= 5)
{
cout << "," << aNum1;
if (aNum1 == 3)
break;
aNum1++;
}
//Different types of Do While - 3
cout << endl;
cout << "Different types of Do While Loop - 3 \n";
aNum1 = 0;
do
{
cout << "," << aNum1;
if (aNum1 == 3)
break;
aNum1++;
} while (aNum1 <= 10);
//Different types of Do While - 4
cout << endl;
cout << "Different types of Do While Loop - 4 \n";
aNum1 = 0;
do
{
if (aNum1 != 5)
{
cout << "," << aNum1;
}
aNum1++;
} while (aNum1 <= 20);
//Different types of For - 5
cout << endl;
cout << "Different types of For Loop - 5" << endl;
aNum1 = 0;
for (int i = 0; i < 5; i++)
{
cout << "," << aNum1;
aNum1++;
}
cout << endl;
}
//By Value Function
void funcWithPassingByValue(int aVal)
{
aVal++;
}
//By Ref Function
void funcWithPassingByRef(int & aVal)
{
aVal++;
}
//Number Operation
void exampleOfNumberOperator()
{
// + , - , * , / , %
// ++ =
// = ++
// * =
// = *
int aNum1 = 12;
float aNum2 = 32.45;
float aRes;
//Devide
aRes = aNum2 / aNum1;
cout << "++ Divide: " << aRes << endl;
//Multiply
aRes = aNum2 * aNum1;
cout << "* Multiply Example: " << aRes << endl;
//add
aRes = aNum2 + aNum1;
cout << "+ Addition Example: " << aRes << endl;
//Subs
aRes = aNum2 - aNum1;
cout << "- Substract Example: " << aRes << endl;
//Mod
aRes = aNum1 % aNum1;
cout << "% Mod Example: " << aRes << endl;
//= ++, ++=
aRes = ++ aNum1 ;
cout << "++ Example: " << aRes << endl;
aRes = 0;
aRes = aNum1 ++;
cout << "++ Example: " << aRes << endl;
cout << "++ Example: " << aNum1 << endl;
aRes *= aNum1;
cout << "*= Example: " << aRes << endl;
}
//Get String from User and print
char getStringFromUser()
{
cout << "Please provide your answer: y or n \n";
char aUserInput;
cin >> aUserInput;
return aUserInput;
}
void printData(string aData)
{
cout << aData << endl;
}
//Example of If Else Statement in c++
void aExampleOfConditionalUse()
{
int a(1);
int b = 13;
if (b < a)
{
cout << "b is less than a.\n" << endl;
}
else if (a == b)
{
cout << "b is equal to a.\n" << endl;
}
else
{
cout << "b is Greater than a.\n" << endl;
int aSmall = 11;
cout << "Local Scope Variable\n";
}
}
//Print Welcome Message
void aWelComeFunc()
{
std::cout << " Good Luck For Game Development! \n";
}
//Example Fucntion to add 2 int values
int myFuncAdd(int aFirstVal, int aSecondVal)
{
return aFirstVal + aSecondVal;
//return aResult;
}
//Print any Int Number
void printData(int aData)
{
cout << aData << endl;
}
void printData(float aData)
{
cout << aData << endl;
}
void printData(char aData)
{
cout << aData << endl;
}
//Print any Char
void printResponse(char aVal1)
{
cout << "You have entered:" << aVal1 << endl;
}