COS132
Imperative Programming
Lecture 1.3
(Week 1, Lecture 3)
Chapter 2:
Introduction to C++
Wednesday 8 Feb 2016 & Thursay 9 Feb
Group 5: 08:30 – 09:20 (EMB 2-151), Wed
Group 4: 10:30 – 11:20 (IT 4-5), Thurs
Some admin
• Quizz on ClickUP
2
Chapter 2:
Introduction
to
C++
Topics to be discussed
2.1 The Parts of a C++ Program
2.2 The cout Object
(and the cin object)
2.3 The #include Directive
2.4 Variables (and Literals)
2.14 Arithmetic Operators
4
2.1 The Parts of a C++ Program
// sample C++ program comment
#include <iostream> preprocessor directive
using namespace std;
which namespace to use
int main()
beginning of function named main
{ beginning of block for main
cout << "Hello, there!"; output statement
return 0; string literal
} Send 0 to operating system
end of block for main
Special characters: // # < > { } "" ;
2.2 The cout Object
• Displays output on the computer screen
• You use the stream insertion operator << to send output
to cout:
cout << "Programming is fun!";
• Can be used to send more than one item to cout:
cout << "Hello " << "there!";
Or:
cout << "Hello ";
cout << "there!";
• This produces one line of output:
cout << "Programming is ";
cout << "fun!";
The endl Manipulator
• You can use the endl manipulator to start a new line of
output. This will produce two lines of output:
cout << "Programming is" << endl;
cout << "fun!";
• cout << "Programming is" << endl;
cout << "fun!";
Programming is
fun!
The \n Escape Sequence
• You can also use the \n escape sequence to start a new
line of output.
• This will produce two lines of output:
cout << "Programming is\n";
cout << "fun!";
Programming is
fun!
The #include Directive
• Inserts the contents of another file into the program
• This is a preprocessor directive, not part of C++
language
• #include lines not seen by compiler
• Do not place a semicolon at end of #include line
#include <iostream>
using namespace std;
2.4 Variables
• Variable:
– a storage location in memory (one or more bytes)
– Has a name and a type of data it can hold
– Must be defined before it can be used, e.g.
int x;
e.g. of picture of memory and variables:
149
x is the name of memory block starting at location whose
address is 16, which holds the integer value 149
2.14 Arithmetic Operators
• Used for performing numeric calculations
• C++ has unary, binary, and ternary operators:
– unary (1 operand) -5
– binary (2 operands) 13 - 7
– ternary (3 operands) exp1 ? exp2 : exp3 (NOT GOOD)
Binary Arithmetic Operators
SYMBOL OPERATION EXAMPLE VALUE OF ans
+ addition ans = 7 + 3; 10
- subtraction ans = 7 - 3; 4
* multiplication ans = 7 * 3; 21
/ division ans = 7 / 3; 2
% modulus ans = 7 % 3; 1
Program example
//variable definition, input using cin & output using cout
#include <iostream>
using namespace std;
int main() variable
definition
{
int x, y;
input
cout << "Enter x value:";
using
cin >> x;
cin
y = x * 5;
cout << "x times 5 = "
<< y one
<< endl; output
return 0; statement
}
Class exercise
//program example Convert the following algorith
//------------------------ to a C++ program
#include <iostream>
Algorithm to read values into x
using namespace std;
and y and compute & display the
int main()
sum z = x + y
{
int x, y; 1. Prompt user to enter x
cout << "Enter x value:"; value
cin >> x; 2. Read x value
y = x * 5;
cout << "x times 5 = :" 3. Prompt user to enter y
<< y value
<< endl; 4. Read y value
return 0;
} 5. Compute z = x + y
6. Display the z value
Class exercise: solution
Convert the following #include <iostream>
algorith to a C++ program using namespace std;
int main()
Algorithm to read values into x {
and y and compute & display int x, y, z;
the sum z = x + y
cout << "Enter x value:";
cin >> x;
1. Prompt user to enter x
cout << "Enter y value:";
value
cin >> y;
2. Read x value
3. Prompt user to enter y z = x + y;
value cout << "x + y = " << z
4. Read y value << endl;
5. Compute z = x + y return 0;
6. Display the z value }
Class exercise: Demo of program
#include <iostream>
using namespace std; 1. Compile & run
int main()
{ 2. possible compilation
int x, y, z; errors
cout << "Enter x value:";
cin >> x;
cout << "Enter y value:";
cin >> y;
z = x + y;
cout << "x + y = " << z
<< endl;
return 0;
}
Note the following:
For group 4:
– The content of slides 11 to 15 will be discussed in the next
lecture
For group 5:
– The content of slides 11 to 15 will be re-visited in the next
lecture
16