C++ is one of a number of well-known computer languages, about which we may say:

  1. They genuinely are languages.
  2. The good news is they have a very small, closed vocabulary.
  3. The bad news is they are much fussier about grammar than a grade eight english teacher!

The purpose of computer languages is for human programmers to write out detailed instructions for computers to carry out.

Our objective is to get you familiar enough with C++ to write meaningful sets of instructions for computers.

There are two aspects to this:

Syntax
The computer engineering term for grammar. You have to form your instructions correctly, or they will be rejected by the compiler.
Semantics
The computer engineering term for meaning. My dog flies books is a well formed English sentence, whose meaning is at least suspect. The compiler is a grammar cop only. It is incapable of checking for meaning.

When you learn a new human language you have to learn both the grammar and the vocabulary. However we generally learn concentrate on vocabulary equivalent to our own. Fermez la porte means the same in French as its English counterpart Close the door.

Learning a computer language is a little different. There is no equivalent in C++ to Close the door. While C++'s vocabulary is very small, it requires you to understand a whole new set of concepts. Consider the sentence,

One whose theories are based on a concept of strength will lose little ground if he can make a stand on the claim that an all-or-none acquisition is simply a peculiarity of the paired-association experiment and not characteristic of human learning in general (p. 115 of a Cognitive Theory on Learning by Marvin Levine).

While the vocabulary is relatively straightforward it is hard to make much of the sentence without knowing something about cognitive psychology.

Similarly, to learn computer languages effectively (to make them meaningful) you will have to learn something about how computers work.

So, throughout the course, there will be twin threads, woven together: Syntax—get the grammar right. Semantics—make it meaningful.

Initially, we will focus on syntax.

Some Language Elements

Alphabet

Uppercase & lowercase roman letters, A-Z and a-z, the digits 0-9, a well defined set of symbols available on every standard keyboard, e.g.

< > { } ( ) : ; , . ? / * & + - ! ^ ' " = | _

Some non-printing characters, the most common of which are the newline (\n) and tab ( \t ) characters

Any continuous combination of newline, tab and/or spaces are collectively known as whitespace

Words are Tokens

The programming language equivalent to word is token.

Simply put

  1. Tokens contain no whitespace
  2. Operators are tokens
  3. Words with no whitespace are tokens

