In most programming languages, you have the concept of variables. These are named objects that hold a value (more formerly referred to as state). By manipulating a variable, you manipulate the state of the object the variable refers to.
add.cpp
void addFunction()
{
int x = 0;
x = x + 5;
int y = x + 3;
}
C++ is a strongly typed language. This means each variable has a specific type that does not change (above that type is int). The operations that can be performed on an object depend on the object's type, and the operation's result can depend on the types involved. C++ has several built-in types (listed below) but allows the definition of new user-defined types (which will be described in a later article). The standard library provides a set of commonly used standard types (listed below).
Built-in Types
char
bool
short
int
long
long long
float
double
Standard Types
std::string
std::vector<T>
std::array<T, size>
std::list<T>
std::map<Key, Value>
std::set<Key>
The list may seem a bit daunting at first, but while you are learning if you restrict yourselves to three built-in types (bool, int and double) and two standard types (std::string and std::vector<T>) you will be able to solve most beginner/training problems.
The other built-in types are usually used when you need a wider range of values or need to save space. The additional standard types (shown above) are different types of containers that provide different access characteristics (which will be explained later). We will eventually cover all these types.
So, an example of usage of the most common types is:
variables.cpp
#include <string>
#include <vector>
#include <iostream>
int main()
{
int age = 28;
std::string name = "Loki";
double grade = 12.45;
std::vector<std::string> courseNames = { "C++", "Teaching", "Maths", "Art", "Music"};
std::cout << "Name: " << name << "\n";
std::cout << "Age: " << age << "\n";
std::cout << "Grade:" << grade << "\n";
std::cout << "Course 1: " << courseNames[1] << "\n";
}