CPP Week 1
CPP Week 1
C++
Vahan
Hovhannisyan
Introduction
Tutorial
Week 1 - Fundamentals
Summary
Vahan Hovhannisyan
Introduction to
Introduction to C++ C++
Vahan
Hovhannisyan
Introduction
Fundamentals
I Your first C++ course
Tutorial
I To prepare for Quantitative Finance with C++ Summary
Vahan
Hovhannisyan
Introduction
Fundamentals
I Fundamentals Tutorial
I Functions Summary
I Arrays
I Pointers
I Object Oriented Programming
Introduction to
Teachers C++
Vahan
Hovhannisyan
Introduction
Fundamentals
Summary
I PhD from Department of Computing
I Research Director at nPlan
I [email protected]
Introduction to
This Week C++
Vahan
Hovhannisyan
Introduction
Tutorial
Summary
Your First C++ program
Fundamentals
Tutorial
Introduction to
What is C++? C++
Motivation Vahan
Hovhannisyan
Introduction
Fundamentals
Programming)
Tutorial
I C++ was first released in 1985 and is still hugely popular
Summary
I Produces extremely efficient programs
I Has both high and low level features
I Has huge code base
I Constantly evolves (last release was in December 2020)
I It is a language of choice for high performance
applications, including in finance
I Many other popular languages (C, Java, C#) have very
similar syntax to C++
Introduction to
Technical Remarks C++
Compiling Vahan
Hovhannisyan
Introduction
Fundamentals
Tutorial
I Unlike scripting languages such as Python and MatLab, Summary
C++ code must be compiled before we can run it
I .cpp extension for source files with C++ code
I Compile the code into a binary file
I Execute the binary file
Introduction to
Tools C++
Vahan
Hovhannisyan
Introduction
Fundamentals
I We will use Notepad++ to write C++ code.
Tutorial
I It can be downloaded from the Software Hub or directly Summary
from https://notepad-plus-plus.org
I We will use a terminal to compile and execute C++
programmes. On Windows machines we will use MinGW
2.3.2 available from Software Hub. On Mac we will
use its built-in Terminal.
Introduction to
Hello World! C++
Vahan
Hovhannisyan
Introduction
We start with the simplest possible program. Your First C++
program
Create a new file in Nontepad++ with the following content
Fundamentals
// Your f i r s t C++ code Tutorial
#i n c l u d e <i o s t r e a m > Summary
u s i n g namespace s t d ;
i n t main ( )
{
c o u t << ” H e l l o World ! ” << e n d l ;
return 0;
}
Save the flie as hello.cpp in your home directory
Introduction to
Compile and run your code C++
Vahan
Hovhannisyan
Introduction
I In the Terminal (or MinGW) navigate to your home
Your First C++
directory: program
Tutorial
I Compile the code with:
Summary
g++ -o hello hello.cpp
I and if there are no error messages, you can run it with:
./hello
I If you encounter File not found errors, make sure the
terminal is navigated to the folder containing the
hello.cpp file by running
ls -ls
and checking that the file is in the list
Introduction to
Breakdown of Hello World C++
Vahan
Hovhannisyan
Introduction
Vahan
Hovhannisyan
Introduction
I int main() - the main function of the program Your First C++
program
I function with return type int
Fundamentals
I name main (fixed and unique)
Tutorial
I parameters in () and
Summary
I the program goes in {}
I cout - console output
I << - insertion operator (always follows cout)
I output string in ""
I another << to separate outputs and
I endl - output manipulator (add a new line)
I return 0; - return value, end of function.
Up to two return statements are allowed.
Introduction to
Playing with the Code C++
Vahan
Hovhannisyan
Introduction
Vahan
Hovhannisyan
Introduction
Tutorial
I #include <iostream> - add an external library
I using namespace std; - declare that we will be using Summary
Vahan
Hovhannisyan
Introduction
{ Summary
First statement;
...
Last statement;
return 0;
}
Introduction to
Math Example C++
Vahan
Hovhannisyan
Read in a real number and print out its
Introduction
1. square root and
Your First C++
2. cosine program
Fundamentals
#i n c l u d e <cmath> Summary
u s i n g namespace s t d ;
i n t main ( )
{
d o u b l e num ;
c i n >> num ;
c o u t << s q r t (num) << ”\ t ”
<< c o s (num) << e n d l ;
return 0;
}
Introduction to
Breakdown of the Math Example C++
Vahan
Hovhannisyan
Introduction
together)
I int and double - declaring integer and real variables
I sqrt and cos from the cmath libarary
I Running the program
I Input using new line or spaces
Introduction to
Basic Operations C++
Vahan
Hovhannisyan
Introduction
I Operators: +, *, -, =, == Fundamentals
Tutorial
I Operator priorities: * and / before + and -
Summary
I Formatting and aligning the code
- White space/alignment and comments are very
important for the readability of the code, but are not
necessary. Except for #include and strings
I Choose meaningful names
Introduction to
Tutorial: Quadratic Equation C++
Vahan
Hovhannisyan
Introduction
Fundamentals
Summary
ax 2 + bx + c = 0,
assuming it has real roots.
Test on x 2 − 2x + 1 = 0.
Introduction to
Identifiers C++
Introduction
Introduction
Fundamentals
Tutorial
I Decision Making - if else, switch case default
Summary
I Loops - for, while, break, continue
I return - immediate return from function
I Dynamic memory allocation - new, delete
I Object oriented keywords - class, private, public...
Introduction to
Identifiers C++
Other Vahan
Hovhannisyan
Introduction
Summary
I Library identifiers - e.g. cin, cout, abs... Should not
be overwritten by the programmer unless there is a very
good reason to do so
I Programmer supplied identifiers: names of variables,
functions, classes etc
I Case sensitive, consists of letters, numbers and ,
starting with a letter or , can’t use reserved words
Introduction to
Tutorial: Variable Names C++
Vahan
Hovhannisyan
Introduction
Tutorial
2. int ROOT;
Summary
3. int rOOT6;
4. int 44root;
5. int 44root;
6. int +r00t;
7. int root-11;
8. int root ;
Introduction to
Integer Types C++
Vahan
Hovhannisyan
Introduction
Vahan
Hovhannisyan
Introduction
Fundamentals
Vahan
Hovhannisyan
Introduction
Summary
cout << 9/2 << endl;
and
cout << 9.0/2 << endl;
I Examples
I 10 % 3 is 1
I abs(-4) is 4
Introduction to
Real Numbers C++
Vahan
Hovhannisyan
Introduction
Fundamentals
Vahan
Hovhannisyan
Introduction
Fundamentals
type size range digits Tutorial
Vahan
Hovhannisyan
Introduction
Fundamentals
Type casting to solve the problem with integer division
Tutorial
I answer = double(numerator) / denominator; Summary
I do NOT use
answer = double(numerator / denominator);
Example: int(8.799) gives 8
Example: double(9)/2 gives 4.5
Introduction to
Constants C++
Vahan
Hovhannisyan
Introduction
Fundamentals
I Constants cannot be changed after initialization Tutorial
I Example Summary
I const float pi = 3.14159;
I The <cmath> library has a constant M PI
I In general use
const <type> name;
Introduction to
Strings C++
Vahan
Hovhannisyan
Introduction
I The string type can be using just like any other type
to create variables after including the <string>
standard library
I Exercise: Replace the text in the Hello World! example
with a variable
Introduction to
Some More <cmath> Functions C++
Vahan
Hovhannisyan
Introduction
Fundamentals
Tutorial
I The <cmath> library contains many mathematical Summary
functions, such as
I cos acos, sqrt, abs, etc
I const float pi = atan(1.0);
Introduction to
Tutorial: Triangle C++
Vahan
Hovhannisyan
Introduction
Fundamentals
Write C++ code to calculate the
Tutorial
1. hypotenuse of a right triangle given: Summary
Vahan
Hovhannisyan
Introduction
Fundamentals
Tutorial
I Introduction to C++ Summary
Vahan
Hovhannisyan
Introduction
Fundamentals
Tutorial
I Tutorial Summary