CPP Week 1 Handout Sol
CPP Week 1 Handout Sol
C++
Vahan
Hovhannisyan
Introduction
Vahan Hovhannisyan
12/Jan/2017
Introduction to
Introduction to C++ C++
Vahan
Hovhannisyan
Introduction
Fundamentals
Vahan
Hovhannisyan
Introduction
Fundamentals
I Fundamentals Summary
I Functions
I Arrays
I Pointers
I Object Oriented Programming
Introduction to
Teachers C++
Vahan
Hovhannisyan
Introduction
Fundamentals
Vahan
Hovhannisyan
Introduction
Fundamentals
Introduction
Summary
Fundamentals
Introduction to
What is C++? C++
Motivation Vahan
Hovhannisyan
Introduction
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
Fundamentals
I .cpp extension for source files with C++ code Summary
Vahan
Hovhannisyan
Introduction
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
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
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
Vahan
Hovhannisyan
Introduction
Vahan
Hovhannisyan
Introduction
{
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>
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
Vahan
Hovhannisyan
Introduction
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
Fundamentals
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++
Introduction
Introduction
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
Vahan
Hovhannisyan
Introduction
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
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
Vahan
Hovhannisyan
Introduction
Fundamentals
Vahan
Hovhannisyan
Introduction
Vahan
Hovhannisyan
Introduction
Fundamentals
Vahan
Hovhannisyan
Introduction
Fundamentals
type size range digits Summary
Vahan
Hovhannisyan
Introduction
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
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
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
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
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
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
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
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
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
Vahan
Hovhannisyan
Introduction
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
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
Fundamentals
Summary
I Blocks and scope
I Decision making
I Loops