-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_2_BasicsOfCplusPlus.cpp
More file actions
1075 lines (961 loc) · 63.3 KB
/
Copy path_2_BasicsOfCplusPlus.cpp
File metadata and controls
1075 lines (961 loc) · 63.3 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
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ===========================================================================
/// <summary>
/// _2_Basics.cpp
/// CplusPlus
/// created by Mehrdad Soleimanimajd on 11.04.2018
/// </summary>
/// <created>ʆϒʅ, 11.04.2018</created>
/// <changed>ʆϒʅ, 02.07.2023</changed>
// ===========================================================================
//! preprocessor directives:
// directives are special lines interpreted by what is known as preprocessor before the compilation of the program begins.
// example: #include <iostream>
// this directive instruct the preprocessor to include header iostream, which is a section of standard C++ code.
// header iostream after inclusion allow the program to perform standard input and output operations.
//#include "pch.h"
#include "CplusPlus.h"
#ifdef _WIN32
#include "Console.h"
#elif defined __APPLE__
#include "Terminal.h"
#endif
//! using namespace std:
//using namespace std;
// to write code simply by introducing visibility of components of the standard C++ library,
// on the other hand by qualifying each and every use of the elements, name collision can be avoided.
// after this declaration, explicit qualified std::cout is going to be replaced with unqualified name cout.
// std is the namespace of standard C++ library and cout is one of its elements.
// the examples in this tutorial are written using the explicit qualification.
void _02_01_StructureOfaProgram () // declaration of the function, explanation in function section
{ // braces {} indicate begin and end of a block of code, in this case the body code of the function
try
{
ColourCouter (" -------------------------------------------------", F_bRED);
ColourCouter ("--------------------------------------------------\n\n", F_bRED);
//! ####################################################################
//! ~~~~~ structure of a program:
// a C++ program always start from the main function, no matter the order of definitions.
// the main function of this program is in CplusPlus.cpp file.
// the main function is the only function which called automatically.
// other functions can be executed if they are called directly or indirectly from main function.
ColourCouter ("~~~~~ Structure of a program:\n", F_bPURPLE);
ColourCouter ("Although C++ language doesn't have any strict rules on structure and indention in source code, it is better for human understanding, that it be properly structured and indented.\n\n", F_YELLOW);
// comment is the important tool, which provides direct documentation within source code
// a line comment
/// a documentation comment
/* block
comment*/
// inserting an output
std::cout << "Hello World! ";
// std::cout is standard character output
// << is insertion operator
// '/n' is the newline character.
std::cout << "I'm the first C++ example of this tutorial.\n\n";
// semicolon (;): all statements in C++ must ends with semicolons.
// preprocessor directives are not statements.
} catch (const std::exception&)
{
}
}
void _03_01_VariablesTypesAndIdentifiers ()
{
try
{
ColourCouter (" -------------------------------------------------", F_bRED);
ColourCouter ("--------------------------------------------------\n\n", F_bRED);
//! ####################################################################
//! ~~~~~ variables, types and identifiers:
// variables are portions of memory that have types and are defined to store values.
ColourCouter ("~~~~~ Variables, types and identifiers:\n", F_bPURPLE);
ColourCouter ("Portions of memory to store values of different types.\n\n", F_YELLOW);
//! ####################################################################
//! ----- declaration and initialization in different C++ revisions
// declaration alone introduces variables with undetermined values,
// therefore they are practically unusable till their first time value assignation.
// initialization is the process of introducing a value for a variable in declaration time.
// a valid identifiers could be built by a sequence of one or more letters, digits or underscore _
// identifiers shall always begin with letters, additionally they can begin with underscore too.
// programmers identifiers can not match C++ reserved keywords.
// C++ is a case-sensitive language.
// std::endl: flushes the stream and prints the newline character
ColourCouter ("----- Declaration and initialization in different C++ revisions:\n", F_bPURPLE);
ColourCouter ("A variable must first be declared and in the moment of declaration, it can be initialized.\n\n", F_YELLOW);
int a1 = 0; // C-like initialization
int a2 (0); // constructor initialization (C++ language)
std::cout << "Initialized with C-Like initialization:\t\t" << "a1: " << a1 << "\n";
std::cout << "Initialized in constructor (C++ language):\t" << "a2: " << a2 << "\n";
int aa {0}, bb {0}, result {0}; // C++ standard initialization (2011 revision)
std::cout << "Initialized in C++ standard revision:\t\t" << "aa: " << aa << '\t' << "bb: " << bb << '\t' << "result: " << result << std::endl << std::endl;
//! - in addition:
// after declaration, a variable can be assigned its first time value or a new value.
// some simple processes on variables for the time being:
ColourCouter ("Assigning new values to variables:\n", F_GREEN);
std::cout << "aa: " << aa << '\t' << "bb: " << bb << '\t' << "Initialization's value of result is: " << result << std::endl;
aa = 5;
bb = 2;
std::cout << "Assigned values:\t" << "aa: " << aa << '\t' << "bb: " << bb << std::endl << std::endl;
aa = aa + 1;
ColourCouter ("Some processes on the variables above:\n", F_GREEN);
std::cout << "Result of ( aa + 1 ):\t" << aa << std::endl;
result = bb - aa;
// last insertion:
std::cout << "aa: " << aa << '\t' << "bb: " << bb << '\t' << "Result of ( bb - aa ): " << result << std::endl << std::endl;
} catch (const std::exception&)
{
}
}
void _03_02_FundamentalTypesAndDeduction ()
{
try
{
//! ####################################################################
//! ~~~~~ fundamental types and deduction
// the zeros and ones stored in memory which represent variables need to be interpreted.
// for this purpose the program needs to be aware of the kind of the stored data.
// implementations of basic types, supported by most systems, directly into the language, represent basic storage units known as fundamental types.
ColourCouter ("~~~~~ Fundamental types and deduction:\n", F_bPURPLE);
ColourCouter ("Fundamental types are the basics types supported by most systems.\n", F_YELLOW);
ColourCouter ("Deduction introduce the ability of the compiler, with which the type of an unknown variable can be identified.\n\n", F_YELLOW);
//! ####################################################################
//! ----- fundamental types: characters
// ----------------------------------------------------------------
// Type name Size / Precision description
// ----------------------------------------------------------------
// char Exactly one byte in size. At least 8 bits.
// ----------------------------------------------------------------
// char16_t Not smaller than char. At least 16 bits.
// ----------------------------------------------------------------
// char32_t Not smaller than char16_t. At least 32 bits.
// ----------------------------------------------------------------
// wchar_t Can represent the largest supported character set.
// ----------------------------------------------------------------
ColourCouter ("----- Fundamental types: Character types\n", F_bPURPLE);
ColourCouter ("Character types can represents a single character and are in different sizes.\n\n", F_YELLOW);
std::cout << "Size of char in byte:\t\t" << sizeof (char) << "\n";
char ch1 {'M'};
std::cout << "char:\t\t\t\t" << ch1 << std::endl << std::endl;
std::cout << "Size of char16_t in byte:\t" << sizeof (char16_t) << "\n";
char16_t ch2 {'M'};
std::cout << "char16_t:\t\t\t" << ch2 << std::endl << std::endl;
std::cout << "Size of char32_t in byte:\t" << sizeof (char32_t) << "\n";
char32_t ch3 {'M'};
std::cout << "char32_t:\t\t\t" << ch3 << std::endl << std::endl;
std::cout << "Size of wchar_t in byte:\t" << sizeof (wchar_t) << "\n";
wchar_t ch4 {'M'};
std::cout << "wchar_t:\t\t\t" << ch4 << std::endl << std::endl;
//! ####################################################################
//! ----- fundamental types: integers
// ------------------------------------------------------------------
// Type name Size / Precision description
// ------------------------------------------------------------------
// signed char Same size as char. At least 8 bits.
// ------------------------------------------------------------------
// signed short int Not smaller than char. At least 16 bits.
// ------------------------------------------------------------------
// signed int Not smaller than short. At least 16 bits.
// ------------------------------------------------------------------
// signed long int Not smaller than int. At least 32 bits.
// ------------------------------------------------------------------
// signed long long int Not smaller than long. At least 64 bits
// ------------------------------------------------------------------
// unsigned char Same size as its signed counterpart
// ------------------------------------------------------------------
// unsigned short int "
// ------------------------------------------------------------------
// unsigned int "
// ------------------------------------------------------------------
// unsigned long int "
// ------------------------------------------------------------------
// unsigned long long int "
// ------------------------------------------------------------------
ColourCouter ("----- Fundamental types: integer types\n", F_bPURPLE);
ColourCouter ("Integer types can represent numerical values, are in variety of sizes and can be either signed or unsigned.\n\n", F_YELLOW);
signed char num1_1 {-128}, num1_2 {127}; // hexadecimal: -0x80 to 0x7f
unsigned char num1_3 {0}, num1_4 {255}; // hexadecimal: 0xff
std::cout << "Size of char:\t\t\t" << sizeof (char) << "\n";
std::cout << "Signed char range:\t\t" << static_cast<int>(num1_1) << "\t\t\tto\t" << static_cast<int>(num1_2) << "\n";
std::cout << "Unsigned char range:\t\t" << static_cast<int>(num1_3) << "\t\t\tto\t" << static_cast<int>(num1_4) << "\n\n";
short num2_1 = {-32768}, num2_2 = {32767}; // hexadecimal: -0x8000 to 0x7fff
unsigned short num2_3 {0}, num2_4 {65535}; // hexadecimal: 0xffff
std::cout << "Size of short:\t\t\t" << sizeof (short) << "\n";
std::cout << "Signed short range:\t\t" << num2_1 << "\t\t\tto\t" << num2_2 << "\n";
std::cout << "Unsigned short range:\t\t" << num2_3 << "\t\t\tto\t" << num2_4 << "\n\n";
int num3_1 {-2147483647}, num3_2 {2147483647}; // hexadecimal: -0x7fffffff to 0x7fffffff
unsigned int num3_3 {0}, num3_4 {4294967295}; // hexadecimal: 0xffffffff
std::cout << "Size of int:\t\t\t" << sizeof (int) << "\n";
std::cout << "Signed int range:\t\t" << num3_1 << "\t\tto\t" << num3_2 << "\n";
std::cout << "Unsigned int:\t\t\t" << num3_3 << "\t\t\tto\t" << num3_4 << "\n\n";
long num4_1 {-2147483647}, num4_2 {2147483647}; // hexadecimal: -0x7fffffff to 0x7fffffff
unsigned int num4_3 {0}, num4_4 {4294967295}; // hexadecimal: 0xffffffff
std::cout << "Size of long:\t\t\t" << sizeof (long) << "\n";
std::cout << "Signed long range:\t\t" << num4_1 << "\t\tto\t" << num4_2 << "\n";
std::cout << "Unsigned long:\t\t\t" << num4_3 << "\t\t\tto\t" << num4_4 << "\n\n";
long long num5_1 {-9223372036854775807}, num5_2 {9223372036854775807}; // hexadecimal: -0x7fffffffffffffff to 0x7fffffffffffffff
unsigned long long num5_3 {0}, num5_4 {18446744073709551615}; // // hexadecimal: -0xffffffffffffffff
std::cout << "Size of long long:\t\t" << sizeof (long long) << "\n";
std::cout << "Signed long long range:\t\t" << num5_1 << "\tto\t" << num5_2 << "\n";
std::cout << "Unsigned long long:\t\t" << num5_3 << "\t\t\tto\t" << num5_4 << "\n\n";
//! ####################################################################
//! ----- fundamental types: floats
// ------------------------------------------------------------------
// Type name Size / Precision description
// ------------------------------------------------------------------
// float (7 digits)
// ------------------------------------------------------------------
// double precision not less than float (15 digits)
// ------------------------------------------------------------------
// long double precision not less than double (15 digits)
// ------------------------------------------------------------------
ColourCouter ("----- Fundamental types: floats\n", F_bPURPLE);
ColourCouter ("Depending on the kind of the floating-point of these types, they can represent real values with different levels of precision.\n\n", F_YELLOW);
float num6_1 {1.8e-38}, num6_2 {3.4e+38};
std::cout << "Size of float:\t\t" << sizeof (float) << "\n";
std::cout << "Float range:\t\t" << num6_1 << "\t\tto\t" << num6_2 << "\n";
double num7_1 {2.2e-308}, num7_2 {1.79e+308};
std::cout << "Size of double:\t\t" << sizeof (double) << "\n";
std::cout << "Double rang:\t\t" << num7_1 << "\tto\t" << num7_2 << "\n";
long double num8_1 {2.2e-308}, num8_2 {1.79e+308};
std::cout << "Size of long double:\t" << sizeof (long double) << "\n";
std::cout << "Long double range:\t" << num8_1 << "\tto\t" << num8_2 << "\n\n";
// check the link for more details on float: http://de.cppreference.com/w/cpp/language/types
//! - in addition:
// since C++ has a lot different compilers, when it comes to types C++ can give these guaranties:
// char size 1 byte [-128...127]
// short is smaller than int, which is smaller than long and long long is the largest one.
// long long, which is a GCC extension, has found his way into C++ standard.
//! ####################################################################
//! ----- fundamental other types: bool, void and nullptr
// ------------------------------------------------------------------
// Type name Size / Precision description
// ------------------------------------------------------------------
// bool one byte
// ------------------------------------------------------------------
// void no storage
// ------------------------------------------------------------------
// decltype (nullptr) void pointer
// ------------------------------------------------------------------
ColourCouter ("----- Fundamental other types: bool, void and nullptr\n", F_bPURPLE);
ColourCouter ("The in C++ known as bool Boolean type, is a logical type and represents 'true' or 'false' as values.\n\n", F_YELLOW);
bool bool_var_1 {true}, bool_var_2 {false};
std::cout << "Size of bool:\t\t" << sizeof (bool) << "\n";
std::cout << "Two bool variables:\t" << bool_var_1 << "\tand\t" << bool_var_2 << "\n\n";
//! - in addition:
// additional reference: https://docs.microsoft.com/en-us/cpp/cpp/void-cpp?view=vs-2017
// void identifies the lack of type.
// use: in function return types, function parameters, universal pointers
// there is no constant or volatile variables of type void.
//! - in addition:
// some basic knowledge on void (universal) pointers:
// they can be dereferenced only by casting to another type.
// they can be converted to other types of data pointer.
// they can point to a function but not to a class member.
//! - in addition:
// A null pointer (nullptr) is a value that any pointer can take to represent that it points to nowhere.
// more on void and nullptr will be mentioned in further sections.
ColourCouter ("The lack of type is identified with 'void' keyword.\n", F_YELLOW);
ColourCouter ("Any pointer can point to nowhere by taking nullptr as its value.\n\n", F_YELLOW);
void* void_poi; // uninitialized: can not be used
int* int_poi; // uninitialized: can not be used
int* nullptr_poi {nullptr}; // initialized to point to nowhere! :)
std::cout << "The address of a pointer initialized with nullptr:\t\t\t\t" << nullptr_poi << "\n";
int int_i {22};
void_poi = &int_i;
std::cout << "Variable address -pointed to by a void pointer-:\t\t\t\t" << void_poi << "\n";
int_poi = (int*) void_poi; // casting operation: optional in C, required in C++
std::cout << "Variable address -pointed to by a int pointer- -cast from void pointer-:\t" << int_poi << "\n\n";
//! - in addition:
// more infos on all fundamental types:
// the properties of fundamental types in particular systems and compiler implementations:
//! http://www.cplusplus.com/%3Climits%3E
// types of specific sizes:
//! http://www.cplusplus.com/%3Ccstdint%3E
//! ####################################################################
//! ----- strings:
// the class string is one of the compound types in C++.
// header file is <string>.
ColourCouter ("----- Strings:\n", F_bPURPLE);
ColourCouter ("One of the compound types in C++ is the class string.\n\n", F_YELLOW);
std::string str1 = {"This is the initial string."}; // initialization in C++ standard form.
std::cout << "The string variable value is:\t" << str1 << "\n\n";
// more details on standard C++ strings:
//! http://www.cplusplus.com/string
//! ####################################################################
//! ----- deduction of type:
// auto and decltype shall mainly be used when the type can't be determined or for improving the readability. the example below is non of them.
ColourCouter ("----- Deduction of type:\n", F_bPURPLE);
ColourCouter ("Uses: determination of types, improving the readability.\n\n", F_YELLOW);
int int_var {0};
auto aut_var {int_var}; // same as: int aut_var { int_var }
decltype(int_var) dec_var {int_var}; // same as: int dec_var { int_var }
std::cout << "Normal declaration and initialization:\t" << int_var << std::endl;
std::cout << "Using auto to deduct the type:\t\t" << aut_var << std::endl;
std::cout << "Using decltype to deduct the type:\t" << dec_var << std::endl << std::endl;
} catch (const std::exception&)
{
}
}
void _04_01_ConstantNumerals ()
{
try
{
ColourCouter (" -------------------------------------------------", F_bRED);
ColourCouter ("--------------------------------------------------\n\n", F_bRED);
//! ####################################################################
//! ~~~~~ constants numerals:
// first kind of the most obvious constants are numerals, which can be of types integer or floating-point.
// like variables numerals have types.
// in the statement "i=5;" 5 is a numeral constant.
ColourCouter ("~~~~~ Constants numerals:\n", F_bPURPLE);
ColourCouter ("One of the most obvious kind of constants are numerals.\n\n", F_YELLOW);
//! ####################################################################
//! ----- integer constants:
// C++ allows the use of numbers in octal or hexadecimal base
ColourCouter ("----- Integer constants:\n", F_bPURPLE);
ColourCouter ("Fixed values of type integer.\n\n", F_YELLOW);
const int con1 {75}; // decimal
const int con2 {0113}; // octal (preceded with 0)
const int con3 {0x4b}; // hexadecimal (preceded with 0x)
std::cout << "Integer constant (initialized in decimal base):\t\t" << con1 << "\n";
std::cout << "Integer constant (initialized in octal base):\t\t" << con2 << "\n";
std::cout << "Integer constant (initialized in hexadecimal base):\t" << con3 << "\n\n";
//! - in addition:
// by default integer literals are "int" but they can be modified by appending certain suffixes.
// modification of types: (these suffixes may be case-sensitiv when it comes to cross-platform development and different compilers.)
// -------------------
// u unsigned
// -------------------
// l long
// -------------------
// ll long long
// -------------------
ColourCouter ("Modifying integer constant by using suffixes:\n\n", F_YELLOW);
const int con4 {75u};
const int con5 {0113lU};
const int con6 {0x4bLL};
const int con7 {0X4bULL};
std::cout << "Modified to type unsigned:\t\t" << con4 << "\n";
std::cout << "Modified to type unsigned long:\t\t" << con5 << "\n";
std::cout << "Modified to type long long:\t\t" << con6 << "\n";
std::cout << "Modified to type unsigned long long:\t" << con7 << "\n\n";
//! ####################################################################
//! ----- floating point constants:
// expressing real values which can include either decimal point or "e" character
// "e" character stands for exponents, isn't case-sensitiv.
// expresses "by ten at the Xth height" where "X" is an integer value that follows "e".
// modification of types: (these suffixes aren't case-sensitiv.)
// ---------------------
// f float
// ---------------------
// l long double
// ---------------------
ColourCouter ("----- Floating point constants:\n", F_bPURPLE);
ColourCouter ("Fixed values of type floating point.\n\n", F_YELLOW);
const double con8 {3.14159f}; // the PI number
const float con9 {static_cast<float>(6.02e23L)}; // the number of Avogadro
const double con10 {1.6E-19}; // the number of electric charge of an electron
const float con11 {3.0};
std::cout << "The PI number:\t\t\t\t\t" << con8 << "\n";
std::cout << "The Avogadro number:\t\t\t\t" << con9 << "\n";
std::cout << "The number of electric charge of an electron:\t" << con10 << "\n";
std::cout << "A float number:\t\t\t\t\t" << con11 << "\n\n";
} catch (const std::exception&)
{
}
}
void _04_02_ConstantLiterals ()
{
try
{
//! ####################################################################
//! ~~~~~ constant literals:
// second kind of the most obvious constants are literals, which can be of types character or string
// this kind of constants are enclosed in quotes.
// ' for characters and " for strings
ColourCouter ("~~~~~ Constant literals:\n", F_bPURPLE);
ColourCouter ("Literals are the second most obvious kind in the sphere of constants.\n\n", F_YELLOW);
ColourCouter ("Some constant characters and strings:\n", F_GREEN);
const char con12 {'Z'};
std::cout << "'Z' character:\t" << con12 << "\n";
const std::string con13 = {"How do you do?"};
std::cout << "A string:\t" << con13 << "\n\n";
//! - in addition:
// character and string literals can be used to represent special characters,
// which are in most cases difficult or impossible to be used in the source code.
ColourCouter ("Constants ability (spacial characters):\n", F_GREEN);
const char con14 {'\n'};
const std::string con15 {"One\tTwo\tThree"};
std::cout << "Special new line character:" << con14;
std::cout << "Special tab characters:\t\t" << con15 << "\n\n";
// ------------------------------------------------------------
// escape sequence representation
// ------------------------------------------------------------
// \' byte 0x27
// ------------------------------------------------------------
// \" byte 0x22
// ------------------------------------------------------------
// \? byte 0x3f
// ------------------------------------------------------------
// \\ byte 0x5c
// ------------------------------------------------------------
// \a audible bell byte 0x07
// ------------------------------------------------------------
// \b byte 0x08
// ------------------------------------------------------------
// \f form feed byte 0x0c
// ------------------------------------------------------------
// \n line feed byte 0x0a
// ------------------------------------------------------------
// \r carriage return byte 0x0d
// ------------------------------------------------------------
// \t horizontal tab byte 0x09
// ------------------------------------------------------------
// \v vertical tab byte 0x0b
// ------------------------------------------------------------
// \nnn arbitrary octal byte nnn
// ------------------------------------------------------------
// \xnn arbitrary hexadecimal byte nn
// ------------------------------------------------------------
// \nnnnn arbitrary Unicode code point U+nnnn
// ------------------------------------------------------------
// \Unnnnnnnn arbitrary Unicode code point U+nnnnnnnn
// ------------------------------------------------------------
// Reference: http://de.cppreference.com/w/cpp/language/escape
// ------------------------------------------------------------
//! - in addition:
// every character in computer is an ASCI code.
// https://de.cppreference.com/w/cpp/language/ascii
// examples: "\202","\x2d". for more on this check the link below.
ColourCouter ("Using ASCII code of characters:\n", F_GREEN);
const char con16 {'\202'};
const char con17 {'\x2d'};
std::cout << "Initialled constants with ASCI code of characters:\t" << con16 << '\t' << con17 << "\n\n";
//! - in addition:
// simply by separating several literal strings by blank spaces they will be concatenated in one.
// in C++ big literal strings within a twin of quotes can be written in more than one line by using "\" backslash character.
// note: only blank spaces which are within the quotes are a part of the literal string, all the other ones or none at all will be ignored.
ColourCouter ("Concatenating string literals features in C++:\n", F_GREEN);
const std::string con18 {"\
This strIng Is ""In ""the" " soURce\
c0de" " paRTed" " And "
"wriTTen" " In Four Line"}; // note: pay attention to the last two line!
std::cout << "Check the initialization expression in source code:\n" << con18 << "\n\n";
//! - in addition:
// the type of the character and string literals can be modified by using specific prefixes
// these prefixes are case-sensitive.
// ------------------
// u char16_t
// ------------------
// U char32_t
// ------------------
// L whar_t
// ------------------
ColourCouter ("Modification to the types of defined characters:\n", F_GREEN);
const char16_t con19 {('UZ')};
const char32_t con20 {('LZ')};
const wchar_t con21 {('uZ')};
std::cout << "Modification of type (to char16_t):\t" << con19 << "\n";
std::cout << "Modification of type (to char32_t):\t" << con20 << "\n";
std::cout << "Modification of type (to wchar_t):\t" << con21 << "\n\n";
//! - in addition:
// two more prefixes are:
// ----------------------------------
// u8 encoding string in UTF_8
// ----------------------------------
// R string is a raw string
// ----------------------------------
// in a raw string no special character is going to be identified
// Note format:
// R"sequence(string)sequence"
// note: in the format above, the both delimiting sequences must be alike,
// both are going to be ignored and both can be anything,
// therefore what lies between parenthesis is the content of the string.
// if needed the combinations of raw prefix and other ones (u, U, L and u8) are a possibility.
ColourCouter ("All possible modification to string literals:\n", F_GREEN);
const std::string con22 {"Z\t!\tz"};
const std::string con23 {R"aRawString(--\"' (^_^) AnY likeable StrIng (^.^) '"/--)aRawString"};
std::cout << "Modifying the type of literal string to UTF_8:\t" << con22 << "\n";
std::cout << "A raw string encoded in UTF_8:\t\t\t" << con23 << "\n\n";
} catch (const std::exception&)
{
}
}
void _04_03_OtherConstantLiterals ()
{
try
{
//! ####################################################################
//! ~~~~~ other constant literals:
// third kind of constants is boolean and pointers type, that is keyword literals true, false and nullptr.
ColourCouter ("~~~~~ Other constant literals:\n", F_bPURPLE);
ColourCouter ("In C++ language there are three other keyword literals which are 'true', 'false' and 'nullptr'.\n\n", F_YELLOW);
const bool con24 {false};
std::cout << "Boolean constant keyword literal 'false':\t" << con24 << "\n";
const int* ptr {nullptr};
std::cout << "Pointer constant keyword literal 'nullptr':\t" << ptr << "\n\n";
//! ####################################################################
//! ----- typed constant expressions (programmer defined):
// for convenience sake, since some literals may often be repeated in source code.
// an example:
ColourCouter ("----- Typed constant expressions:\n", F_bPURPLE);
ColourCouter ("To avoid the often repetition of literals in the source code.\n\n", F_YELLOW);
const char tab {'\t'};
const char nline {'\n'};
const double pi {3.1415926};
double radius {5.0}, circle;
circle = radius * radius * pi;
ColourCouter ("Using typed constants:\n", F_GREEN);
std::cout << "The circle area:" << tab << radius << " * " << radius << " * " << pi << " = " << circle << nline << nline;
//! ####################################################################
//! ----- preprocessor definitions (#define):
// another way to define constant values
// Note syntax:
// #define identifier replacement
// the occurrences of identifier will be interpreted to replacement.
// the replacement can be any set of characters till the end of the line.
// the replacement happens before compiling by the preprocessor,
// therefore the mechanism is a blind replacement and there is no verification of the involved syntax.
// #define lines are preprocessor directives,
// which means they are single line instructions and unlike statements don't need semicolons.
// a semicolon in the replacement will be a part of the sequence,
// so it is going to be included in all of the occurrences.
ColourCouter ("----- Preprocessor definitions:\n", F_bPURPLE);
ColourCouter ("Another mechanism to define constant values.\n\n", F_YELLOW);
#define Tab '\t'
#define Nline '\n'
#define Pi 3.1415926
circle = radius * radius * Pi;
ColourCouter ("Using preprocessor definitions:\n", F_GREEN);
std::cout << "The circle area:" << Tab << radius << " * " << radius << " * " << Pi << " = " << circle << Nline << Nline;
} catch (const std::exception&)
{
}
}
// scope start point of these constants
const char tab {'\t'};
const char nline {'\n'};
void _05_01_ArithmeticOperators ()
{
try
{
ColourCouter (" -------------------------------------------------", F_bRED);
ColourCouter ("--------------------------------------------------\n\n", F_bRED);
//! ####################################################################
//! ~~~~~ mathematical operators:
// mathematical operators will be used to have mathematical operations on operands.
ColourCouter ("~~~~~ Mathematical operators:\n", F_bPURPLE);
ColourCouter ("Mathematical operators represent the most important and basic mathematical operations.\n\n", F_YELLOW);
//! ####################################################################
//! ----- assignment operator (=):
// the assignment operations always takes place from right to left.
ColourCouter ("----- Assignment operator (=):\n", F_bPURPLE);
ColourCouter ("The most simple mathematical operation in C++ is represented by assignment.\n\n", F_YELLOW);
int x {0}, y {2};
std::cout << "Current values are:" << "\t\t\t" << "x: " << x << tab << "y: " << y << nline;
x = 5;
std::cout << "The values after first assignment:" << tab << "x: " << x << tab << "y: " << y << nline;
x = y;
std::cout << "The values after second assignment:" << tab << "x: " << x << tab << "y: " << y << nline;
x = y = 3; // valid in C++
std::cout << "The values after third assignment:" << tab << "x: " << x << tab << "y: " << y << nline << nline;
//! - in addition:
// assignment operation can be evaluated, this means the assignment itself has a value,
// which in fundamental types is the value that assigned in the operation.
ColourCouter ("The value of assignment operation:\n", F_GREEN);
y = 6 + (x = 10); // y is 6 + the value of another assignment operation
std::cout << "y = 6 + ( x = 10 ):" << tab << "x: " << x << tab << "y: " << y << nline << nline;
//! ####################################################################
//! ----- arithmetic operators (+, -, *, /, %):
// percentage sign represent modulo which is the remainder of a division.
ColourCouter ("----- Arithmetic operator (+, -, *, /, %):\n", F_bPURPLE);
ColourCouter ("The most simple arithmetic operators with the most use.\n\n", F_YELLOW);
int q {0};
q = 23 % 10;
std::cout << "The result of modulo operator ( 23 % 10 ):" << tab << "q: " << q << nline << nline;
//! ####################################################################
//! ----- compound assignments (+=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=):
// modification of the current value of the variable by performing an operation on it.
ColourCouter ("----- Compound assignments (+=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=):\n", F_bPURPLE);
ColourCouter ("To introduce expressions that modify the current value while assigning.\n\n", F_YELLOW);
int a {0}, b {2};
std::cout << "Current values are:" << "\t\t\t" << "a: " << a << tab << "b: " << b << nline;
b *= a + 1; // equivalent to b=b*(a+1)
std::cout << "Result of expression ( b *= a + 1 ):" << tab << "a: " << a << tab << "b: " << b << nline << nline;
//! ####################################################################
//! ----- increment and decrement (++, --):
// these operators increase or decrease the value stored in a variable by one.
ColourCouter ("----- Increment and decrement (++, --):\n", F_bPURPLE);
ColourCouter ("Increment and decrement of the value by one.\n\n", F_YELLOW);
int c {0}, d {2};
std::cout << "Current values are:" << "\t\t" << "c: " << c << tab << "d: " << d << nline;
c++;// equivalent to c+=1 or c=c+1
--d;
std::cout << "Result of expression ( c++ ):" << tab << "c: " << c << nline;
std::cout << "Result of expression ( --d ):" << tab << "d: " << d << nline << nline;
//! - in addition:
// these operators can be used both as prefix and as suffix.
// although simple expressions like ++d and d++ have the same meaning,
// they may result different values in different expressions and different compilers.
ColourCouter ("Difference between the expressions ( d = ++c ) and ( d = c++ ):\n", F_GREEN);
ColourCouter ("Results are compiler dependent!\n", F_CYAN);
c = 1;
d = 0;
std::cout << "Current values are:" << "\t\t\t" << "c: " << c << tab << "d: " << d << nline;
d = ++c; // d = c after the increment (c = d = 2)
std::cout << "Result of expression ( d = ++c ):" << tab << "d: " << d << nline << nline;
c = 1;
d = 0;
std::cout << "Current values are:" << "\t\t\t" << "c: " << c << tab << "d: " << d << nline;
d = c++; // d = c before the increment (c = 2, d = 1)
std::cout << "Result of expression ( d = c++ ):" << tab << "d: " << c << nline << nline;
} catch (const std::exception&)
{
}
}
void _05_02_LagicalOperators ()
{
try
{
//! ####################################################################
//! ~~~~~ boolean operators:
// boolean operators will be used to have logical operations on operands.
ColourCouter ("~~~~~ Boolean operators:\n", F_bPURPLE);
ColourCouter ("Boolean operators represent the logical operations.\n\n", F_YELLOW);
//! ####################################################################
//! ----- relational and comparison operators (==, !=, <, > ,<=, >=):
// the results of comparison are going be boolean. any value can be compared.
ColourCouter ("----- Relational and comparison operators (==, !=, <, > ,<=, >=):\n", F_bPURPLE);
ColourCouter ("To introduce the comparison of expressions.\n\n", F_YELLOW);
int x {0}, y {2}, z {4};
std::cout << "To be compared values are:" << "\t\t\t" << "x: " << x << tab << "y: " << y << tab << "z: " << z << nline;
if ((x + 6) >= (y * x))
{
if ((z - x) >= y)
{
std::cout << "Result of expression ( z != y ):" << "\t\t" << (z != y) << nline;
// the use of = and == operators needs some attention:
std::cout << "Result of expression (( z = 2 ) == x ):" << "\t\t" << ((z = 2) == x) << tab << "z: " << z << nline << nline;
}
}
//! ####################################################################
//! ----- logical operators (!, &&, ||):
// the operator ! (logical NOT) inverts its one operand, which will be written to its right.
ColourCouter ("----- Logical operators (!, &&, ||):\n", F_bPURPLE);
ColourCouter ("Logical operators evaluate their operands to come up with the rational end results.\n\n", F_YELLOW);
int a {0}, b {2}, c {4};
std::cout << "Current values are:" << "\t\t\t" << "a: " << a << tab << "b: " << b << tab << "c: " << c << nline;
std::cout << "Result of expression !( a <= b ): " << tab << !(a <= b) << nline;
std::cout << "Result of expression !( b >= c ): " << tab << !(b >= c) << nline << nline;
//! - in addition:
// short circuit evaluation: C++ only evaluates what is necessary to come up with the combined relational result.
// this evaluation happens from left to right.
// for example in '(a==b)&&(a<=b)' statement, considering the logical AND, if 'a==b' is false, C++ never checks the rest of the statement.
ColourCouter ("Short circuit evaluation in C++:\n", F_GREEN);
std::cout << "Current values are:" << "\t\t\t\t\t" << "a: " << a << tab << "b: " << b << nline;
if (!(a == b))
{
std::cout << "Result of expression (( a == b ) && ( a <= b )):" << tab << ((a == b) && (a <= b)) << nline;
std::cout << "Result of expression (( a == b ) || ( a >= b )):" << tab << ((a == b) || (a >= b)) << nline << nline;
}
//! - in addition:
// this is most important when it comes to statements that has side effects.
// for example altering values in the right-hand expression.
ColourCouter ("Side effects of short circuit evaluation:\n", F_GREEN);
std::cout << "Current values are:" << tab << "a: " << a << tab << "b: " << b << tab << "c: " << c << nline;
if ((a == 4) || (++b > a)) // if a==4 is true, then ++b will never be executed
std::cout << "The increment in (( a == 4 ) || ( ++b > a )) is going to happen if a isn't 4:" << nline;
std::cout << "Values after operation:" << tab << "a: " << a << tab << "b: " << b << tab << "c: " << c << nline << nline;
//! ####################################################################
//! ----- conditional ternary operator (?):
// if its condition is true, the operator will return result1 and otherwise result2.
// Note statement syntax (format):
// condition ? result1 : result2
ColourCouter ("----- Conditional ternary operator (?):\n", F_bPURPLE);
ColourCouter ("This operator evaluate an expression and returns one value.\n\n", F_YELLOW);
int e {0}, f {2}, g {4};
std::string str_result;
int int_result;
std::cout << "Current values are:" << "\t\t" << "e: " << e << tab << "f: " << f << tab << "g: " << g << nline;
str_result = ((e == f) && (e > g)) ? "True" : "False";
int_result = ((e == f) && (e > g)) ? e : g;
std::cout << "Result of expression (( e == f ) && ( e > g )) ? \"True\" : \"False\";\tis:" << tab << str_result << nline;
std::cout << "Result of expression (( e == f ) && ( e > g )) ? e : g;\t\t\tis:" << tab << int_result << nline << nline;
//! ####################################################################
//! ----- comma operator (,):
// this operator will be used to separate the expressions,
// when there are more than one expression in a statement that actually suppose to have one.
ColourCouter ("----- Comma operator (,):\n", F_bPURPLE);
ColourCouter ("Separating the expressions is what this operator does.\n\n", F_YELLOW);
int h {1}, i {1}, j {1};
std::cout << "Current values are:" << "\t\t\t\t" << "h: " << h << tab << "i: " << i << tab << "j: " << j << nline;
h = (i = 2, ++j * i);
std::cout << "Result of expression (i = 2, ++j * i):" << "\t\t" << h << nline << nline;
//! - in addition:
// in cases that the set of expressions need to be evaluated for a value to be reached,
// only the most right expression is considered.
ColourCouter ("Evaluation of a set of expressions and reaching a value:\n", F_GREEN);
std::cout << "Current values are:" << "\t\t\t\t\t" << "h: " << h << tab << "i: " << i << tab << "j: " << j << nline;
h = (i = 3, i + 2); // in this expression set, first the assignment 'i=3' is considered and then the rest.
std::cout << "Result of expression ( i = 3, i + 2 ):" << "\t\t\t" << h << nline << nline;
std::cout << "Current values are:" << "\t\t\t\t\t" << "h: " << h << tab << "i: " << i << tab << "j: " << j << nline;
h = (i >= 2, j <= 4, j != 4); // in this expression set, only the last expression 'j!=4' is considered.
ColourCouter ("Evaluation result of Last expression 'j != 4':\n", F_CYAN);
std::cout << "Result of expression ( i >= 2, j <= 4, j != 4 ):" << tab << h << nline << nline;
} catch (const std::exception&)
{
}
}
void _05_03_OtherOperators ()
{
try
{
//! ####################################################################
//! ~~~~~ other operators:
//
ColourCouter ("~~~~~ Other operators:\n", F_bPURPLE);
ColourCouter ("The introduction of some other important operators.\n", F_YELLOW);
ColourCouter ("Additional to the below operators, as the tutorial goes on, some more will be introduced.\n\n", F_YELLOW);
//! ####################################################################
//! ----- bitwise operators (&, |, ^, ~, <<, >>):
// modifying an operand in its stored bit patterns which represent their value in system.
// ----------------------------------------------
// & AND bitwise And
// ----------------------------------------------
// | OR bitwise inclusive or
// ----------------------------------------------
// ^ XOR bitwise exclusive or
// ----------------------------------------------
// ~ NOT unary complement (bit inversion)
// ----------------------------------------------
// << SHL shift bits left
// ----------------------------------------------
// >> SHR shift bits right
// ----------------------------------------------
char xx {1};
ColourCouter ("----- Bitwise operators (&, |, ^, ~, <<, >>):\n", F_bPURPLE);
ColourCouter ("To introduce modification on the stored bit patterns of a value.\n\n", F_YELLOW);
std::cout << "Current value which stays unchanged:" << tab << static_cast<int>(xx) << nline;
ColourCouter ("The result of modification:\n", F_GREEN);
std::cout << "Result of expression ( xx & xx ):" << tab << (xx & xx) << nline; // -0001 and -0001 = -0001
std::cout << "Result of expression ( xx | xx ):" << tab << (xx | xx) << nline; // -0001 or -0001 = -0001
std::cout << "Result of expression ( xx ^ xx ):" << tab << (xx ^ xx) << nline; // -0001 xor -0001 = -0000
std::cout << "Result of expression ( ~xx ):" << "\t\t" << (~xx) << nline; // not -0001 = -1110
std::cout << "Result of expression ( xx << 2 ):" << tab << (xx << 2) << nline; // left shift -0001 = -0100
std::cout << "Result of expression ( xx >> 2 ):" << tab << (xx >> 2) << nline << nline; // right shift -0001 = -0000
//! ####################################################################
//! ----- explicit type casting operator:
// convert the value of a given type to another type
ColourCouter ("----- Explicit type casting operator:\n", F_bPURPLE);
ColourCouter ("To introduce the conversion of the type of a value to another type:\n\n", F_YELLOW);
int yy {0};
float f {static_cast<float> (3.14)}; // type casting in C++ standard 2011 revision (initialization)
std::cout << "Current values:" << "\t\t\t\t\t\t" << "float: " << f << tab << "int: " << yy << nline;
yy = (int) f; // C-like explicit type casting
std::cout << "Converting the type (C-like explicit type casting):" << tab << yy << nline;
yy = int (f); // C++ functional notation casting
std::cout << "Converting the type (C++ functional notation casting):" << tab << yy << nline << nline;
//! ####################################################################
//! ----- The 'sizeof' operator:
// sizeof: take one parameter (type or variable) and return the size in bytes.
ColourCouter ("----- The 'sizeof' operator:\n", F_bPURPLE);
ColourCouter ("Returns the size of fundamental types.\n\n", F_YELLOW);
yy = sizeof (long long); // the returned value of 'sizeof' is a compile-time constant (before program execution)
std::cout << "The size of type long long is:" << tab << yy << nline << nline;
} catch (const std::exception&)
{
}
}
void _05_04_PrecedenceOfOperators ()
{
try
{
//! ####################################################################
//! ~~~~~ precedence of operators:
// higher precedence of operators determines the evaluation order of operators in an expression
ColourCouter ("~~~~~ Precedence of operators:\n", F_bPURPLE);
ColourCouter ("Defines the evaluation order of operators in an expression.\n\n", F_YELLOW);
int x {0};
x = 2 + 8 % 3; // the remainder operator will be evaluated first
std::cout << "The result of expression ' 2 + 8 % 3 ':" << "\t\t" << x << nline << nline;
//! - in addition:
// uses of parenthesis:
// to explicitly clarify the intended effect
// to override the precedence of operators
ColourCouter ("Overriding the precedence of operators:\n", F_GREEN);
x = 2 + (8 % 3); // same as without parenthesis
std::cout << "The result of expression ' 2 + ( 8 % 3 ) ':" << tab << x << nline;
x = (2 + 8) % 3; // overriding the precedence
std::cout << "The result of expression ' ( 2 + 8 ) % 3 ':" << tab << x << nline << nline;
//! - in addition:
// evaluation of C++ operator from greatest to smallest happen in the following order:
// ===========================================================================-----------------------
// Level Precedence Group Operator Description Grouping
// ===========================================================================-----------------------
// 1 Scope :: scope qualifier left-to-right
// ===========================================================================-----------------------
// 2 Postfix (unary) ++ -- postfix increment / decrement left-to-right
// 2 " () functional forms "
// 2 " [] subscript "
// 2 " . -> member access "
// ===========================================================================-----------------------
// 3 Prefix (unary) ++ -- prefix increment / decrement Right-to-left
// 3 " ~ ! bitwise NOT / logical NOT "
// 3 " + - unary prefix "
// 3 " & * reference / dereference "
// 3 " new delete allocation / deallocation "
// 3 " sizeof parameter pack "
// 3 " (type) C-style type casting "
// ===========================================================================-----------------------
// 4 pointer-to-member .* ->* access pointer left-to-right
// 5 Arithmetic: scaling * / % multiply, divide, modulo "
// 6 Arithmetic: addition + - addition, subtraction "
// 7 Bitwise shift << >> shift left, shift right "
// 8 Relational < > <= >= comparison operators "
// 9 Equality == != equality / inequality "
// 10 And & bitwise AND "
// 11 Exclusive or ^ bitwise XOR "
// 12 Inclusive or | bitwise OR "
// 13 Conjunction && logical AND "
// 14 Disjunction || logical OR "
// Assignment-level = *= /= %= -= assignment / compound
// 15 expressions >>= <<= &= ^= |= assignment Right-to-left
// ? : conditional operator
// 16 sequencing , comma separator left-to-right
// ===========================================================================-----------------------
// an expression that has two the same precedence level operators, either left-to-right or right-to-left groupings determine which one is first to be evaluated. so every operator based on its defined precedence and grouping is independent.
// therefore, the consideration is there, that enclosure in parenthesis is a good practice and can improve the code readability.
//! - in addition:
// there are some other operators such as ones referring to pointers or
// the specifics for object-oriented programming, which will be introduced later on.
} catch (const std::exception&)
{
}
}
void _06_01_BasicInputOutput ()
{
try
{
ColourCouter (" -------------------------------------------------", F_bRED);
ColourCouter ("--------------------------------------------------\n\n", F_bRED);
//! ####################################################################
//! ~~~~~ basic input/output:
// standard features to interact with users.
// abstraction streams is the convenient way C++ use to perform input/ output operations in sequential media like screen.
// a stream is an entity, where a program can interact with, also inserting or extracting characters to/from.
// there are a handful of streams defined in the standard library to access the standard source and destination in an environment where the program runs.
// the most useful ones are:
// ------------------------------------------
// cin standard input stream
// cin standard output stream
// cin standard error (output) stream
// cin standard logging (output) stream
// ------------------------------------------
// The streams cin and cout will be mentioned in details.
// cerr and clog streams work like the cout stream with the difference,
// that they identify streams for error messages and logging
// which in many cases and most environment setups they do the same thing which is printing on screen,
// although they can be individually redirected.
ColourCouter ("~~~~~ Basic input/output (cin, cout, cerr and clog):\n", F_bPURPLE);
ColourCouter ("The streams are the standard features, which C++ language uses to interact with users.\n\n", F_YELLOW);
//! ####################################################################
//! ----- standard output (cout):
// default output of this stream is screen and processes the operation with using the insertion operator (<<)
// which inserts the data into the stream that precedes it
ColourCouter ("----- Standard output (cout):\n", F_bPURPLE);
ColourCouter ("Inserting outputs of different types:\n\n", F_YELLOW);
std::cout << "An output string\n"; // a string literal
std::cout << 23487284; // a number literal (numeral)
std::cout << '\n'; // a character literal
std::cout << nline; // a variable of type char (\n)
ColourCouter ("Chained insertion:\n", F_GREEN);
std::cout << "Current year is: " << tab << 2018 << nline << "My birth year is: " << tab << 1989 << nline;
//! - in addition:
// since cout dosen't automatically break the line, "\n" or endl manipulator will be used instead.
// endl manipulator break the line and flushes the buffer which means to physically write the output in device.
// this affects the fully-buffered systems but cout generally isn't one.
// it is a good idea to use it as an extra feature when needed,
// since it incurs a certain overhead and on some devices it may produce a delay.
std::cout << nline << "Breaking the line:" << nline;
std::cout << "\n ____This is the sentence between two break line characters____ \n" << std::endl;
//! ####################################################################
//! ----- standard input (cin):
// default input of this stream is keyboard, and processes the operation with the help of extraction operator (>>),
// which followed by the variable where the extracted data needs to be stored.
// the extraction of characters from input by cin stream will be continued till the user presses the enter or return key.
// depended on the type of the variable, cin stream determines how to interpret the entered characters.
ColourCouter ("----- Standard input (cin):\n", F_bPURPLE);
ColourCouter ("Extracting inputs of different types:\n\n", F_YELLOW);
char char_in;
std::cout << "Please enter a character as input:" << tab;
std::cin >> char_in;
std::cout << "You have entered:" << "\t\t\t" << char_in << nline << nline;
//! - in addition: