SMM303 C++
Programming in Finance
Lecture 1
C++ programming language is
one of the most popular general-purpose computer programming languages. C++ is
a high level programming language.
The further a programming
language is from the computer architecture, the higher the language's level.
Therefore lowest-level languages are machine languages that computers
understand directly. High-level programming languages are therefore close to the
human languages.
High-level programming
languages, such as C++, have the following advantages:
·
Readability: C++ programs are easy to read.
·
Maintainability: C++ programs can be
maintained very easily (essentially they are text files).
·
Portability: C++ programs can easily made
portable across different computer platforms.
The C++ language's
readability and maintainability benefit directly from its relative closeness to
human languages, especially English.
The process of writing a C++
program involves
Your
first C++ program
Let’s look at the following
piece of code.
/* My
first C++ program
*/
// This
line can contain a comment.
#include
<iostream.h>
void main
() {
cout << "Hello World \n";
}
This is probably one of the
simplest C++ programs. Lets proceed line by line to evaluate the above program.
It is not hard to guess that
the first two lines form comments within the program. Notice that the comments
are enclosed by /* and */. In C, /* is
called the opening comment mark, and */ is the closing comment mark. The C
compiler ignores everything between the opening comment mark and closing
comment mark. The fourth line is also a
comment but starts with two slashes (//) to mark the beginning of a comment
line. The // can be used to comment a single line within a program. For example the following two way of
commenting are identical
/*
The purpose of commenting a
program is to help you to document specific sections of the program. Comments
are especially helpful if you are reading someone else’s program.
*/
// The purpose of commenting
a program is to help you to document specific sections
// of the program. Comments
are especially helpful if you are reading someone else’s
// program.
#include
directive
Let's now move to line with
#include <iostream.h>
We note that that this line
starts with a hash sign, #, which is followed by include. In C++, #include
forms a pre-processor directive that tells the pre-processor to look for a file
and place the contents of the file in the location where the #include directive
indicates. The pre-processor is a program that does some preparations for the
compiler before your code is compiled. Also in this line, you see that
<iostream.h> follows #include. You may guess that the file the #include
directive asks for is something called <iostream.h>. You are exactly
right! Here, the #include directive does ask the pre-processor to look for and
place <iostream.h> where the directive is in the program.
The name of the
<iostream.h> file stands for standard input-output header file. The
<iostream.h> file contains numerous prototypes and macros to perform
input or output (I/O) for the programs. Besides stdio.h, there are more header
files, such as cstring.h and math.h. We will learn more about these as we
proceed.
The
main() function
This is a very special function in C++. Every C++ program must have a main() function, and every C++ program can only have one main() function. You can put the main() function wherever you want in your C++ program. However, the execution of your program always starts with the main() function. Note the main function is enclosed by { }. Because the above program is a very simple program, the main() function is the only function defined in the program.
cout function
Within the main() function body, a library function, cout, is called in order to print out a greeting message. Note cout << "Hello World \n"; ends with a ;. The cout function is a prototype within the header file iostream.h
The
newline character (\n)
In the cout function, one
thing worth mentioning at this stage is the newline character, \n. Usually
suffixed at the end of a message, the newline character tells the computer to
generate a carriage-return and line-feed sequence so that anything printed out
after the message will start on the next new line on the screen.
You may already be anxious
to know how an executable file is made. Let's have a look at how a C++ program
is compiled and translated into an executable file (binary code). We will use Visual C++ programming
environment for this.
When you finish writing a C/C++ program and start to compile it, you might get some error or warning messages. Don't panic when you see error messages. With a bit of experience in writing programs and with the help of from the debugger within Visual C++ environment you will be able to rectify them.
In programming we often require temporary storage of
data. The data is stored in a variable, which is a temporary storage in the
computer's memory. The variable name is the identifier used to identify the
data. Variables are made up of a sequence of characters that denotes the name
of the variable to be used. We can use letters (a-z and A-Z), digits (0-9),
and underscores. All variables must start with letter or an underscore.
Variable declarations are usually made at the start of the function before any
statements. Note: C++ is case sensitive,
e.g. Main() and main() are two different functions.
char
character
int
an integer (whole numbers. i.e.
.., -3, -2, -1, 0, 1, 2, 3,..)
float
single precision floating point number (numbers with decimals)
double
double precision floating point number (double the size of float)
examples:
char first_letter_of_my_name;
int num;
int __my_phone_number__;
float me;
double StRaNgE;
All functions in C++ can return values. For instance, when you make a function to add two numbers, you can make such a function that returns to you the value of the addition. If we do not want a function to return a value we will have to explicitly say so, using the void at the beginning of the function.
void main
() {
cout << "Hello World \n";
}
On the other hand we could ask the function main to return an integer value. To do this we modify the main function as
int main
() {
cout << "Hello World \n";
return 0;
}
Note int main() specify that
the main function is returning an integer and return 0; specify that the main
is returning the integer value 0. Also
note carefully the use of; at the end of statements (something we will have to
bear in mind all the time).
So the general format of the C++ function is as follows:

double
function1 (int x, double, y, char z) {
int
a;
double
b;
//
code for function1 will go in here
return b;
}
A typical C++ program will
then be:
//1. Include
the necessary header files e.g.
#include
<iostream.h>
//2.
functions e.g.
void
function1 () {
// code
for function1 will go in here
}
void
function2 () {
// code
for function2 will go in here
}
//3.
finally we need to have the main function which can call the other //functions.
i.e.
void main
() {
function1();
function2();
}