Sunday, 19 August 2012

C++ Programming Tutorial Lesson #2 - Variables and Constants


What are variables?

A variable is a space in memory where a program stores data. Variables can store numbers and letters among other things.

Declaring variables

Before you can use a variable you must declare it. When you declare a variable you choose its data type and give it a name. Here is an example of how to declare a variable of the data type integer with the name i.
int i;
Here is a table of the basic data types that C++ supports.
NameStores
booltrue or false
charCharacters
intNumbers
floatNumbers
doubleNumbers
You can declare 2 variables of the same type on one line by separating them with a comma.
int i, j;

Using variables

You set the value of a variable using a =. Here is an example of how to set the value of an int called i to 5.
int i;
i = 5;
You can set the value of a variable at the same time as you declare it. This is called initialization.
int i = 5;
A char is a letter, number or any special character. You must put char values inside single quotes.
char c = 'a';
A bool variable can only store either true or false.
bool b = true;
bool c = false;
A variable can also be signed or unsigned. Signed means that it can have negative numbers and unsigned means it can't but unsigned gives a variable a greater positive range. You simply put the words signed or unsigned in front of a variable declaration to use them.
unsigned int i;
Putting short or long in front of a variable gives it a smaller or bigger range respectively.
short int i;
Using signed, unsigned, short and long without a variable type will use int by default.
signed s;
unsigned u;
short sh;
long l;
A string is a type of variable that can store words and sentences. There are 2 kinds of strings. The first kind is a string pointer. You declare it as a *char. The * means that it points to the first char of the string.
char *s;
s = "Hello";
The second kind is an array of characters which you must give a size when you declare it. You have to use the strcpy command to put values in it. You must also include the string header file to be able to use the strcpy command.
#include<string>
...
char t[10];
strcpy(t,"Hello");

User input and output with variables

The cout command can be used to print the value of a variable on the screen. If you want to print text and variable values at the same time then you must separate them with a <<.
int age = 18;
cout << "Your age is " << age;
The cin command is used for reading in values that are entered by the user. When you read a value you must store it inside a variable. Here is an example of how to read an int into a variable.
int age;
cout << "Enter your age: ";
cin >> age;

Calculations with variables

You can add, subtract, multiply and divide when working with variables. When you do this you must store the result in a variable. You can also use variables in calculations including the variable that is going to store the result.
int i, j, k, l;
i = 1 + 2;
j = 5 - 3;
k = 10 * 2;
l = 100 / 5;
i = j + 5;
i = i - 2;

What are constants?

A constant is a variable whose value can't change. Constants are used to give a meaningful name to a value such as the name PI for 3.14. The value of a constant must always be set when it is declared. There are 2 types of constants. The first uses the word const in front of the declaration.
const PI = 3.14;
The other type of constant uses #define to create a constant.
#define PI 3.14