C++ can be defined as a general-purpose, imperative, object-oriented programming language, that allows for low-level memory management. It also has generic programming capabilities.
Primitive data types
Boolean: bool
Character: char
Integer: int
Floating point: float
Double floating point: double
Valueless: void
Wide character: wchar_t
Numeric data types can also be:
signed
unsigned
short
long
Literal Values
C++ allows for different types of literals. It is useful to remember them since using them leads to a more fine-grained control over data stored in variables. Some literal examples are:
Integer: 212, 215u, 0xFeeL
Float: 3.14159, 314159E-5L, 210f
Comments
C++ supports inline and multi-line comments.
// In line comment!
/* In line comment! */
/* Multi-line
* comments!
*/
Hello World Program
All C++ programs have the basic syntax of the program bellow. There can only be one main() function in a C++ program, although a C++ program can have multiple files with many functions.
#include <iostream> //Library needed for std::cout
using namespace std; //Using this we can type “cout” instead of “std::cout”
// All C++ programs start in main()
int main() {
cout << “Hello, World!”; //Prints Hello World to the console
return 0;
}
Operators
C++ offers many operators. Operators can be divided in the categories bellow:
Arithmetic Operators: + – * / % ++
Relational Operators: == != > < >= <=
Logical Operators: || && !
Bitwise Operators: & | ~ ^ << >>
Assignment Operators: = += -= *= /= %= <<= >>= &= ^= |=
Some operators have a higher precedence compared to others. For instance, multiplication has a higher precedence then addition. Some have equal precedence such as addition and subtraction.
Arrays
In order to store data in a sequential fashion: arrays. Arrays are a collection of items of equal type. It allows you to create one variable that points to more than one value. The array variable actually points to the first element of a list of elements of the same type.
An array is declared the following manner:
type arrayName [ size ];
For instance, double numbers[5] creates an array called “numbers” that holds up to 5 double values. The code bellow populates the numbers array with 5 double values:
#include <iostream>
using namespace std;
int main () {
double numbers[ 5];
for ( int i = 0; i < 5; i++ ) {
numbers[i] = i * 2.3d;
}
}
C-String
We covered several data types so far, but we have not mentioned an essential data type that requires more detailed explanation right from the start: The C-Style Character String or C-String. The C-String as the name implies is the C programming language C. The C-String is a sequence of characters terminated by a special character called the null character. The null character is represented by ‘\0’. A C-String would then be created the following manner:
char hello[6] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
The cstring library contains a number of functions that manipulate c-strings. These functions are the following:
strcpy(s1, s2); //Copies string s2 into string s1.
strcat(s1, s2); //Concatenates string s2 onto the end of string s1.
strlen(s1); //Returns the length of string s1.
strcmp(s1, s2); //Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
strchr(s1, ch); //Returns a pointer to the first occurrence of character ch in string s1.
strstr(s1, s2); //Returns a pointer to the first occurrence of string s2 in string s1.
Loop Control Structures
When creating a program you will often want to perform an action repeatedly. That’s what loop structures are for. C++ offers several types of loop structures. Each can be used in slightly different scenarios:
while loop: Repeats the logic while a given condition is true. It tests the condition before executing the logic every time.
for loop: Repeats the logic up to a certain number of times.
do…while loop: Similar to while, but check the condition after executing the logic in the loop body.
The example bellow populates the numbers array in 3 different ways:
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
int main () {
double numbers[ 5];
for ( int i = 0; i < 5; i++ ) {
numbers[i] = i * 2.3d;
}
int i = 0;
while(i < 5) {
numbers[i] = i * 2.3d;
i++;
}
i = 0;
do {
numbers[i] = i * 2.3d;
i++;
} while (i < 5);
}
There’s a lot more to C++. In the next article will cover more aspects of the language, such as, control structures, pointers and more.