0% found this document useful (0 votes)
30 views

CPP Week 1 Handout Sol

finance C++

Uploaded by

edison6685
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

CPP Week 1 Handout Sol

finance C++

Uploaded by

edison6685
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Introduction to

C++

Vahan
Hovhannisyan

Introduction

Your First C++


program
Introduction to C++ Fundamentals

Week 1 - Fundamentals Summary

Vahan Hovhannisyan

12/Jan/2017
Introduction to
Introduction to C++ C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals

I Your first C++ course Summary

I To prepare for Quantitative Finance with C++


I Tutorials will be integrated into lectures
I Bjarne Stroustrup ’The C++ programming language’ -
for reference
Introduction to
Course Syllabus C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals
I Fundamentals Summary

I Functions
I Arrays
I Pointers
I Object Oriented Programming
Introduction to
Teachers C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals

Teaching team: Summary

I Vahan Hovhannisyan and


I Juan Campos Salazar
both from the Department of Computing
Introduction to
This Week C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals
Introduction
Summary

Your First C++ program

Fundamentals
Introduction to
What is C++? C++

Motivation Vahan
Hovhannisyan

Introduction

Your First C++


I C++ is based on C with extra features (Object Oriented program

Fundamentals
Programming)
Summary
I C++ was first released in 1983 and is still hugely popular
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 2014)
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

Your First C++


program

Fundamentals
I .cpp extension for source files with C++ code Summary

I Compile the code into a binary source code


I Locate and fix errors
I Object module
I Program is linked
I Find objects and functions from external libraries
I Gives a complete machine executable program
Introduction to
Breakdown of C++ Code C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals

Summary
I Keyboard→ cin → code (”assign an integer value to a
variable”)→ cout → screen
I cin and cout are objects from the <iostream> library
- contains objects performing input/output operations
Introduction to
Hello World! C++

Vahan
We start with the simplest possible program Hovhannisyan

I Create a new file and write the code (copy) Introduction

Your First C++


// Your f i r s t C++ code program

#i n c l u d e <i o s t r e a m > Fundamentals

u s i n g namespace s t d ; Summary

i n t main ( )
{
c o u t << ” H e l l o World ! ” << e n d l ;
return 0;
}
I Use Cygwin to compile:
cd /cygdrive/h
g++ -o hello hello.cpp
I and run:
./hello
Introduction to
Breakdown of Hello World C++

Vahan
Hovhannisyan

Introduction

Your First C++


program
I Comments are ignored, but are very useful Fundamentals
I // one line comment Summary
I /* multi-line
comment */
I #include <iostream> - links the library to our code
I preprocessing is in red
I using namespace std; - groups all standard libraries
together
I reserved words are in blue
Introduction to
Breakdown of Hello World C++

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)
Summary
I parameters in () and
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

Your First C++


program
I Empty int main() {} will compile, but do nothing
Fundamentals
I Try without endl or with "\n" instead Summary

I Output multiple messages using <<


c o u t << ” H e l l o ” << ” World ! ”
<< ” What ’ s up ?\ n” ;
I Try other escape sequences
I ”\n” - new line
I ”\t” - horizontal tab
Introduction to
Summary of the Example C++

Vahan
Hovhannisyan

Introduction

Your First C++


program
I #include and using are preprocessing derivatives
Fundamentals
I #include <iostream> - add an external library
Summary
I using namespace std; - declare that we will be using
objects and functions from a particular group
I cout<<"string"<<endl; - prints out on the screen
I return 0; could be omitted by using
void main() {statements;}
without return 0, but is not encouraged anymore
I Exactly one main() function is allowed
Introduction to
Structure of a C++ Program C++

Vahan
Hovhannisyan

Introduction

Your First C++


program
preprocessing Fundamentals

int main() 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 <i o s t r e a m > Summary

#i n c l u d e <cmath>
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

Your First C++


program
I #include <cmath> - library with mathematical Fundamentals
functions Summary

