Module 1 - C++ Basics
Module 1 - C++ Basics
What is C++ ?
▪ A cross-platform language that can be used to create high-performance
applications, such as game development, database system software, operating
systems, etc. It is also widely used for electronic embedded systems and robotics.
▪ C/C++ powers the world. Most OS kernels (Windows, Linux, Mac, iOS, Android),
web browsers, game engines use C/C++ in their development (ref: link).
using std::cout; tells the compiler to use cout object from the standard
library std (avoid typing its full name std::cout later)
Note: we should NOT do “using namespace std;” since it is considered as a BAD PRACTICE (ref: 1 2)
• Names must NOT be a reserved keyword in C or C++ (e.g string, short, float).
7
RMIT Classification: Trusted
Operators
• Arithmetic: +, -, /, *, % (modulo), ++ (increment), -- (decrement)
• Relational: a == b, a != b, a > b, a < b, a >= b, a <= b
• Logical: !a, a && b, a || b
8
RMIT Classification: Trusted
return 0;
}
Conditional Statements
▪ Conditinal operator: (condition) ? value if true : value if false;
▪ if-else ▪ switch-case
if (exp1) { switch (exp) {
stmt1; case value1:
} else if (exp2) { stmt1;
break;
stmt2;
case value2:
… stmt2;
} else { break;
stmt; …
} default:
stmt;
}
Note:
• exp, value1, value2 must be integer or char values
• The break statement terminates the switch block. Without it, all
cases presented after the matched case will be also executed.
10
RMIT Classification: Trusted
Loop Statements
• while is a pre-test loop • do-while is a post-test loop • init: initialize a start value
• stmt will run whenever • stmt runs first then the • cond: condition for executing
cond is non-zero cond is checked • update: update the value
init, exp, update are optional
11
RMIT Classification: Trusted
Example - Loop
#include <iostream> #include <iostream> #include <iostream>
using std::cout; using std::cout; using std::cout;
return 0;
}
Result: 1 2 3 4 5 6 7 8 9 10 Result: 1 2 3 4 5 6 7 8 9 10
Result: 1 2 3 4 6 7 8
Arrays
▪ Arrays hold items of the same data type
▪ One-dimensional array
• Syntax: data_type array_name[array_size];
▪ Multi-dimensional array
• int matrix[5][2] = { {1, 1}, {2, 8}, {3, 27}, {4, 64}, {5,125} };
13
RMIT Classification: Trusted
C-type Strings
#include <iostream>
#include <cstring>
▪ A C-type string is one-dimensional array of using std::cout;
characters terminated by a null character ‘\0’ int main() {
char s1[100] = {'H', 'e', 'l', 'l', 'o', '\0'}; // strcmp() example code:
char str1[100] = "ABC";
char s2[] = "Hello"; // '\0' is auto inserted char str2[100] = "DEF";
cout << "strlen of str1 = " << strlen(str1) << endl;
▪ Character handling functions from <cctype> library: cout << "strcmp = " << strcmp(str1, str2) << endl;
isdigit() isspace() isalpha() isupper() islower() tolower()
strcat(str1, str2);
toupper(), etc… cout << "str1 after strcat = " << str1 << endl;
strcpy(str1, str2);
▪ String handling functions from <cstring> library: cout << "str1 after strcpy = " << str1 << endl;
• strlen() // length
return 0;
• strcpy(), strncpy() // string copy }
• strcat(), strncat()// concatenate (append)
• strcmp(), strncat()// compare Value Compare string1 with string2
• ………………… the first character that does not match
<0 has a lower value in string1 than in
Important standard libraries to use: string2
1. https://www.asciitable.com/ (ASCII code of characters) 0 string1 is identical to string2
2. https://cplusplus.com/reference/cctype/ (character handling functions) the first character that does not match
3. http://www.cplusplus.com/reference/cstring (string handling functions) >0
has a greater value in ptr1 than in ptr2
14 14