C++ Programming (Elementary)
Deepak Sharma
Assistant Professor
BSBE, IIT Roorkee
Getting started
One needs:
Text editor – create/edit files eg. vi editor
Compiler – translates programs into machine
language
(binary code)
“Hello, World” Program
Standard header
Preprocessor directive; Standard
mainC++
Where
Data – the
function;
type onebegins
-program
lib and only one
integer Output operator
Standard output
Program body
stream
“Hello, World” Program – Take 2
Characters and Literals
newline character
\n
horizontal tab \t
print special characters \”
Variables and Constants
const int m = 44;
Initialize
Input operator
C++ Programming (Data Types)
Deepak Sharma
Assistant Professor
BSBE, IIT Roorkee
Data Types
• Integers
– 11 types
– int, char and bool
• Floating point numbers
– 3 types
– float, double (greater precision than float),
long double
Boolean variable
Arithmetic Operators
modulus/remainder
operator
Floating-Point Arithmetic
Composite Assignment
Operators
sizeof Operator
Type Conversion
Integer Overflow
Floating-point Overflow
Round-off Error
C++ Programming (Conditions)
Deepak Sharma
Assistant Professor
BSBE, IIT Roorkee
if Statement
if..else Statement
Keywords
Comparison Operators
Standard Input & Common
Error
Find Minimum of 3 Integers
Statement Blocks
Compound Conditions & Short-
circuiting
else..if Statement
Conditional Expression
Operator
C++ Programming (Loops)
Deepak Sharma
Assistant Professor
BSBE, IIT Roorkee
while Loop
do..while Loop
for Loop
Terminating a Loop
Terminating a Program
continue Statement
Descending for Loop
Pseudo-random Numbers
Random Numbers
C++ Programming (Functions)
Deepak Sharma
Assistant Professor
BSBE, IIT Roorkee
Standard C++ Library
Functions
Standard C++ library functions is a collection of pre-
defined functions which are accessed through header
files
rand () function defined in <cstdlib>
time () function defined in <ctime>
sqrt() Function
Trignometric Functions
Common Functions in <cmath>
Header
Functions Description Example
ceil(x) ceiling of x (rounds up) ceil(3.14) returns 4.0
floor(x) floor of x (rounds down) floor(3.14) returns 3.0
fabs(x) absolute value of x fabs(-2) returns 2.0
exp(x) exponential of x (base e) exp(2) returns 7.38906
log(x) natural log of x (base e) log(2) returns 0.693147
log10(x) common log of x (base 10) log10(2) returns 0.30103
sin(x) sin of x (in radians) sin(2) returns 0.909297
cos(x) cos of x (in radians) cos(2) returns -0.416147
tan(x) tan of x (in radians) tan(2) returns -2.18504
pow(x,p) x to the power p pow(2,3) returns 8.0
sqrt(x) square root of x sqrt(2) returns 1.41421
User-defined Functions & Test drivers
head
body
Function Declarations & Definitions
declaration
definition
Separate Function Compilation
void Function & Passing by
Reference
Passing by Value vs. Passing by
Reference
Passing By Value Passing By Reference
int x; int& x;
Parameter x is a local variable Parameter x is a local reference
Duplicate of argument Synonym of argument
Cannot change the argument Can change the argument
Argument may be a constant, a Argument must be a variable
variable or an expression
Argument is read-only Argument is read-write
C++ Programming (Arrays)
Deepak Sharma
Assistant Professor
BSBE, IIT Roorkee
Arrays
• An ordinary variable of the primitive type is
called a scalar object
• An array is a composite object composed of
several elements of the same type
Array
Print in Reverse Order using
Array
Initializing an Array
Initializing an Array with Trailing
Zeros
Uninitilized Array
Passing an Array to a Function
Multidimensional Array
Thank You !
while loop
do-while loop