I cin >> - input and extraction operators (always go


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

Your First C++


program

I Operators: +, *, -, =, == Fundamentals

Summary
I Operator priorities: * and / before + and -
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

Your First C++


program

Fundamentals

Write a program that solves a given quadratic equation Summary

ax 2 + bx + c = 0,
assuming it has real roots.
Test on x 2 − 2x + 1 = 0.
Introduction to
Tutorial: Quadratic Equation C++

Solution Vahan
Hovhannisyan

Introduction
#i n c l u d e <i o s t r e a m > Your First C++
program
#i n c l u d e <cmath>
Fundamentals
u s i n g namespace s t d ;
Summary
i n t main ( )
{
double a , b , c , root1 , root2 ;
c i n >> a >> b >> c ;
d o u b l e d i s c r i m i n a n t = b∗b − 4∗ a ∗ c ;
r o o t 1 = (−b+s q r t ( d i s c r i m i n a n t ) ) / ( 2 ∗ a ) ;
r o o t 2 = (−b−s q r t ( d i s c r i m i n a n t ) ) / ( 2 ∗ a ) ;
c o u t << r o o t 1 << ”\ t ” << r o o t 2 << e n d l ;
return 0;
}
Introduction to
Identifiers C++

Data Types Vahan


Hovhannisyan

Introduction

Your First C++


program
I Data types: Fundamentals
I void - means ”nothing” or ”no type” Summary
I char - integer to represent one character
I short, int, long - integer variables of various sizes
I enum - enumerations
I float, double - real numbers
I Type modifiers:
I unsigned - restricts a particular integer type to only
non-negative variables
I const - the value of the variable cannot be changed
Introduction to
Identifiers C++

Reserved words Vahan


Hovhannisyan

Introduction

Your First C++


program

Fundamentals

Summary
I Decision Making - if else, switch case default
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

Your First C++


program
I sizeof - get the size of a type or variable during Fundamentals

compilation time 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

Which of the following declarations are legal? Your First C++


program

1. int root; Fundamentals

Summary
2. int ROOT;
3. int rOOT6;
4. int 44root;
5. int 44root;
6. int +r00t;
7. int root-11;
8. int root ;
Introduction to
Tutorial: Variable Names C++

Solution Vahan
Hovhannisyan

Introduction

Your First C++


Which of the following declarations are legal? program

Fundamentals
1. int root; // legal
Summary
2. int ROOT; // legal
3. int rOOT6; // legal
4. int 44root; // ILLEGAL
5. int 44root; // legal
6. int +r00t; // ILLEGAL
7. int root-11; // ILLEGAL
8. int root ; // legal
Introduction to
Integer Types C++

Vahan
Hovhannisyan

Introduction

Your First C++


program
I Set space in memory for a particular type, example: Fundamentals
unsigned int m, n; - reserves memory for two Summary
non-negative integers
I There are certain rules to follow:
1. Decimal points cannot be used - 5.6 is not an integer
2. Commas cannot be used - 12, 345 must be written as
12345
3. Cannot start with a leading 0 - 011 will be interpreted
as a base eight number with value 9
Introduction to
Integer Types C++

Vahan
Hovhannisyan

Introduction

Your First C++


I Some of integer types in C++: program

Fundamentals

type bytes range Summary

char 1 -128 to 127


short 2 -32768 to 32767
int 4 -2,147,483,648 to 2,147,483,647
long 4 -2,147,483,648 to 2,147,483,647
unsigned short 2 0 to 65,535
I Varies depending on the particular architecture (32-bit
and 64-bit). Use sizeof() to check.
Introduction to
Operators C++

Vahan
Hovhannisyan

Introduction

Your First C++


program
I +, -, *, /, % Fundamentals

I Be careful when applying / on integers, try 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

Your First C++


program

Fundamentals

I Can use . or e to represent real numbers Summary

Example: 3.14 or −2.5e3


I There are three types for real numbers:
float, double and long double
I Can’t use unsigned with real numbers
Introduction to
Real Numbers C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals
type size range digits Summary

float 4 +/ − 3.4 × 10+/−38 7


double 8 +/ − 1.7 × 10+/−308 15
long double 8 +/ − 1.7 × 10+/−308 15

The size depends on machine architecture, use sizeof() to


check
Introduction to
Type Casting C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals
Type casting to solve the problem with integer division
Summary
I answer = double(numerator) / denominator;
I do NOT use
answer = double(numerator / denominator);
Example: int(8.799) gives 8
Example: double(9)/2 gives 4.5
Introduction to
Characters C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals
I char is an integer, Summary
example: ’x’ == 120, ’5’ == 53
I Uses ASCII code to translate integers into chars
I char is a subset of int
I We can even do arithmetic on char variables:
’9’ - ’0’ == 57 - 48 == 9
Introduction to
Tutorial: Chars and Integers C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals
Find the ASCII code for
Summary
1. ’5’
2. ’H’
3. ’h’
4. ’#’
5. ’-’
Introduction to
Tutorial: Chars and Integers C++

Solution Vahan
Hovhannisyan

Introduction

Your First C++


#i n c l u d e <i o s t r e a m > program
u s i n g namespace s t d ; Fundamentals

i n t main ( ) Summary

{
char character ;
i n t number ;
c i n >> c h a r a c t e r ;
number = c h a r a c t e r ;
c o u t << c h a r a c t e r << ”\ t ”
<< number << e n d l ;
return 0;
}
Introduction to
Constants C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals
I Constants cannot be changed after initialization Summary
I Example
I const float pi = 3.14159;
I The <cmath> library has a constant M PI
I In general use
const <type> name;
Introduction to
Enumerations C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

I Enumerations are a compact way to declare constants Fundamentals

Summary
I Example: enum {MON, TUES, WED, THURS, FRI,
SAT, SUN};
I Default values are 0, 1, 2, ...
I Can give any values: enum {MON=1, TUES, WED=-5,
THURS=0, FRI, SAT, SUN};
I Constant values can be repeated
Introduction to
Boolean Values C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals

Summary
I Basis for decisions and loops
I Used to be integer 1 for True and integer 0 for False,
but could lead to confusions
I Now C++ includes enum bool {false, true}
Introduction to
Some More <cmath> Functions C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals

Summary
I The <cmath> library contains many mathematical
functions, such as
I cos acos, sqrt, abs, etc
I const float pi = atan(1.0);
Introduction to
Tutorial: Triangle C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals
Write C++ code to calculate the
Summary
1. hypotenuse of a right triangle given:
I the other two sides
I one side and its adjacent angle
I its area and one of the angles
2. average length of all sides of the triangle
Introduction to
Tutorial: Triangle C++

Solution Vahan
Hovhannisyan
#i n c l u d e <i o s t r e a m >
Introduction
#i n c l u d e <cmath>
Your First C++
u s i n g namespace s t d ; program

i n t main ( ) Fundamentals

{ Summary

double a , b , c , area , angle ;


c i n >> a >> b ; // p a r t 1
c = s q r t ( a ∗ a + b∗b ) ;
c o u t << c << e n d l ;
c i n >> a >> a l p h a ; // p a r t 2
c = a / sin ( alpha ) ;
c o u t << c << e n d l ;
c i n >> a r e a >> a l p h a ; // p a r t 3
c = s q r t (4 ∗ area / s i n (2 ∗ alpha ) ) ;
c o u t << c << e n d l ;
return 0;
}
Introduction to
Increment/Decrement Operators C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals
I ++, -- increment/decrement the value of a variable
Summary
I Pre-increment and post-increment are different:
int a = 3;
c o u t << ++a ; // p r i n t s o u t 4
c o u t << a++; /∗ p r i n t s o u t 4 ,
but t h e v a l u e o f a i s changed t o 5 ∗/
Introduction to
Summary C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals

Summary
I Introduction to C++
I Your first C++ code
I Variables and arithmetic in C++
Introduction to
Next Week C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals

Summary
I Blocks and scope
I Decision making
I Loops

You might also like