C++ class notes
INTRODUCTION
A simple C++ program
Before looking at how to write C++ programs consider the following simple example program.
// Sample program
// IEA September 1995
// Reads values for the length and width of a rectangle
// and returns the perimeter and area of the rectangle.
#include <iostream.h>
void main()
{
int length, width;
int perimeter, area; // declarations
cout << "Length = "; // prompt user
cin >> length; // enter length
cout << "Width = "; // prompt user
cin >> width; // input width
perimeter = 2*(length+width); // compute perimeter
area = length*width; // compute area
cout << endl
<< "Perimeter is " << perimeter;
cout << endl
<< "Area is " << area
<< endl; // output results
} // end of main program
Download program.
The following points should be noted in the above program:
Any text from the symbols // until the end of the line is ignored by the compiler. This facility allows the programmer to insert Comments in the program. Every program should at least have a comment indicating the programmer's name, when it was written and what the program actually does. Any program that is not very simple should also have further comments indicating the major steps carried out and explaining any particularly complex piece of programming. This is essential if the program has to be amended or corrected at a later date.
The line
#include <iostream.h>
must start in column one. It causes the compiler to include the text of the named file (in this case iostream.h) in the program at this point. The file iostream.h is a system supplied file which has definitions in it which are required if the program is going to use stream input or output. All your programs will include this file. This statement is a compiler directive -- that is it gives information to the compiler but does not cause any executable code to be produced.
The actual program consists of the function main which commences at the line
void main()
All programs must have a function main. Note that the opening brace ({) marks the beginning of the body of the function, while the closing brace (}) indicates the end of the body of the function. The word void indicates that main does not return a value. Running the program consists of obeying the statements in the body of the function main.
The body of the function main contains the actual code which is executed by the computer and is enclosed, as noted above, in braces {}.
Every statement which instructs the computer to do something is terminated by a semi-colon. Symbols such as main(), { } etc. are not instructions to do something and hence are not followed by a semi-colon.
Sequences of characters enclosed in double quotes are literal strings. Thus instructions such as
cout << "Length = "
send the quoted characters to the output stream cout. The special identifier endl when sent to an output stream will cause a newline to be taken on output.
All variables that are used in a program must be declared and given a type. In this case all the variables are of type int, i.e. whole numbers. Thus the statement
int length, width;
declares to the compiler that integer variables length and width are going to be used by the program. The compiler reserves space in memory for these variables.
Values can be given to variables by the assignment statement, e.g. the statement
area = length*width;
evaluates the expression on the right-hand side of the equals sign using the current values of length and width and assigns the resulting value to the variable area.
Layout of the program is quite arbitrary, i.e. new lines, spaces etc. can be inserted wherever desired and will be ignored by the compiler. The prime aim of additional spaces, new lines, etc. is to make the program more readable. However superfluous spaces or new lines must not be inserted in words like main, cout, in variable names or in strings (unless you actually want them printed).
Subsections
· Summary
Variables
A variable is the name used for the quantities which are manipulated by a computer program. For example a program that reads a series of numbers and sums them will have to have a variable to represent each number as it is entered and a variable to represent the sum of the numbers.
In order to distinguish between different variables, they must be given identifiers, names which distinguish them from all other variables. This is similar to elementary algebra, when one is taught to write ``Let is an identifier for the value of the acceleration. The rules of C++ for valid identifiers state that:
An identifier must:
· start with a letter
· consist only of letters, the digits 0-9, or the underscore symbol _
· not be a reserved word
Reserved words
The syntax rules (or grammar) of C++ define certain symbols to have a unique meaning within a C++ program. These symbols, the reserved words, must not be used for any other purposes. The reserved words already used are int and void. All reserved words are in lower-case letters. The table below lists the reserved words of C++.
C++ Reserved Words and | and_eq | asm | auto | bitand |
bitor | bool | break | case | catch |
char | class | const | const_cast | continue |
default | delete | do | double | dynamic_cast |
else | enum | explicit | export | extern |
false | float | for | friend | goto |
if | inline | int | long | mutable |
namespace | new | not | not_eq | operator |
or | or_eq | private | protected | public |
register | reinterpret_cast | return | short | signed |
sizeof | static | static_cast | struct | switch |
template | this | throw | true | try |
typedef | typeid | typename | union | unsigned |
using | virtual | void | volatile | wchar_t |
while | xor | xor_eq |
Some of these reserved words may not be treated as reserved by older compilers. However you would do well to avoid their use. Other compilers may add their own reserved words. Typical are those used by Borland compilers for the PC, which add near, far, huge, cdecl, and pascal.
Notice that main is not a reserved word. However, this is a fairly technical distinction, and for practical purposes you are advised to treat main, cin, and cout as if they were reserved as well.
Declaration of variables
In C++ (as in many other programming languages) all the variables that a program is going to use must be declared prior to use. Declaration of a variable serves two purposes:
· It associates a type and an identifier (or name) with the variable. The type allows the compiler to interpret statements correctly. For example in the CPU the instruction to add two integer values together is different from the instruction to add two floating-point values together. Hence the compiler must know the type of the variables so it can generate the correct add instruction.
· It allows the compiler to decide how much storage space to allocate for storage of the value associated with the identifier and to assign an address for each variable which can be used in code generation.
For the moment only four variable types are considered, namely, int, float, bool and char. These types hold values as follows:
int
variables can represent negative and positive integer values (whole numbers). There is a limit on the size of value that can be represented, which depends on the number of bytes of storage allocated to an int variable by the computer system and compiler being used. On a PC most compilers allocate two bytes for each int which gives a range of -32768 to +32767. On workstations, four bytes are usually allocated, giving a range of -2147483648 to 2147483647. It is important to note that integers are represented exactly in computer memory.
float
variables can represent any real numeric value, that is both whole numbers and numbers that require digits after the decimal point. The accuracy and the range of numbers represented is dependent on the computer system. Usually four bytes are allocated for float variables, this gives an accuracy of about six significant figures and a range of about to . It is important to note that float values are only represented approximately.
bool
variables can only hold the values true or false. These variables are known as boolean variables in honour of George Boole, an Irish mathematician who invented boolean algebra.
char
variables represent a single character -- a letter, a digit or a punctuation character. They usually occupy one byte, giving 256 different possible characters. The bit patterns for characters usually conform to the American Standard Code for Information Interchange (ASCII).
Examples of values for such variables are:
int 123 -56 0 5645
float 16.315 -0.67 31.567
char '+' 'A' 'a' '*' '7'
A typical set of variable declarations that might appear at the beginning of a program could be as follows:
int i, j, count;
float sum, product;
char ch;
bool passed_exam;
which declares integer variables i, j and count, real variables sum and product, a character variable ch, and a boolean variable pass_exam.
A variable declaration has the form:
A variable declaration has the form:
type identifier-list;
type specifies the type of the variables being declared. The identifier-list is a list of the identifiers of the variables being declared, separated by commas.
Variables may be initialised at the time of declaration by assigning a value to them as in the following example:
int i, j, count = 0;
float sum = 0.0, product;
char ch = '7';
bool passed_exam = false;
which assigns the value 0 to the integer variable count and the value 0.0 to the real variable sum. The character variable ch is initialised with the character 7. i, j, and product have no initial value specified, so the program should make no assumption about their contents.
Constants and the declaration of constants
Often in programming numerical constants are used, e.g. the value of
Algorithms
Informally, an algorithm is a series of instructions which if performed in order will solve a problem. For an algorithm to be suitable for computer use it must possess various properties:
Finiteness: The algorithm must terminate after a finite number of steps. For example the algorithm:
produce first digit of 1/7.
while there are more digits of 1/7 do
produce next digit.
never terminates because 1/7 cannot be expressed in a finite number of decimal places.
Non-ambiguity: Each step must be precisely defined. For example the statement
set kto the remainder when mis divided by n.
is not precise because there is no generally accepted definition for what the remainder is when m is divided by n when m and n are negative. Different programming languages may well interpret this differently.
Effectiveness: This basically means that all the operations performed in the algorithm can actually be carried out, and in a finite time. Thus statements like `if there are 5 successive 5's in the expansion of FPRIVATE "TYPE=PICT;ALT=\( \pi \)"then ...' may not be able to be answered.
Even if an algorithm satisfies the above criteria it may not be a practical way of solving a problem. While an algorithm may execute in a finite time it is not much use if that finite time is so large as to make solution completely impractical. Thus there is a lot of interest in finding `good' algorithms which generate correct solutions in a short time compared with other algorithms. In sorting 10,000 numbers into ascending order a `good' algorithm executing on a PC took less than a second while a `poor' algorithm took over 10 minutes.
Describing an Algorithm
A simple example is used to look at the problem of designing an algorithm in a suitable form for implementation on a computer. The simple computational problem considered is:
Write a program to input some numbers and output their average.
This is a very informal specification and is not complete enough to define exactly what the program should do. For example where are the numbers going to come from--entered by the user from the keyboard or perhaps read from a file? Obviously to find the average of a series of numbers one adds them together to find their sum and then divides by the number of numbers in the series. So how is the number of numbers entered known? For example it could be input as part of the data of the program or the program could count the numbers as they are entered. This supposes that the program has some way of knowing when the user has stopped entering numbers or when the end of the file has been reached.
Structure of a program | Published by Juan Soulie |
Last update on Aug 20, 2008 at 10:57am UTC |
Probably the best way to start learning a programming language is by writing a program. Therefore, here is our first program:
// my first program in C++ #include <iostream> using namespace std; int main () { cout << "Hello World!"; return 0; } | Hello World! |
The first panel shows the source code for our first program. The second one shows the result of the program once compiled and executed. The way to edit and compile a program depends on the compiler you are using. Depending on whether it has a Development Interface or not and on its version. Consult the compilers section and the manual or help included with your compiler if you have doubts on how to compile a C++ console program.
The previous program is the typical program that programmer apprentices write for the first time, and its result is the printing on screen of the "Hello World!" sentence. It is one of the simplest programs that can be written in C++, but it already contains the fundamental components that every C++ program has. We are going to look line by line at the code we have just written:
// my first program in C++
This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. The programmer can use them to include short explanations or observations within the source code itself. In this case, the line is a brief description of what our program is.
#include <iostream>
Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program.
using namespace std;
All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. So in order to access its functionality we declare with this expression that we will be using these entities. This line is very frequent in C++ programs that use the standard library, and in fact it will be included in most of the source codes included in these tutorials.
int main ()
This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.
The word main is followed in the code by a pair of parentheses (()). That is because it is a function declaration: In C++, what differentiates a function declaration from other types of expressions are these parentheses that follow its name. Optionally, these parentheses may enclose a list of parameters within them.
Right after these parentheses we can find the body of the main function enclosed in braces ({}). What is contained within these braces is what the function does when it is executed.
cout << "Hello World!";
This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program.
cout represents the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (which usually is the screen).
cout is declared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code.
Notice that the statement ends with a semicolon character (;). This character is used to mark the end of the statement and in fact it must be included at the end of all expression statements in all C++ programs (one of the most common syntax errors is indeed to forget to include some semicolon after a statement).
return 0;
The return statement causes the main function to finish. return may be followed by a return code (in our example is followed by the return code 0). A return code of 0 for the main function is generally interpreted as the program worked as expected without any errors during its execution. This is the most usual way to end a C++ console program.
You may have noticed that not all the lines of this program perform actions when the code is executed. There were lines containing only comments (those beginning by //). There were lines with directives for the compiler's preprocessor (those beginning by #). Then there were lines that began the declaration of a function (in this case, the main function) and, finally lines with statements (like the insertion into cout), which were all included within the block delimited by the braces ({}) of the main function.
The program has been structured in different lines in order to be more readable, but in C++, we do not have strict rules on how to separate instructions in different lines. For example, instead of
int main () { cout << " Hello World!"; return 0; } |
We could have written:
int main () { cout << "Hello World!"; return 0; } |
All in just one line and this would have had exactly the same meaning as the previous code.
In C++, the separation between statements is specified with an ending semicolon (;) at the end of each one, so the separation in different code lines does not matter at all for this purpose. We can write many statements per line or write a single statement that takes many code lines. The division of code in different lines serves only to make it more legible and schematic for the humans that may read it.
Let us add an additional instruction to our first program:
// my second program in C++ #include <iostream> using namespace std; int main () { cout << "Hello World! "; cout << "I'm a C++ program"; return 0; } | Hello World! I'm a C++ program |
In this case, we performed two insertions into cout in two different statements. Once again, the separation in different lines of code has been done just to give greater readability to the program, since main could have been perfectly valid defined this way:
int main () { cout << " Hello World! "; cout << " I'm a C++ program "; return 0; } |
We were also free to divide the code into more lines if we considered it more convenient:
int main () { cout << "Hello World!"; cout << "I'm a C++ program"; return 0; } |
And the result would again have been exactly the same as in the previous examples.
Preprocessor directives (those that begin by #) are out of this general rule since they are not statements. They are lines read and processed by the preprocessor and do not produce any code by themselves. Preprocessor directives must be specified in their own line and do not have to end with a semicolon (;).
Comments
Comments are parts of the source code disregarded by the compiler. They simply do nothing. Their purpose is only to allow the programmer to insert notes or descriptions embedded within the source code.
C++ supports two ways to insert comments:
// line comment /* block comment */ |
The first of them, known as line comment, discards everything from where the pair of slash signs (//) is found up to the end of that same line. The second one, known as block comment, discards everything between the /* characters and the first appearance of the */ characters, with the possibility of including more than one line.
We are going to add comments to our second program:
We are going to add comments to our second program:
/* my second program in C++ with more comments */ #include <iostream> using namespace std; int main () { cout << "Hello World! "; // prints Hello World! cout << "I'm a C++ program"; // prints I'm a C++ program return 0; } | Hello World! I'm a C++ program |
If you include comments within the source code of your programs without using the comment characters combinations //, /* or */, the compiler will take them as if they were C++ expressions, most likely causing one or several error messages when you compile it.
Variables. Data Types. | Published by Juan Soulie |
Last update on Jul 19, 2008 at 1:57pm UTC |
The usefulness of the "Hello World" programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting program just to obtain a simple sentence written on the screen as result. It certainly would have been much faster to type the output sentence by ourselves. However, programming is not limited only to printing simple texts on the screen. In order to go a little further on and to become able to write programs that perform useful tasks that really save us work we need to introduce the concept of variable.
Let us think that I ask you to retain the number 5 in your mental memory, and then I ask you to memorize also the number 2 at the same time. You have just stored two different values in your memory. Now, if I ask you to add 1 to the first number I said, you should be retaining the numbers 6 (that is 5+1) and 2 in your memory. Values that we could now for example subtract and obtain 4 as result.
The whole process that you have just done with your mental memory is a simile of what a computer can do with two variables. The same process can be expressed in C++ with the following instruction set:
a = 5; b = 2; a = a + 1; result = a - b; |
Obviously, this is a very simple example since we have only used two small integer values, but consider that your computer can store millions of numbers like these at the same time and conduct sophisticated mathematical operations with them.
Therefore, we can define a variable as a portion of memory to store a determined value.
Each variable needs an identifier that distinguishes it from the others, for example, in the previous code the variable identifiers were a, b and result, but we could have called the variables any names we wanted to invent, as long as they were valid identifiers.
Identifiers
A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. In addition, variable identifiers always have to begin with a letter. They can also begin with an underline character (_ ), but in some cases these may be reserved for compiler specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere. In no case they can begin with a digit.
Another rule that you have to consider when inventing your own identifiers is that they cannot match any keyword of the C++ language nor your compiler's specific ones, which are reserved keywords. The standard reserved keywords are:
asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while
Additionally, alternative representations for some operators cannot be used as identifiers since they are reserved words under some circumstances:
and, and_eq, bitand, bitor, compl, not, not_eq, or, or_eq, xor, xor_eq
Your compiler may also include some additional specific reserved keywords.
Very important: The C++ language is a "case sensitive" language. That means that an identifier written in capital letters is not equivalent to another one with the same name but written in small letters. Thus, for example, the RESULT variable is not the same as the result variable or the Result variable. These are three different variable identifiers.
Very important: The C++ language is a "case sensitive" language. That means that an identifier written in capital letters is not equivalent to another one with the same name but written in small letters. Thus, for example, the RESULT variable is not the same as the result variable or the Result variable. These are three different variable identifiers.
Fundamental data types
When programming, we store the variables in our computer's memory, but the computer has to know what kind of data we want to store in them, since it is not going to occupy the same amount of memory to store a simple number than to store a single letter or a large number, and they are not going to be interpreted the same way.
The memory in our computers is organized in bytes. A byte is the minimum amount of memory that we can manage in C++. A byte can store a relatively small amount of data: one single character or a small integer (generally an integer between 0 and 255). In addition, the computer can manipulate more complex data types that come from grouping several bytes, such as long numbers or non-integer numbers.
Next you have a summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one:
Name | Description | Size* | Range* |
char | Character or small integer. | 1byte | signed: -128 to 127 unsigned: 0 to 255 |
short int (short) | Short Integer. | 2bytes | signed: -32768 to 32767 unsigned: 0 to 65535 |
int | Integer. | 4bytes | signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 |
long int (long) | Long integer. | 4bytes | signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 |
bool | Boolean value. It can take one of two values: true or false. | 1byte | true or false |
float | Floating point number. | 4bytes | +/- 3.4e +/- 38 (~7 digits) |
double | Double precision floating point number. | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
long double | Long double precision floating point number. | 8bytes | +/- 1.7e +/- 308 (~15 digits) |
wchar_t | Wide character. | 2 or 4 bytes | 1 wide character |
No comments:
Post a Comment