So the following are all C++ tokens: while window { x George +

The following expression contains five tokens: (x1+b)

They are (    x1    +    b    )

You can see why the more specialised term token was adopted. while window George and x look like words but { and + don't

There are effectively four kinds of words

keywords
words defined for the language. Basically, its vocabulary.
identifiers
words created by the programmer as names for things.
symbols
Sometimes single as { + * and sometimes in pairs as != or /*
literals
A constant written directly into the text such as 3.14159 or "Hello". See below.

Keywords

Keywords are sometimes called reserved words as programmers may not use them as identifiers. They are reserved for the language. There are only about a hundred keywords in C++ and we won't be using all of them. Here is a list of the ones we will use:

int   double   char   bool   if   else   for   while   do   using   namespace   return   void   true   false   const

And that's all.

Symbols

C++ is rich in symbols but, again, we won't use all of them. Here's the one's we will use:

+  -  *  /  %  =   ==   !=   <   >   <=   >=   { }   ( ) [ ]   &&  ||  !   ++   --    /*  */  //   ;  ,  .  += -= *= /=  <<  >>

Identifiers and the Naming Rules

Identifiers are the names we, as programmers, give to things. They have to conform to the naming rules.

Here are some legal identifiers:

x   y   i   circle   arg22   thisIsAVeryLongIdentifier   swap   Swap   PI   DEFAULT_WINDOW   _legalButDontUse

C++ is case sensitive so swap and Swap are different names.

And some bad ones

1x   thisIsAnIncrediblyLongIdentifier1    thisIsAnIncrediblyLongIdentifier2   default-window

1x starts with a digit which is illegal

default-window includes a - which is illegal

The two long identifiers are legal but the difference between the two of them occur past the 31st character which means that they may not be distinguished by all compilers

Name Style Rules

Here are some style rules we will use for names.

Sentences are Statements

The equivalent of a sentence is a statement. Statements are always terminated by a semicolon.

x = y + z - 4;
cout << "Hello world!\n";

spaces have been inserted for readability but are not required

x=y+z-4;
cout<<"Hello world!\n";

are fine as well. It is not even necessary to stay on a single line.

x = 24*gorganzola - 14*emmenthaler
     + 17*balderson;
cout  <<  
	"The quick brown fox jumped over the lazy dogs\n";

are both legal.

Paragraphs are Blocks

Again we have a different technical term. The C++ equivalent of a paragraph is a block—a set of statements enclosed in a pair of curly brackets { }

We can turn the above statements into a block as follows:

{
   x = y + z - 4;
   cout << "Hello world!\n";
}

It is considered good style to indent statements inside a block.

Some Specifics

Comments

Comments are for people. The precompiler actually throws them away

They are a major mechanism for documenting programs.

They come in two flavours:

              // A single line comment. Ends at the end of the line
   /*
   * Comments enclosed in slash-asterix ... asterix-slash can
   * extend over several lines
   */

Literals

A literal is a constant as it appears in the text of the code. For example, the line of code

x = 3;

tells the computer to set x to the value 3. 3 is a literal as it appears in the text code.

integer literals
standard integers such as 1, 0, 2,  -17 or 2056. There are also some special ones such as 0xff, the hexadecimal (base 16) representation of 255. We won't worry about these this term.
double literals
real number literals using decimal notation (3., 3.0, 3.14159 or -17.65) or floating point (exponential) notation (1.0e11 or -23.6e-3)
character literals
single characters such as 'a', 'x', 'H', '!' or '\n'
string literals
a sequence of characters such as "hello world!\n", "Michael" or
"The quick fox jumped over the lazy dogs."

Note that single characters are always enclosed by a single quote while a set of characters, known as a string, are always enclosed in double quotes.

The Standard Output Stream

When we write

cout << "Hello world!\n";

cout is the standard output stream while the << is actually an operator, called the insertion operator. So the line actually means, insert the string to the right of the operator into the stream at the left of it. The standard output stream is normally connected to a console, a plain text window. (The word console is historical, harking back to the era of large, central computers. A console was a simple, dumb terminal with no computing ability of its own.)

You can insert multiple items into the output stream, for example

cout << "Hello " << "world!\n";

puts exactly the same thing to the screen as does the first example, as does this one

cout << "Hello ";
cout << "world!\n";

When you are done outputting a string literal to the screen, it does not force a newline (unless of course, you put one in, as we did at the end of world). The next output takes up exactly where the old one left off.

The endl Token

There is another way to force a newline. We can insert an endl token into the output stream like this.

cout << "Hello world!" << endl ;

Why have two ways? As it happens, there is a difference, which has to do with the way computers write to the screen. The actual writing is handled by the operating system (e.g. Windows XP, Linux, Mac OSX). For reasons of efficiency, output requests are normally written in a two step process. They are first written to a buffer. Then, sometime later, the contents of the buffer get written to the screen.

This means you might output something and it won't appear right away. If you toss and endl into the output stream, it not only ends the line, it also requests that the buffer contents be written to the screen at once. So for interaction with humans, we often use endl.

This example pulls together most of the preceding code samplets so you can see them in action.

Outputting Other Data

The output stream routines know how to handle most built-in data types (and can be told how to handle user defined types as well).

Thus we can write

Run this example yourself. Note that a line of cout code does not necessarily produce a line of output. The next piece of data output starts right where the last one left off. If you want to start a new line, you have to output either a '\n' (alone or as part of a string literal) or an endl.

Arithmetic Expressions

Expressions occur in C++ in many places. Most are easily read by engineering students as they are often based on mathematical expressions. Again, we will study them in detail shortly. What you need to know now is that when an expression is encountered in a statement while a program is running, it is always evaluated. In C++, that quite literally means reduced to a single value. The standard arithmetic operators should be familiar except that in programming a * is always used to signify multiplication instead of an x.

Here's a very simple program that just ouputs the results of evaluating some expressions.

Good Code, Bad Code

Here are different versions of the hello world program, stripped of comments. See if you can tell which are good code and which are not. Click the answer button to see if you are right.

#include <iostream>
using namespace std;
int main(){
cout<<"Hello world!\n";
return 0;
}

This is legal code. As a matter of style the cout<<"Hello world!\n"; should be indented but poor style does not render the code wrong. It does make it harder to read and maintain.

#include <iostream>
using   namespace   std   ;
int main(){
   cout  <<   "Hello world!\n"   ;
return 0  ;
}

This is legal code. The extra spaces are simply ignored.

int main(){
    cout<<"Hello world!\n";
return 0;
}

This is ilegal code. cout is a name from the library and won't be recognized because <iostream> has not been included.

#include <iostream>
int main(){
    std::cout<<"Hello world!\n";
return 0;
}

This is legal code. The using namespace has been dropped but the full name of cout has been used instead.

#include <iostream>
using namespace std;
int main()
{
   cout<<"Hello world!\n";
return 0;}  

This is legal code. Which line the brackets go on is a matter of style.

#include <iostream>
using namespace std; int main(){ cout<<"Hello world!\n"; return 0; } 

This is legal code, The #include should be on a separate line (as it is) but most other C++ code is not line sensitive. C++ is a character oriented language. Using lines is to make code easier for people to read.

Exercises

  1. Go to the outputs.cpp TM example above and edit the second set of outputs to present the numeric data differently. For example, separate the nos with a comma followed by a space, or put each number on its own line.
  2. Spot the syntax (grammar) errors in the following code:
    #include <iostream>
    using namespace std;
    
    
    main()
        cout < "All is vanity/n;
        return 0
    }

#include <iostream>
using namespace std;

int main(){
    cout << "All is vanity/n";
    return 0;
}

Examples Shown in Full