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.
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
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
a computer program anyway? Nothing more than a series of instructions supplied
to a computer to perform a given task – that’s what.
examined under the surface in greater detail, this statement encompasses a
whole number of things.
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
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.
an example of an algorithm. It works out how old someone is.
the current year
the person’s birth year
the number of years in the birth year from the current year
the result (the persons age)
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.
a computer program is the expression of an algorithm. The algorithm is
manifested in the program.
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.
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.
computers don’t (yet) understand human languages. In fact, they don’t
understand very much at all (except one thing – more about it later).
programming languages come in two varieties – high level languages and low
level languages. Most modern languages (like C++) are high level.
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.
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.
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.
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).
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.
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.
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.
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…
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.
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.
famous low level language is Assembly. It and other development systems like it
are called low level languages because they communicate more directly with
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.
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.
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:
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.
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
examine what these objects actually are, starting with variables.
variable is essentially a container for information that changes – hence the
name, variable – derived from variation – a change.
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
happens, C++ is particularly good with numbers and as one might expect, there
are many variations for integer variables so to speak.
a C++ string variable:
Name = Theodore;
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.
‘Name’ is simply a string variable I’ve created and its value is my name.
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).
indicates that the particular instruction is complete and the computer can move
to the next line.
an integer variable:
currentage = 44;
‘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
looking at variables in greater detail in tutorial 3 in this series.
are expressions. Expressions are statements which return a value. Here’s an
example of an expression.
– birthyear = currentage;
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
birthyear uses the mathematical operator (the minus) between it and currentyear
to subtract from currentyear, the result is placed in another integer, the
expressions behave in the same way. An expression is basically a calculation
and once performed generates a result or in programmer speak, ‘returns a
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
FindTime (int distance, int speed)
distance = 0;
speed = 0;
birthyear = 0;
currentyear = 0;
currentage = 0;
the person’s birth year
the current year
the number of years in the person’s birth year from the number of
in the current year
= currentyear - birthyear;
<< "you are currently:
<< currentage << "years old";
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