Lecture 02
Lecture 02
● Use of streams
● Use of namespace
3
First example program
Example - 01
#include<iostream>
#include<stdio.h>
● Reading int from standard input
int main(int argc, char *argv[])
● A C++ program that uses printf and {
scanf statements int v1;
scanf("%d", &v1);
● stdio.h is part of standard C library Printf("%d\n", v1);
}
● It provides functions for performing
input and output operations. Compiling
$ gcc hello.c -g
● printf, scanf, putchar, getchar
Executing
$ ./a.out
5
Example - 02
#include<iostream>
● Formatting specification rests on user #include<stdio.h>
6
Example - 03
#include<iostream>
● Errors cannot be handled by these functions #include<stdio.h>
7
Example - 04
#include<iostream>
using namespace std;
● iostream is part of the C++ standard
int main(int argc, char *argv[])
library.
{
int v1;
● It provides stream classes for cin >> v1;
performing input and output operations count << v1;
using an object-oriented approach. }
9
Example - 06
#include<iostream>
// without using namespace
● Without using namespace std and
int main(int argc, char *argv[])
{
● Without std::cin or
int v1;
cin >> v1;
● std::cout
count << v1;
}
● Statements results in error
10
Example - 07
#include<iostream>
0 0 0 0 0 0 0 1
128 64 32 16 8 4 2 1
1 << 3
0 0 0 0 1 0 0 0
128 64 32 16 8 4 2 1
11
Example - 08
● The bitwise operators <<, >> when used with such as cin >> v1
12
Example - 09
● 10
● 20
● 30 .a.out < input.txt
Indirection operator
13
Data Types
Overview
● Basic data types ● Basic data types - char, int, float, double, void, bool
● Identifier names
● Variables
● Variable initialization
● Operators
● Expressions
15
Overview
16
Identifiers & Variables
Overview
● Basic data types ● The names of variables, functions, labels, and user-
defined objects are called identifiers.
● Modifying basic data types
● Name can be one or more characters long
● Identifier names
● First character must be from the set: {a, …, z, A, …,
● Variables
Z, _}
● Variable initialization
● Cannot have spaces or special characters like $, !,
● Operators #, %, :, ::, -, -> etc.
● Expressions
18
Overview
19
Overview
20
Overview
● Basic data types ● Local variables are declared inside a function and can
only be accessed within that function
● Modifying basic data types
21
Overview
● Static variables: retain their value between function
calls. They are initialized only once.
● Basic data types
#include<iostream>
● Modifying basic data types
using namespace std;
void example()
● Identifier names {
static int v1 = 0;
● Variables
v1 = v1 + 1;
cout << v1;
● Variable initialization
}
● Operators
int main(int argc, char *argv[])
{
● Expressions
example();
example();
example();
22
}
Overview
● Static global variables: global variables which are
static declared outside function.
● Basic data types
#include<iostream>
● Modifying basic data types
using namespace std;
void example()
● Identifier names {
static int v1 = 0;
● Variables
v1 = v1 + 1;
cout << v1;
● Variable initialization
}
● Operators
int main(int argc, char *argv[])
{
● Expressions
example();
example();
example();
23
}
Overview
● Static global variables: global variables which are
static declared outside function.
● Basic data types
#include<iostream>
● Modifying basic data types
using namespace std;
static int v1 = 0;
● Identifier names
void example01()
● Variables
{
v1 = v1 + 1;
● Variable initialization
cout << v1;
}
void example02()
● Operators
{
v1 = v1 + 1;
● Expressions
cout << v1;
}
24
Overview
● Static global variables: global variables which are
static declared outside function.
● Basic data types
int main(int argc, char *argv[])
● Modifying basic data types
{
example01();
● Identifier names example01();
cout << v1 << endl;
● Variables
example02();
example02()
● Variable initialization
cout << v1 << endl;
}
● Operators
v1: 4
● Expressions
25
Overview
● Static global variables: global variables which are
static declared outside function.
● Basic data types
Declaration Inside function Outside function
● Modifying basic data types
Scope Within function Within current file
● Identifier names Reinitialization Only once Only once
● Variables
● Variable initialization
● Operators
● Expressions
26
Overview
● Function parameters: treated as local variables
● Operators
● Expressions
27
Overview
● Constant variables
● Variables
● Variable initialization
const int max_marks = 100;
● Operators
● Expressions
28
Overview
● External variable
● File1.cc -- counter
● Basic data types
● File2.cc -- variable counter in file1.cc is reused by stating
● Modifying basic data types ● extern int counter;
● Identifier names
● Variables
● Variable initialization
● Operators
● Expressions
29
Overview
● External variable
● File1.cc -- counter
● Basic data types
● File2.cc -- variable counter in file1.cc is reused by stating
● Modifying basic data types ● extern int counter;
● Identifier names
● Variables
● Variable initialization
● Operators
● Expressions
30
Overview
● External variable
● File1.cc -- counter
● Basic data types
● File2.cc -- variable counter in file1.cc is reused by stating
● Modifying basic data types ● extern int counter;
● Identifier names
● Variables
● Variable initialization
● Operators
● Expressions
● Variable initialization
i2 = i1 * 10 + example(p1);//expression
● Operators
● Expressions
32
Overview
● Local variable initialization methods
● Variable initialization
We will understand first two when
discussing constructors.
● Operators
Last one prevents narrowing datatypes
● Expressions
int c{3.14}; This is not allowed.
33
Operators
Overview
● Arithmetic operators
● Identifier names
● Variables
● Variable initialization
● Operators
● Expressions
35
Overview
● Relational operators
● Identifier names
● Variables
● Variable initialization
● Operators
● Expressions
36
Overview
● Logical operators
● Variables
● Variable initialization
● Operators
● Expressions
37
Overview
● Bitwise operators
● Variable initialization
● Operators
● Expressions
38
Overview
● Assignment operators
39
Overview
● Conditional operator
● Variables
● Variable initialization
● Operators
● Expressions
40
Overview
● Increment/decrement operator
● Variables
● Variable initialization
● Operators
● Expressions
41
Overview
● scope resolution operator
● Identifier names
● Variables
● Variable initialization
● Operators
● Expressions
42
Overview
● Member access operators
● Variable initialization
● Operators
● Expressions
43
Overview
● Pointer operators
#include<iostream>
● Variables
use namespace std;
● Identifier names
● Variables
● Variable initialization
● Operators
● Expressions
45
Expressions
Overview
● Arithmetic expressions
#include <iostream>
● Basic data types
int main(int argc, char *argv[])
● Modifying basic data types {
int a = 10;
int b = 5;
● Identifier names
int sum = a + b;
int difference = a - b;
● Variables int product = a * b;
int quotient = a / b;
● Variable initialization int remainder = a % b;
return 0;
} 47
Overview
● Relational expressions
#include <iostream>
● Basic data types
int main(int argc, char *argv[])
{
● Modifying basic data types int a = 10;
int b = 5;
return 0;
} 48
Overview
● Logical expressions
● Expressions return 0;
}
49
Overview
● Bitwise expressions
#include <iostream>
● Basic data types
int main(int argc, char *argv[])
{
● Modifying basic data types int a = 6; // 0110
int b = 3; // 0011
return 0;
50
}
Overview
● Assignment expressions
#include <iostream>
● Basic data types
int main(int argc, char *argv[])
{
● Modifying basic data types int a = 10;
int b = 5;
● Identifier names a += b;
std::cout << "a += b: " << a << std::endl;
a -= b;
● Variables
std::cout << "a -= b: " << a << std::endl;
● Variable initialization a *= b;
std::cout << "a *= b: " << a << std::endl;
● Operators a /= b;
std::cout << "a /= b: " << a << std::endl;
● Expressions
a %= b;
std::cout << "a %= b: " << a << std::endl;
return 0;
51
}
Overview
● Conditional expressions
#include <iostream>
● Basic data types using namespace std;
● Operators
return 0;
}
● Expressions
52
Overview
● Scope resolution expressions
#include <iostream>
● Basic data types using namespace std;
● Operators return 0;
}
● Expressions
53
Conditional statements, loops
Overview
● If statement
#include <iostream>
● Conditional statements using namespace std;
return 0;
} 55
Overview ● Nested if statements
#include <iostream>
using namespace std;
return 0;
56
}
Overview ● Switch statement
#include <iostream>
return 0;
}
58
Overview
● While loop
● Conditional statements
return 0;
}
59
Overview
● Do-While loop
● Conditional statements
return 0;
}
60
Jump statements
Overview ● Jump statements
#include <iostream>
● Conditional statements using namespace std;
return 0;
}
62
Overview
● Jump statements
● Conditional statements
#include <iostream>
● Loop structures using namespace std;
return 0;
}
63
Block structure statements
Overview
● Block statements
● Conditional statements
#include <iostream>
Using namespace std;
● Loop structures
int main(int argc, char *argv[])
{
● Jump statements int number = 10;
// new block
● Block statements {
int iNumber = 5;
cout << "inner: " << iNumber << endl;
}
return 0;
}
65
Arrays
Overview ● Static one dimensional arrays
#include <iostream>
67
Overview ● Declare & initialize
#include <iostream>
return 0;
● Default initialization }
68
Overview ● Partial initialization
#include <iostream>
return 0;
● Default initialization }
69
Overview ● Default initialization
#include <iostream>
return 0;
● Default initialization }
70
Overview ● Dynamic array declaration
#include <iostream>
return 0;
}
71
Overview ● Dynamic array declaration
#include <iostream>
72
Overview
73
Overview
74
Overview
● delete calls destructors for class ● free does not call destructors.
objects.
75
Overview
● Using vector
● We will discuss these two methods after the theme "class" and "template"
76
Two dimensional Arrays
Overview ● Static two dimensional arrays
● Partial initialize
78
Overview ● Static 2D arrays – declare & initialize
● Partial initialize
79
Overview ● Partial initialization of 2D arrays
● Partial initialize
80
Overview ● Dynamic 2D arrays
● Partial initialize
81
Pointers
Pointers
● Pointer Assignment
● Pointer Arithmetic
● Pointers to pointers
83
Pointers
● Pointer Assignment
● Pointer Arithmetic
● Pointers to pointers
84
Pointers
● Pointer Assignment
● Pointer Arithmetic
● Pointers to pointers
85
Pointers
● Pointer Assignment
● Pointer Arithmetic
● Pointers to pointers
86
Pointers
● Pointer Assignment
● Pointer Arithmetic
● Pointers to pointers
87
Pointers
● Pointer Assignment
● Pointer Arithmetic
● Pointers to pointers
88
Passing Data Using A Pointer
Passing Data Using A Pointer
● Primary reason for passing data using pointer is to allow the function to modify
the data
90
Passing Data Using A Pointer
91
Passing Data Using A Pointer
92
Passing By Value
Passing Data By Value
● If pointers are not passed, the values will not get swapped.
● Example
94
Passing Data By Value
95
Passing Data By Value
96
Passing A Pointer To A Constant
Passing A Pointer To A Constant
● If this behavior is not desirable, then passing a pointer to a constant is the solution
98
Passing A Pointer To A Constant
99
Passing A Pointer To A Constant
will result in compilation error Void function_01(const int *num1, int *num2)
as read only constant pointer {
*num1 = 200;
is modified *num2 = 200;
}
● A type mis-match also result in
compilation error as shown in const int limit = 100;
the example.
function_01(&limit, &limit);
function_01(&23, &23);
100
Returning A Pointer
Returning A Pointer
● Allocate memory within the function using malloc and return its address. The caller
is responsible for de-allocate the returned memory
● Pass an object to the function where it is modified. This makes allocation and de-
allocation the responsibility of the caller.
102
Returning A Pointer
103
Returning A Pointer
104
Returning A Pointer
105
Returning A Pointer
106
Returning A Pointer
107
Returning A Pointer
108
Pointer To A Pointer
Passing A Pointer To A Pointer
● If we want to modify the original pointer and not the copy of the pointer, we need
to pass it as pointer to a pointer.
110
Passing A Pointer To A Pointer
● The function will return void allocate_array(int **arr, int size, int value)
{
allocated memory through first *arr = (int *) malloc(size * sizeof(int));
parameter for(int i = 0; i < size; i++)
{
*arr[i] = value;
● To modify the pointer "vector", }
return arr;
we need to pass vector's }
address
111
Passing A Pointer To A Pointer
112