Topic 02 Writing First C++ Program
Topic 02 Writing First C++ Program
1
A Small C Program
Below is a small C/C++ program
It consists of one function
int main(void)
{
return 0;
}
2
A Small C Program
A function is a block of codes/instructions.
A C/C++ program consists of one or more functions.
There must be a minimum of one function.
main must be one of the functions.
The main function is where program execution begins.
When a C/C++ program runs, it gives back a value to indicate
whether program execution is successful.
0 (zero) shows successful execution.
Non-zero shows there was some problem.
3
A Small C Program
4
A Small C Program (cont.)
5
A Small C Program (cont.)
6
Data Type int
The numbers (0,1,2, etc.) are integers.
Integers are whole numbers which do not have decimal
point.
Integer values are one category of constants i.e. values
that appear in a program.
In C/C++, integers belong to a group of data called int.
int is a C/C++ data type.
7
Statements
A statement is a C/C++ instruction that causes an action
to be performed by the CPU.
A C/C++ statement translates into one or more machine
language instructions.
int main(void)
{
return 0; Notice the statement ends
with a semicolon (;).
}
8
Input-Process-Output
A basic program consists of the following
operations:
1. Get some input data
2. Process the data
3. Produce some output result
10
Declaration Statement
Before we can use a memory cell in the program, we need
to tell the compiler the name and the kind of data it will
store (data type).
12
Assignment Statement
How do we tell the program to store a value in the
memory cell?
By writing an assignment statement.
Example: Assignment statement tells the
program to store the value 5 in the
x = 5; memory cell with the name x.
14
Assignment Statement
x 5
x = 5;
y = x;
15
Assignment Statement
x 5
x = 5;
Copy value
y = x; of x into y
y
16
Assignment Statement
x 5
x = 5;
Copy value
y = x; of x into y
y 5
17
Complete Program
int main(void)
{
int x, y;
x = 5;
y = x;
return 0;
}
18
Complete Program
x = 5; Function Body
y = x;
return 0;
}
19
Complete Program
int main(void)
{
Declarations
int x, y;
x = 5;
y = x; Statements
return 0;
}
20
Complete Program with Memory
Manipulation
int main(void)
{
int x, y;
x = 5; Assignment statements
y = x;
return statement
return 0;
}
22
Showing Results
The syntax of cout and << is:
23
Showing Results
A manipulator is used to format the output
Example: endl causes insertion point to move to the beginning
of the next line.
24
Showing Results – Standard Libraries
The cout object is defined in standard libraries.
A library is a collection of classes and functions to provide
features and tasks for the language.
The cout object is described in a file called iostream
which must be inserted into a program in order to use it.
We insert this file by:
#include <iostream>
This is called a preprocessor directive.
25
Showing Results - Namespace
cin and cout are declared in the header file iostream, but
within std namespace. Namespace allows two functions to share a
common name. Function under a namespace must be addressed using
"namespace::" prefix.
To use cin and cout in a program, we also can use the following:
#include <iostream>
using namespace std;
The C++ Standard Library incorporates 18 headers of the ISO C90 C
standard library ending with ".h", but their use is deprecated. All other
headers in the C++ Standard Library DO NOT end in ".h".
The header from the C++ Standard Library is included under a different
name, generated by removing the .h, and adding a 'c' at the start; for
example, 'time.h' becomes 'ctime'.
26
Complete Program with Display
#include <iostream> Memory Cells
using namespace std;
x 5
int main(void)
{
int x, y; y 5
x = 5; Computer Screen
y = x;
cout << "y = " << y; y=5
return 0;
}
27
Computation – Arithmetic Operators
How do we make the program do some computation or
calculation?
Use arithmetic operators.
Operator In Maths In C/C++
Add a+b a+b
Subtract a-b a-b
Multiply ab a*b
a
Divide --- or a / b or a ÷ b a/b
b
Modulus a mod b a%b
28
Variables
Programs store data in variables. Variables refer to
the memory cells where values can be changed during
program execution.
29
Variable Declaration and Assignment
int n; Variable declaration – tells
compiler the variabe’s name and
data type.
n = 72;
Assigns value 72 to variable n.
31
Getting Input Data
The syntax of cin and >> is:
Example:
If num is an integer variable
32
Getting Input Data
We can input multiple data. Assume feet and
inches are variables of type int:
cin >> feet >> inches;
Inputs two integers from the keyboard
Places them in variables feet and inches respectively
The user should type the first number, followed by a space or
“Enter”, then the second number, followed by a/another
“Enter”.
33
Getting Input Data
34
Complete Program with Input
#include <iostream>
using namespace std;
int main(void)
{
int num;
Read statement
cin >> num;
return 0;
}
35
Getting Input Data
Computer Screen
A blinking cursor appears
_ and program waits for the
user to enter a number.
36
Complete Program with Display and
Input
#include <iostream>
using namespace std;
int main(void)
Displays a
{
prompt.
int num;
37
Getting Input Data
Computer Screen
Cursor appears and
Enter a number: _ program waits for the
user to enter a number
Prompt displayed by
cout object
38
Getting Input Data
Computer Screen
User types a number and
Enter a number: 91 presses “Enter” key.
num 91
39
Store a value into variable
Note the two ways to store a value into a variable:
int num;
By using the assignment statement
num = 35;
By using a read statement
cin >> num;
40
What does this program do?
#include <iostream>
using namespace std;
int main(void)
{
int n;
cout << "Enter n: ";
cin >> n;
int n_squared = n * n;
return 0;
}
41
What does this program do?
Computer Screen
5 n
Enter n: 5
n x n = 25
25 n_squared
42
What does this program do?
#include <iostream>
using namespace std;
int main(void)
{
int a, b;
cout << "Enter a: ";
cin >> a;
int sum = a + b;
return 0;
}
43
What does this program do?
Computer Screen a
531
Enter a: 531
Enter b: 24 24 b
a + b = 555
555 sum
44
What does this program do?
#include <iostream>
using namespace std;
int main(void)
{
int a, b;
cout << "Enter a and b: ";
cin >> a >> b;
int sum = a + b;
cout << a << " + " << b << " = " << sum << endl;
return 0;
}
45
What does this program do?
a 531 b 24 sum ?
46
What does this program do?
47
What does this program do?
#include <iostream>
using namespace std;
int main(void)
{
int a, b;
a = 3;
b = 5;
cout << "a = " << a << ", b = " << b << endl;
a = b;
b = a;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
48
What does this program do?
a = 3;
a 3 b ?
b = 5;
a = b;
b = a;
49
What does this program do?
a = 3;
a 3 b 5
b = 5;
a = b;
b = a;
50
What does this program do?
a = 3;
a 5 b 5
b = 5;
a = b;
b = a;
51
What does this program do?
a = 3;
a 5 b 5
b = 5;
a = b;
b = a;
52
What does this program do?
#include <iostream>
using namespace std;
int temp;
int main(void)
{ temp = a;
int a, b; a = b;
b = temp;
a = 3;
b = 5; cout << "a = " << a
<< ", b = " << b
cout << "a = " << a << endl;
<< ", b = " << b
<< endl; return 0;
}
53
What does this program do?
a = 3;
a 3 b ? temp ?
b = 5;
temp = a;
a = b;
b = temp;
54
What does this program do?
a = 3;
a 3 b 5 temp ?
b = 5;
temp = a;
a = b;
b = temp;
55
What does this program do?
a = 3;
a 3 b 5 temp 3
b = 5;
temp = a;
a = b;
b = temp;
56
What does this program do?
a = 3;
a 5 b 5 temp 3
b = 5;
temp = a;
a = b;
b = temp;
57
What does this program do?
a = 3;
a 5 b 3 temp 3
b = 5;
temp = a;
a = b;
b = temp;
58