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

CPP Week 1

The document is an introduction to a C++ course that will cover fundamentals. It outlines what will be covered in the first week, including writing a "Hello World" program as the first example. Instructions are provided on how to set up the development environment and compile and run a simple C++ program that outputs "Hello World". Key aspects of the example code like comments, headers, and namespaces are also explained.

Uploaded by

maxi maaeez
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)
10 views

CPP Week 1

The document is an introduction to a C++ course that will cover fundamentals. It outlines what will be covered in the first week, including writing a "Hello World" program as the first example. Instructions are provided on how to set up the development environment and compile and run a simple C++ program that outputs "Hello World". Key aspects of the example code like comments, headers, and namespaces are also explained.

Uploaded by

maxi maaeez
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/ 35

Introduction to

C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Introduction to C++ Fundamentals

Tutorial
Week 1 - Fundamentals
Summary

Vahan Hovhannisyan
Introduction to
Introduction to C++ C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals
I Your first C++ course
Tutorial
I To prepare for Quantitative Finance with C++ Summary

I Tutorials will be integrated into lectures


I Bjarne Stroustrup ’The C++ programming language’ -
as a reference only
I cplusplus.com
Introduction to
Course Syllabus C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals
I Fundamentals Tutorial

I Functions Summary

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

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals

Vahan Hovhannisyan Tutorial

Summary
I PhD from Department of Computing
I Research Director at nPlan
I [email protected]
Introduction to
This Week C++

Vahan
Hovhannisyan

Introduction

Your First C++


program
Introduction
Fundamentals

Tutorial

Summary
Your First C++ program

Fundamentals

Tutorial
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)
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

Your First C++


program

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

Your First C++


program

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

cd H:/Users/COLLEGE USERNAME Fundamentals

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

Your First C++


program
I Comments are ignored, but are very useful Fundamentals
I // one line comment Tutorial
I /* multi-line Summary
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)
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

Your First C++


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

I Output multiple messages using << Summary

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

Tutorial
I #include <iostream> - add an external library
I using namespace std; - declare that we will be using Summary

objects and functions from a particular group


I cout<<"string"<<endl; - prints out on the screen
I return 0; could be omitted by using
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() Tutorial

{ 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 > Tutorial

#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

Your First C++


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

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

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

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

Your First C++


program

Fundamentals

Write a program that solves a given quadratic equation Tutorial

Summary

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

Data Types Vahan


Hovhannisyan

Introduction

Your First C++


program
I Data types: Fundamentals
I bool - Can only represent true or false states Tutorial
I char - integer to represent one character Summary
I short, int, long - integer variables of various sizes
I float, double - real numbers
I void - means ”nothing” or ”no type”
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

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

Your First C++


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

compilation time Tutorial

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

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

Your First C++


program
I Set space in memory for a particular type, example: Fundamentals
unsigned int m, n; - reserves memory for two Tutorial
non-negative integers Summary

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

I Some of integer types in C++: Your First C++


program

Fundamentals

type bytes range Tutorial

char 1 -128 to 127 Summary

short 2 -32768 to 32767


int 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.
I long int and long long int allow larger integers
Introduction to
Operators C++

Vahan
Hovhannisyan

Introduction

Your First C++


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

I Be careful when applying / on integers, try Tutorial

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 Tutorial

Example: 3.14 or −2.5e3 Summary

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 Tutorial

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

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

long double allows larger/more accurate floating numbers


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

Your First C++


program

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

Your First C++


program
I C++ comes with a rich set of compound standard Fundamentals
types, for example, the string class Tutorial

I For example, ”Hello World!” is a string Summary

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

Your First C++


program

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

Your First C++


program

Fundamentals
Write C++ code to calculate the
Tutorial
1. hypotenuse of a right triangle given: Summary

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
Summary C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals

Tutorial
I Introduction to C++ Summary

I Your first C++ code


I Variables and arithmetic in C++
Introduction to
Next Week C++

Vahan
Hovhannisyan

Introduction

Your First C++


program

Fundamentals

Tutorial
I Tutorial Summary

I Blocks and scope


I Decision making

You might also like