Variables
In most programming languages you have the concept of variables. These are simply named objects that hold a value (more formerly refereed to as state). By manipulating a variable you manipulate the state of the object that the variable referees too.
add.cpp
void addFunction()
{
int x = 0;
x = x + 5;
int y = x + 3;
}
C++ is a strongly typed language. This means that each variable has a specific type that does not change (above that type is int). The operations that can be performed on an object are dependent on the type of the object and the result of the operation 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 user defined 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 larger range of values or need to save space. The additional standard type (shown above) are different types of container and provide different accesses characteristics (which will be explained later). We will cover all these types in due course.
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";
}