As the worlds increasing use
of computer technology in the digital era continues apace, it becomes ever more
vital for engineers and developers to have a better understanding of the
technical processes at work in this ubiquitous and dynamic state of affairs.
This is especially true for programmers who’ll be
building the future software we’ll be using in our devices and machines in
years to come which we’ve already taken advantage of so much and come to rely
on in recent years.
Though C++ has only been around for about half the
time that computer programming itself has been in existence (25 years compared
to 50), in this time, it’s arguably become the most important programming
language for half a century.
Its complexity is both to its advantage and
disadvantage. On the one hand, virtually no other language is better placed to
deal with the challenges and demands created by the need for ever more
sophisticated software. On the other, it has an extremely rich feature set that
can seem intimidating to some at first glance.
In this series of tutorials, we’ll be looking at
the language from the ground up and forming a better understanding of
programming along the way.
For readers who’ve never written a program before,
I’ll state here and now that there’s nothing to fear. The tiger you’ve been
afraid of all this time is nothing more than a pussy cat.
Although
C++ is an extremely powerful language (it’s capable of building just about any
type of computer program) it’s also intuitive to use and once insight into it
is gained will become a great ally in projects and challenges that come your
way.
Gaining
such understanding isn’t difficult and I’ll guide you every step of the way to
the conclusion where you’ll be building complex programs to be proud of. So
let’s begin.
What is
a computer program anyway? Nothing more than a series of instructions supplied
to a computer to perform a given task – that’s what.
When
examined under the surface in greater detail, this statement encompasses a
whole number of things.
The
first thing is the concept of an algorithm. What is an algorithm? More or less
the same thing (a series of instructions etc) described in actual programming
terms.
To be
absolutely accurate, an algorithm is a series of steps followed by a computer
program – and each step is an instruction – which the program gives to the
hardware (the machinery) that comprises the computer.
Here’s
an example of an algorithm. It works out how old someone is.
1.Get
the current year
2.Get
the person’s birth year
3.Subtract
the number of years in the birth year from the current year
4.Output
the result (the persons age)
A very
simple example, I think you’ll agree. But in this simple document lies in
essence what an algorithm –and a computer program are all about – clear,
detailed steps (sometimes dizzying in their complexity for large programs)
providing instructions to a computer via a piece of software.
Basically,
a computer program is the expression of an algorithm. The algorithm is
manifested in the program.
Another
useful point to bear in mind is that virtually any algorithm (especially if
it’s a complicated one) can be written in almost any number of ways. But I
digress, so let’s move on.
As ever
in life, things are simple – but not that simple. If a programmer literally fed
those instructions into her PC, at worst she’d get a rude message (something
and something or other is undefined – in other words I don’t know what you’re
talking about) or at best (more likely) nothing would happen at all.
You see,
computers don’t (yet) understand human languages. In fact, they don’t
understand very much at all (except one thing – more about it later).
Computer
programming languages come in two varieties – high level languages and low
level languages. Most modern languages (like C++) are high level.
High
level languages are alpha numeric – that is, they are made up of alphabetical
and numerical characters (or letters and numbers to you non-geeks) in their
syntax (or language structure) which makes them easier to read and understand.
As
mentioned earlier however, computers find what we intuitively understand
completely perplexing. So we have to translate – or rather a particular type of
software does this for us. This software is called a compiler.
When a
computer program is written in a high level language like C++ before the
computer can understand the programmer’s instructions, the instructions (or
code) need to be compiled.
The
compilation of a program is a process which occurs in between the writing of it
and its execution (when it runs as software in front of you on the screen).
Compilers
are computer programs, sometimes used separately but often built within the
programmer’s IDE (Integrated Development Environment – a computer program for
writing computer programs) that perform this task.
When a
program is compiled it turns into something called machine code. The content of
this code is simply a series of ones and zeroes and if it sounds familiar,
you’re absolutely right because another name for machine code is binary code.
Since
the dawn of the age of modern computing, computers have understood instructions
in this form that we all learned at school where a sequence of ones and zeroes
indicates a value built from multiples of two increasing by the power of two
with each digit to the left.
The
reason computers use this system is simplicity (computers are pretty simple at
heart, bless) where each zero symbolises an ‘off’ and each 1 symbolises an
‘on’. Taken as a whole, a complete sequence in binary code made from a
succession of on-off switches form a set of instructions that equate to ‘yes’
or ‘no’ or ‘do’ or ‘don’t’. Should I make the tea? Yes. Shall I jump off this
cliff? No. Can I give you a massage? Do. Do I crash the car? Don’t! And so on…
So basically
that’s the language of computers (plus all the electronic machines and devices
you use every day) and it’s pretty dull (see how long you can stare at a long
stream of binary code before falling asleep – or getting dizzy). But the binary
system has informed everything in computing from circuit design to the
construction of the multi-core processor to the distribution of global networks
– so it’s pretty important.
As
mentioned, there are high level languages and low level languages. Use of low
level languages isn’t as common any more as the use of high level ones.
One
famous low level language is Assembly. It and other development systems like it
are called low level languages because they communicate more directly with
hardware.
Assembly
resembles machine code in its structure and because it’s understandable and
more native to the machinery of a PC it works faster. This is useful for
operations where speed is critical – such as the execution of instructions by a
Graphical Processing Unit (GPU) or code in a game that computes a lot of
information very fast.
Going
into further detail on the alpha numeric nature of high level languages – the
content of C++ is actually made up of familiar key words in English – which
translate to instructions that will become recognisable to the machine when the
code is compiled.
These
key words define entities with generic names like function, variable and
expression. In many languages (C++ included) for clarity, the unique names of
examples of these entities are preceded by their generic names – e.g. function:
Get Coffee.
Such key
words are known as reserved words, meaning they can’t be used in user defined
examples because they’re already built into the language itself. User defined
basically means an example of an entity or object created by the programmer.
So for
example, a programmer couldn’t create a function called Function. They would
receive an error message telling them that the word ‘function’ is a reserved
word.
Let’s
examine what these objects actually are, starting with variables.
A
variable is essentially a container for information that changes – hence the
name, variable – derived from variation – a change.
Variables
are categorized into different types called data types. Data type is simply a
programming term for ‘different type of information’. There are a considerable
number of data types in C++ (which we’ll encounter in future tutorials) but the
ones most commonly used are integer (for numeric information) and string (for text
based information).
As it
happens, C++ is particularly good with numbers and as one might expect, there
are many variations for integer variables so to speak.
Here are
some examples.
This is
a C++ string variable:
char
Name = Theodore;
The ‘char’
word is a keyword in C++ for the ‘Character’ data type. ‘Character’ is used for
strings or pieces of text data. In C++ every variable must be defined first
with a data type.
The word
‘Name’ is simply a string variable I’ve created and its value is my name.
The
semi-colon on the end of the line is a character you need to include at the end
of each line of code –or statement - in C++ (and other languages).
It
indicates that the particular instruction is complete and the computer can move
to the next line.
This is
an integer variable:
int
currentage = 44;
The
‘int’ keyword is short for ‘integer’. Integers are used for numeric data. I’ve
named this example of an integer ‘currentage’ and it’s value is my age which is
44.
We’ll be
looking at variables in greater detail in tutorial 3 in this series.
Next up
are expressions. Expressions are statements which return a value. Here’s an
example of an expression.
currentyear
– birthyear = currentage;
currentyear
and birthyear are both integers containing number values providing information
on the number of years in the current year and the number of years in someone’s
birth year.
When
birthyear uses the mathematical operator (the minus) between it and currentyear
to subtract from currentyear, the result is placed in another integer, the
currentage integer.
All
expressions behave in the same way. An expression is basically a calculation
and once performed generates a result or in programmer speak, ‘returns a
value’.
The next
step from writing statements is to write a function. A function is simply a
collection of related statements that focus on a particular task. Here’s an
example:
#include
"stdafx.h"
#include
using
namespace std;
int
FindTime (int distance, int speed)
{
int
time;
time =
distance/speed;
return
(time);
}
int main
() {
int
distance = 0;
int
speed = 0;
int time
= 0;
cout
> distance;
cout
> speed;
using
namespace std;
int
main(void)
{
int
birthyear = 0;
int
currentyear = 0;
int
currentage = 0;
//Get
the person’s birth year
cout
> birthyear;
//Get
the current year
cout
> currentyear;
//Subtract
the number of years in the person’s birth year from the number of
//years
in the current year
currentage
= currentyear - birthyear;
cout
<< "you are currently:
"
<< currentage << "years old";
return
0;
}
And
there we are. As you’ve seen from the code above, programming isn’t rocket
science (though it’s a very useful tool for the maths and physics involved in
rocket science).