Introduction to Programming and C++

Contents Previous Topic Next Topic

Up until now, all the examples have either printed or manipulated explicit values: 1, 3.14, true, 'a', or "Hello World!". Such objects are called literals: what you see in your source code is literally what is used.

In reality, we need to be able to store more than just literal values, and we have to be able to modify those values. Here are two examples:

  1. As you move the mouse, the operating system must store where the mouse is currently sitting so that when you click, it knows what to do: If you click on an icon which represents a program or a document, it must execute the appropriate command and if you click in a window of a program which is already running, the operating system must indicate to that window that a click occurred and where that click occurred.
  2. As you type (or more likely, cut-and-paste) the programs in these tutorials into Dev C++ (you are using Dev C++ by now?) that information must be stored somewhere. As you edit a file and change a character, that character must be updated somewhere.

In order to store and modify information, we must be able to refer to a memory location somewhere and modify its contents. This is done through variables, that is, unlike the value of a literal which cannot be changed, the value of a variable may be changed.

In reality, each variable is associated with a memory location somewhere in main memory (RAM), however, as a programmer, you are not interested in where in RAM that information is stored. You're only interested in using it and manipulating it. To facilitate this, we represent the variable by a variable name, a sequence of alpha-numeric characters which must be start with a letter. The compiler will then associate that variable with a memory location in RAM and each time you refer to that variable, the program will use what is stored at the associated location and each time you modify the variable, the program will modify what is stored at that memory location.

One problem with variables: the compiler has to know what is being stored in that location so that it knows how to manipulate it. Therefore, we must declare the type of the variable before we use it. Declaring type may be done with

type variable_name;

Once the compiler knows the type of a variable, then it can:

  • Allocate enough memory to store an object of the given type, and
  • Know how to manipulate, print, update, the variable.

As you may suspect, a double uses twice as much memory as an int, and the sequence of instructions required to add to variables of type double is significantly different from the single machine instruction required to add two variables of type int.

Programs 1 and 2 show how a variable can be declared to be of type int and double, respectively. To assign a value to the memory location associated with the variable, use the construction:

variable_name = value;

Where variable is a name and value is something which is of the same type as variable. We say that the value is assigned to the variable variable_name.

Program 1. An integer (int) variable in main.

#include <iostream>

using namespace std;

int main() {
	int augend;   // declare 'augend' to be an integer

	// store the value 284 in the memory location associated with 'augend'
	augend = 284;

	int sum;      // declare 'sum' to be an integer

	sum = augend + 513;

	cout << "The sum of " << augend << " and 513 is " << sum << endl;

	return 0;
}

Program 2. A real-valued (double) variable in main.

#include <iostream>

using namespace std;

int main() {
	double augend;   // declare 'augend' to be a double

	// store the value 2.84 in the memory location associated with 'augend'
	augend = 2.84;

	double sum;      // declare 'sum' to be a double

	sum = augend + 51.39;

	cout << "The sum of " << augend << " and 51.39 is " << sum << endl;

	return 0;
}

One question you may have is "What is the value of augend after you declare it but before you assign to it. You can always try it out, as is shown in Program 3.

Program 3. Printing a real-valued (double) variable without initialization.

#include <iostream>

using namespace std;

int main() {
	double augend;   // declare 'augend' to be a double

	cout << "Before assigning, augend equals " << augend << endl;

	augend = 2.84;

	cout << "After assigning, augend equals " << augend << endl;

	return 0;
}

If you were to run this code, you would find that augend is initially assigned a value of 0, however, in C++ this is not always the case. To avoid this, some programming languages either: a) forbid you to use a variable until you have assigned a value to that variable (this is called initializing the variable) or b) automatically assign a default value of to all variables. C++ does neither, however, to avoid this situation, you can always use the following construct:

type variable_name = expression;

This declares the variable and immediately initializes it. Program 2 could therefore be rewritten as Program 4.

Program 4. Immediate initialization of variables.

#include <iostream>

using namespace std;

int main() {
	// store the value 2.84 in the memory location associated with 'augend'
	double augend = 2.84;

	double sum = augend + 51.39;

	cout << "The sum of " << augend << " and 51.39 is " << sum << endl;

	return 0;
}

Conventions

Instead of always saying store the value in the memory associated with variable_name, we will simply say, assign the value to variable_name. We will always initialize all variables which are of type int, double, or bool.

We can also assign a new value to a variable, including a value which uses the previous value, as is shown in Program 5.

Program 5. Reusing the same variable.

#include <iostream>

using namespace std;

int main() {
	int sum = 0;

	cout << "The current sum is " << sum << endl;

	sum = sum + 10;

	cout << "The current sum is " << sum << endl;

	sum = sum + 23;

	cout << "The current sum is " << sum << endl;

	sum = sum + 399;

	cout << "The current sum is " << sum << endl;

	return 0;
}

Multiple Variable Declarations

To protect you, the programmer, the compiler does not allow you to declare the same variable twice. If you need a different variable, pick a different name, otherwise, this will very quickly lead to bugs. If you remove either of the comments from Program 6, the program will not compile.

Program 6. Multiple declarations of the same variable.

#include <iostream>

using namespace std;

int main() {
	int sum = 0;

	// int sum = 532;
	// double sum = 532;

	return 0;
}

Contents Previous Topic Top Next Topic