Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 93

School of Computing

Sciences
Introduction to C++
Programming

Riara University Lecture 2 1


  •

                                   

 
Object

Oriented programming
                                   

   •

Object-oriented programming (OOP) is a


                                   

 
programming

language model organized
                                   

around objects rather than "actions" and data


  than logic. Historically, a program has

rather                                    

been viewed as a logical procedure that takes


  data, processes it, and produces output
input

                                   

 
data.

                                   

  •

                                   

  •

                                   

  •

                                   
Riara University Lecture 2 2
Features of Object Oriented Programming (OOP)

Riara University Lecture 2 3


FEATURES OF OOP:
 Object
 Class
 Data Hiding and Encapsulation
 Dynamic Binding
 Message Passing
 Inheritance
 Polymorphism

Riara University Lecture 2 4


Object
 OBJECT: Object is a collection of number of
entities.
 Objects take up space in the memory. Objects

are instances of classes.


 When a program is executed , the objects

interact by sending messages to one another.


 Each object contain data and code to

manipulate the data. Objects can interact


without knowing details of each others data
or code. 

Riara University Lecture 2 5


CLASS
 CLASS: Class is a collection of objects of similar
type.
 Objects are variables of the type class. Once a
class has been defined, we can create any number
of objects belonging to that class. Eg: grapes,
bananas and orange are the member of class fruit.
Example:
Fruit mango;
 In the above statement object mango is created
which belong to the class fruit.
 NOTE: Classes are user define data types.

Riara University Lecture 2 6


DATA ABSTRACTION AND ENCAPSULATION:

 Combining data and functions into a single unit called


class and the process is known as Encapsulation.
 Data encapsulation is important feature of a class.
Class contains both data and functions. Data is not
accessible from the outside world and only those
function which are present in the class can access the
data.
 The insulation of the data from direct access by the
program is called data hiding or information hiding.
 Hiding the complexity of program is called Abstraction
and only essential features are represented. In short
we can say that internal working is hidden.

Riara University Lecture 2 7


DYNAMIC BINDING& MESSAGE
PASSING
 DYNAMIC BINDING: Refers to linking of
function call with function definition (the
code that makes up the function) is called
binding and when it takes place at run time it
is called dynamic binding.

 MESSAGE PASSING: The process by which one


object can interact with other object is called
message passing.

Riara University Lecture 2 8


POLYMORPHISM
 POLYMORPHISM: A greek term means ability
to take more than one form. An operation
may exhibite different behaviours in different
instances. The behaviour depends upon the
types of data used in the operation.
 Example:
 Operator Overloading 
 Function Overloading

Riara University Lecture 2 9


INHERITANCE:
 INHERITANCE: it is the process by which object
of one class acquire the properties or features
of objects of another class. The concept of
inheritance provide the idea of reusability
means we can add additional features to an
existing class without Modifying it. This is
possible by driving a new class from the
existing one. The new class will have the
combined features of both the classes.
Example:  Robin is a part of the class flying
bird which is again a part of the class bird. 

Riara University Lecture 2 10


Q: What is C++
 C++ is a compiled, object-oriented language
 It is the “successor” to C, a procedural
language
 (the “++” is called the successor operator in C++)
 C was derived from a language called B which
was in turn derived from BCPL(Basic
Combined Programming Language)
 C was developed in the 1970’s by Dennis
Ritchie of AT&T Bell Labs
 C++ was developed in the early 1980’s by
Bjarne Stroustrup of AT&T Bell Labs.
 Most of C is a subset of C++
Riara University Lecture 2 11
Riara University Lecture 2 12
Riara University Lecture 2 13
C++ Program
Consists of…
 Declarations
 Define the use of various identifiers, thus
creating the elements used by the program
(computer)
 Statements
 Or executable statements, representing
actions the computer will take on the user’s
behalf

Riara University Lecture 2 14


Identifiers
 Names for various entities used in a program;
used for...
 Variables: values that can change frequently
 Constants: values that never changes
 Functions: programming units that represents
complex operations
 Parameters: values that change infrequently

Riara University Lecture 2 15


Simple C++ Program
#include <iostream>  Compiler
Using Namespace std; directive:
Tells the
int main() compiler
{
what to do
before
// Declarations
compiling
// Statements  This one
return 0;
includes
} source code
from another
file

Riara University Lecture 2 16


Simple C++ Program
#include <iostream>  Main
Using Namespace std; function -
marks the
int main() beginning of
{ an execution
// Declarations of a
// Statements program.
return 0;
}

Riara University Lecture 2 17


Simple C++ Program
#include <iostream>  Header for
Using Namespace std; main function
 States…
int main()  data type
{ for the
return value
// Declarations  identifier for
// Statements function
return 0;  list of
} arguments
between
parenthesis
(none for
this
function) 18
Riara University Lecture 2
Simple C++ Program
#include <iostream>  Braces
Using Namespace std; enclose the
int main() body of the
{ function
// Declarations  They
// Statements represent the
return 0; start and end
} of the
function

Riara University Lecture 2 19


Simple C++ Program
#include <iostream>  Declarations
Using Namespace std; and
statements
int main()  Main body of
{ function (or
// Declarations main part)
// Statements
 “//”
return 0;
represents
}
the start of a
comment

Riara University Lecture 2 20


Simple C++ Program
#include <iostream>  Return
Using Namespace std; statement
int main()  specifies the
{ value the
// Declarations function
// Statements returns
return 0;
 All (almost)
} declarations
and
statements
end with a
semi-colon
“;”
Riara University Lecture 2 21
Simple C++ Program
#include <iostream>
Using Namespace std;
 This
program
int main()
{
doesn’t
// Declarations do
// Statements
return 0;
anything
} !

Riara University Lecture 2 22


Sample C++ Program
#include <iostream>  Variable
Using Namespace std; declaration
 The identifier
void main() number is
{ declared as
int number; being of data
cout << “Enter a number” << type int, or
endl;
integer
cin >> number;
cout << “You entered: “ <<
number << endl;
}
Riara University Lecture 2 23
Sample C++ Program
#include <iostream>
Using Namespace std;  cout
the output
int main() statement for
{ C++
int number;  Note the
cout << “Enter a number” << direction of
endl; “<<“
cin >> number;  endl
cout << “You entered: “ << represents an
number << endl; end-of-line
Return 0;
}

Riara University Lecture 2 24


Sample C++ Program
#include <iostream>  cin
Using Namespace std; the input
statement for
void main() C++
{  Note the
int number; direction of
cout << “Enter a number” << “>>”
endl;
cin >> number;
cout << “You entered: “ <<
number << endl;
}
Riara University Lecture 2 25
Sample C++ Program
#include <iostream>  Did you
Using Namespace std; copy this
int main() down?
{  You had
int number;
cout << “Enter a number” <<
better,
endl; since this
cin >> number; will be the
cout << “You entered: “ << first
number << endl; program
return 0; you’ll try!
}
Riara University Lecture 2 26
Sample C++ Program
#include <iostream>
Using Namespace std;
 That
int main()
{ mea
int number;
cout << “Enter a number” << ns
right
endl;
cin >> number;

now!
cout << “You entered: “ <<
number << endl;
return 0;
}
Riara University Lecture 2 27
Assignment
 Assignment is an operation that assigns
the value of an expression to a variable
 It operates/assigns from left to right
 Ex.
Total = 2 + 3 + 5
 First, the expresssion “2 + 3 + 5” is
evaluated
 Then, this value is assigned to the
variable “Total”
Riara University Lecture 2 28
Assignment
 When a variable is declared, space is
allocated in the computer’s memory for the
variable
 Each data type requires a different number of
bytes in memory for storing a variable
int - 2
float - 4
double - 8
char, bool - 1

Riara University Lecture 2 29


Assignment
 When a variable is
Total = 2 + 3 + 5;
assigned a value,
the value is placed
into the variable’s
memory location
Total
10

Riara University Lecture 2 30


#include <iostream>
2 using namespace std;
3 int main()
4{
5     
6     cout << "Hello, world!" << endl;
7     return 0;
}

The output of the above program is :


Hello World

Riara University Lecture 2 31


Header Files
C++ Standard Library header files
Each contains a portion of the Standard Library.
Function prototypes for the related functions
Definitions of various class types and functions
Constants needed by those functions
“Instruct” the compiler on how to interface with
library and user-written components.
Header file names ending in .h
Are “old-style” header files
Superseded by the C++ Standard Library header files
Use #include directive to include class in a
program.

Riara University Lecture 2 32


Preprocessor directives:
• Preprocessor directives are lines
included in the code of programs
preceded by a hash sign (#).
• These lines are not program statements
but directives for the preprocessor.
• The preprocessor examines the code
before actual compilation of code begins
and resolves all these directives before
any code is actually generated by regular
statements.
Riara University Lecture 2 33
• std is an abbreviation of standard. std is
the standard namespace. cout, cin and a
lot of other things are defined in it. ( This
means that one way to call them is
by using std::cout and std::cin.)
• In essence, a namespace defines a
scope

Riara University Lecture 2 34


C++ Standard
Library header Explanation
files
<iostream> Contains function prototypes for the C++ standard input and
standard output functions. This header file replaces header file
<iostream.h>. This header is discussed in detail in
Chapter 26, Stream Input/Output.
<iomanip> Contains function prototypes for stream manipulators that format
streams of data. This header file replaces header file
<iomanip.h>. This header is used in Chapter 26, Stream
Input/Output.
<cmath> Contains function prototypes for math library functions. This
header file replaces header file <math.h>.
<cstdlib> Contains function prototypes for conversions of numbers to text,
text to numbers, memory allocation, random numbers and various
other utility functions. This header file replaces header file
<stdlib>.
<ctime> Contains function prototypes and types for manipulating the time
and date. This header file replaces header file <time.h>.
<vector>, These header files contain classes that implement the C++
<list> Standard Library containers. Containers store data during a
<deque>, program’s execution.
<queue>,
<stack>,
<map>,
<set>,
<bitset>

Riara University Lecture 2 35


Riara University Lecture 2 36
Riara University Lecture 2 37
Variables and C++ Data
Types

Riara University Lecture 2 38


Program to Display Sum of Two
Numbers

Riara University Lecture 2 39


Riara University Lecture 2 40
Riara University Lecture 2 41
identifiers

Riara University Lecture 2 42


mathExample2.cpp
// math example

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
cout << "The reciprocal of 10 is " << 1.0/10.0 << endl;
cout << "The square root of 10 is " << sqrt(10.0) << endl;
cout << "e^(10.0) = " << exp(10.0) << endl;
cout << "The reciprocal of 15 is " << 1.0/15.0 << endl;
cout << "The square root of 15 is " << sqrt(15.0) << endl;
cout << "e^(15.0) = " << exp(15.0) << endl;

return 0; // exit program


}

Riara University Lecture 2 43


mathExample2.cpp
> g++ -o mathExample2.exe mathExample2.cpp

> mathExample2.exe
The reciprocal of 10 is 0.1
The square root of 10 is 3.16228
e^(10.0) = 22026.5
The reciprocal of 15 is 0.0666667
The square root of 15 is 3.87298
e^(15.0) = 3.26902e+06

>

Riara University Lecture 2 44


Variables (1)
 Programs like mathExample2.cpp have little
use in practice…

 There is no room for change. They simply


print the same output onto the screen every
time they are executed.

 A more interesting program might have


different behavior under different
circumstances.

Riara University Lecture 2 45


Variables (2)
 For instance, a program that asks for a
number and outputs its reciprocal and square
root is much more useful than
mathExample2.cpp.

◦ This program needs placeholders for the incoming


values.
◦ These placeholders are called variables

Riara University Lecture 2 46


mathExample3.cpp
// math example

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
double x;

x = 10.0;
cout << "The reciprocal of 10 is " << 1.0/x << endl;
cout << "The square root of 10 is " << sqrt(x) << endl;
cout << "e^(" << x << ") = " << exp(x) << endl;

x = 15.0;
cout << "The reciprocal of 15 is " << 1.0/x << endl;
cout << "The square root of 15 is " << sqrt(x) << endl;
cout << "e^(" << x << ") = " << exp(x) << endl;

return 0; // exit program


}

Riara University Lecture 2 47


Declaration Statements
 Before you can use a variable, you must declare it.
This allows the computer to set aside the memory
that will be needed for that variable.

 Variables consist of a name and its data type. C++


variable declarations are of the form:
dataType variableName;

 dataType can be: int, float, char, double, unsigned


int, ...
 variableName must be composed of alphanumeric
characters or underscore ‘_’.

Riara University Lecture 2 48


Declaration Example
#include <iostream>
using namespace std;

int main()
{
int age;
float wage;
char initial;
double height;

return 0; // exit program


}

Riara University Lecture 2 49


Variable Names
 Variable names are composed of the characters:
a,b,c,..,z,A,B,C,…,Z,0,1,2,…,9 and _.
 Variables names must begin with:

a,b,c,..,z,A,B,C,…,Z or _.
 Capitalized and lower case letters are different.
 Examples:
int age; int age1;
int Age; int age2;
int myAge; int age3B;
int Jacks_age; int _age;

Riara University Lecture 2 50


Variable Names
Which of these are valid variable names?
 me2
 Me2
 2L8
 R-Wenger
 He_who_hesitates_is_lost
 HeWhoHesitatesIsLost
 Gordon.Gee

Riara University Lecture 2 51


Variable Assignments
 Variables can be assigned values during or
after declaration, but never before (why?)

 Assignment is done with the equals sign


height = 67.34;
initial = ‘E’; //initial = E; will not work as expected
double totalPay = salary + overtime;

 Variable assignment is NOT commutative!


The variable must always be on the left side
of an equals sign. The following is NOT
valid:
67 = x;

Riara University Lecture 2 52


mathExample4.cpp
// math example

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
double x;

cout << "Enter x: "; // Note: no new line


cin >> x; // Note: operator ">>“, not operator "<<"

cout << "The reciprocal of " << x << " is " << 1.0/x << endl;
cout << "The square root of " << x << " is " << sqrt(x) << endl;
cout << "e^(" << x << ") = " << exp(x) << endl;

return 0; // exit program


}

Riara University Lecture 2 53


Input Using cin (1)

double x;

cout << "Enter x: "; // Note: no new line


cin >> x; // Note: operator ">>“, not operator "<<"

 cin (Console INput) can be used to obtain user


input .
 Unlike cout, use >> with cin, and not <<
 When the program is run, cin will wait indefinitely
for user input.
 cin will input a single value into a variable when it
detects a new line from the input:
 Remember that before using inputting values into
variables, the variables MUST have already been
declared!

Riara University Lecture 2 54



int main()
{
double x;

cout << "Enter x: "; // Note: no new line


cin >> x; // Note: operator ">>“, not operator "<<"

cout << "The reciprocal of " << x << " is " << 1.0/x << endl;
cout << "The square root of " << x << " is " << sqrt(x) << endl;
cout << "e^(" << x << ") = " << exp(x) << endl;

> mathExample4.exe
Enter x: 10.0
The reciprocal of 10 is 0.1
The square root of 10 is 3.16228
e^(10) = 22026.5
>

Riara University Lecture 2 55


mathExample4.cpp
// math example

#include <iostream>
#include <cmath>

using namespace std;


Try inputs:
int main() 20
{
double x; -20
1000
cout << "Enter x: "; // Note: no new line
cin >> x; // Note: operator ">>“, not operator "<<“ -1000
0
cout << "The reciprocal of " << x << " is " << 1.0/x <<
endl; -0
cout << "The square root of " << x << " is " << sqrt(x)
<< endl;
cout << "e^(" << x << ") = " << exp(x) << endl;

return 0; // exit program


}

Riara University Lecture 2 56


xyz1.cpp
// multiple declarations example

#include <iostream>
using namespace std;

int main()
{
double x;
double y;
double z;

cout << "Enter x, y and z: ";


cin >> x;
cin >> y;
cin >> z;

cout << "x = " << x << endl;


cout << "y = " << y << endl;
cout << "z = " << z << endl;

return 0; // exit program


}

Riara University Lecture 2 57


cout << "Enter x, y and z: ";


cin >> x;
cin >> y;
cin >> z;

cout << "x = " << x << endl;


cout << "y = " << y << endl;
cout << "z = " << z << endl;

> xyz1.exe
Enter x, y and z: 1 2 3
x=1
y=2
z=3

>

Riara University Lecture 2 58


cout << "Enter x, y and z: ";


cin >> x;
cin >> y;
cin >> z;

cout << "x = " << x << endl;


cout << "y = " << y << endl;
cout << "z = " << z << endl;

> xyz1.exe
Enter x, y and z:
1
2
3
x=1
y=2
z=3

>

Riara University Lecture 2 59


Multiple Declarations
 In the previous example we had
double x;
double y;
double z;

 This can be done in one statement like this:


double x, y, z;

 This is very useful when creating several


variables of the same type.

Riara University Lecture 2 60


xyz2.cpp
// multiple declarations example

#include <iostream>
using namespace std;

int main()
{
double x, y, z; // multiple declarations

cout << "Enter x, y and z: ";


cin >> x;
cin >> y;
cin >> z;

cout << "x = " << x << endl;


cout << "y = " << y << endl;
cout << "z = " << z << endl;

return 0; // exit program


}

Riara University Lecture 2 61


xyz3.cpp
// multiple declarations example

#include <iostream>
using namespace std;

int main()
{
double x, y, z;

cout << "Enter x, y and z: ";


cin >> x >> y >> z; // read x, then y, then z

cout << "x = " << x << endl;


cout << "y = " << y << endl;
cout << "z = " << z << endl;

return 0; // exit program


}

Riara University Lecture 2 62


Multiple Inputs Using cin
 cin can be used to obtain multiple inputs.

 cin knows when to delimit (i.e. start looking


for the next input) upon reaching a
“whitespace.”

 Whitespaces are: tabs, spaces, new lines

Riara University Lecture 2 63



cout << "Enter x, y and z: ";
cin >> x >> y >> z;

cout << "x = " << x << endl;


cout << "y = " << y << endl;
cout << "z = " << z << endl;

> xyz1.exe
Enter x, y and z: 1 2 3
x=1
y=2
z=3

>

Riara University Lecture 2 64



cout << "Enter x, y and z: ";
cin >> x >> y >> z;

cout << "x = " << x << endl;


cout << "y = " << y << endl;
cout << "z = " << z << endl;

> xyz1.exe
Enter x, y and z:
1
2
3
x=1
y=2
z=3

>

Riara University Lecture 2 65


Breaking up Multiple Inputs
 Sometimes it makes more sense to break up multiple
inputs into single inputs, even if they are correlated:

int x, y, z;

cout << “Enter x: “;


cin >> x;

cout << “Enter y: “;


cin >> y;

cout << “Enter z: “;


cin >> z;

Riara University Lecture 2 66


cin and cout
Which of the following C++ statements have
syntax errors and what are the errors?
 cout >> “Answer = ” >> x+y >> endl;
 cin << x;
 cout << “Yes, ” << “ or ” << “ no “ << “ or ”

<< “ maybe.” << endl;


 cin >> yes >> no >> maybe;
 cout << “x + y = ” (x + y) << endl;

Riara University Lecture 2 67


Variables (3)
 Variables are used to hold data within a
program (the data is held in your
computer’s main memory). A program can
read-from and write-to variables. That is,
their values can vary.

 Every variable consists of two parts:


1. The name of a variable is tied to a location in
memory
2. Its data type (discussed next...)

Riara University Lecture 2 68


Data Type: Integers (1)
 An integer value is any number that has no
decimal point.

123 -45000 +1432431 0 are all valid integers

 1,244 is not a valid integer in C++; no


commas are used. Neither is $12 because $
is not a valid part of an integer.

Riara University Lecture 2 69


Data Type: Integers (2)
 The largest and smallest integers supported is
dependent of the computer on which the program
is compiled. Most of today’s computers use 32-bits
to represent an integer, so 232 values can be
represented.

 Integers can be signed or unsigned.


◦ What is the maximum value of an unsigned 32 bit
integer?
◦ What is the maximum value of a signed 32 bit
integer?

Riara University Lecture 2 70


intExample1.cpp
// example using type int

#include <iostream>
using namespace std;

int main()
{
int x, y;
Try inputs:
cout << "Enter x and y: ";
cin >> x >> y; // Read in x and 17 3
then y
3 17
cout << "x = " << x << endl; 0 17
cout << "y = " << y << endl;
cout << "x+y = " << x+y << endl; 17 0
cout << "x/y = " << x/y << endl; 2000000000 2000000000
cout << "Done." << endl;

return 0; // exit program


}

Riara University Lecture 2 71


Integer Division
 In C++, (15 / 2) is 7. Why?
 Remember that integers have no fractional parts!
 There is no rounding in integer division. If your
program contained: cout << (15 / 16); The
output would be 0.

 The fractional part of an integer division is


truncated so the result can be an integer. If you
need the remainder, use %. If you need a decimal
answer, make your operands floating-point
numbers:
cout << (15.0 / 16.0);

Riara University Lecture 2 72


Data Types: Floating Point Numbers
(1)
 Floating-point numbers have a decimal
point, and they can also be signed or
unsigned.

 There are three basic types:


float
double
long double
◦ The differences between these are their supported
range and precision.

Riara University Lecture 2 73


Data Types: Floating Point Numbers
(2)
 To represent a floating point number:
◦ float uses 32 bits (4 bytes)
◦ double uses 64 bits (8 bytes)
◦ long double uses 128 bits (16 bytes)

 The tradeoff is storage vs. precision and


range

 What exactly is the precision and range, and


how are floating point numbers represented
in binary format? IEEE 754 Standard

Riara University Lecture 2 74


Data Types: Floating Point Numbers
(3)
 Always use “double” to represent
floating point numbers.

Riara University Lecture 2 75


Data Types: Floating Point Numbers
(4)
 Floating-point numbers can be written in
exponential notation:
134.56 or 1.3456e2
-0.00345 or -3.45e-3

 Here, e is short for “times ten to the power


of”, just like in scientific notation.

Riara University Lecture 2 76


rationalExample1.cpp
// example using type double

#include <iostream>
using namespace std;

int main()
{ Try inputs:
double x, y;
cout << "Enter x and y: "; 17 3
cin >> x >> y;
3 17
cout << "x = " << x << endl; 17 0
cout << "y = " << y << endl;
cout << "x+y = " << x+y << endl; 4000000000 4000000000
cout << “x*y = " << x*y << endl; 1e100 1e100
cout << “x/y = " << x/y << endl;
cout << "Done." << endl; 1e200 1e200

return 0; // exit program


}

Riara University Lecture 2 77


Data Type: Characters
 Characters include:
◦ All letters of the alphabet (upper and lower case)
◦ The symbolic representation of digits 0 – 9
◦ All various symbols such as: + * & ^ % $ | , !

 A character value is simply one of these in


single quotes, such as 'A' or '8' or ':' or ' '
(blank space).

Riara University Lecture 2 78


Data Type: Characters
 A character value is a character in single
quotes, such as 'A' or '8' or ':' or ' ' (blank
space).
 NOTE: '8' and 8 are different.
◦ '8' is the symbolic representation of the character,
‘8’;
◦ 8 is the integer 8.

Riara University Lecture 2 79


Data Type: Characters
 Characters usually take up 8 bits (1 byte)

 That means there are 28 (or 256) different


characters, and every number within the
range of [0, 255] is mapped onto some
character

 So a character really boils down to a


numerical representation known as ASCII
encoding.

Riara University Lecture 2 80


ASCII Code
Code Char Code Char Code Char Code Char
32 Space 48 0 65 A 97 a
33 ! 49 1 66 B 98 b
34 " 50 2 67 C 99 c
35 # 51 3 68 D 100 d
36 $ 52 4 69 E 101 e
37 % 53 5 70 F 102 f
38 & 54 6 71 G 103 g
39 ' 55 7 72 H 104 h
40 ( 56 8 73 I 105 i
41 ) 57 9 74 J 106 j
… … … … … … … …

Riara University Lecture 2 81


ASCII Table

Riara University Lecture 2 82


charExample1.cpp
// example using type char

#include <iostream>
using namespace std;

int main()
{
char c1, c2, c3;

cout << "Enter first initial: ";


cin >> c1;
cout << "Enter second initial: ";
cin >> c2;

c3 = 'X';

cout << "Created by: ";


cout << c1 << c2 << c3 << endl;

return 0; // exit program


}

Riara University Lecture 2 83



char c1, c2, c3;
cout << "Enter first initial: ";
cin >> c1;
cout << "Enter second initial: ";
cin >> c2;
c3 = 'X';
cout << "Created by: ";
cout << c1 << c2 << c3 << endl;

> charExample1.exe
Enter first initial: R
Enter second initial: W
{What is the output?}

Riara University Lecture 2 84


Characters Strings
 A character string is an array of characters.
 Examples:

◦ "Hello"
◦ "Hello World!" (Note: Blank space is part of the
string.)
◦ "He who hesitates is lost.\nHaste makes waste.\
n"
◦ "" (The empty string.)

Riara University Lecture 2 85


Character Strings
 NOTE: 'A' and "A" are different.
◦ 'A' is the symbolic representation of the character,
‘A’;
◦ "A" is a string containing a single character.
 NOTE: '8' and "8" and 8 are different.
◦ '8' is the symbolic representation of the character,
‘8’;
◦ "8" is a string containing a single character;
◦ 8 is the integer 8.

Riara University Lecture 2 86


Data Type: Boolean
 The Boolean data type is used for just two
values: true and false
 Like characters, they only consume 1 byte of

storage.

 It is worth noting that when dealing with


Boolean values in C++, any number other
than 0 is always interpreted as true.

Riara University Lecture 2 87


Arithmetic Operations
 Addition: 2 + 3
 Subtraction: 5 - 2
 Multiplication: 10 * 4
 Division: 12 / 3
 Modulus 11 % 2

Riara University Lecture 2 88


Order of Operations
 Arithmetic expressions are evaluated
according to the following order of
operations
 At each level, operations are evaluated
left to right
 (1) Parenthesis, Functions
(2) Multiplication, Division
(3) Addition, Subtraction
 (4) Modulus
Riara University Lecture 2 89
Parenthesis
 Parenthesis are used to alter the order
with which operations are evaluated
 Ex.
4 + 5 * 2 equals 14
(4 + 5) * 2 equals 18

Riara University Lecture 2 90


Here we go!
 Problem: To determine the average of three
numbers
 Task: Request, from the user, three numbers,
compute the average and the three numbers,
and print out the original values and the
computed average
 Do it!
 You have 0 minutes!

Riara University Lecture 2 91


Arithmetic Operations (2)
// Examples of arithmetic operations

#include <iostream>
using namespace std;

int main()
{
cout << "5 % 3 = " << (5 % 3) << endl;
cout << "4 - 3 = " << (4 - 3) << endl;
cout << "5.0 / 2.0 = " << (5.0 / 2.0) << endl;
cout << "5 / 2 = " << (5 / 2) << endl;

// Is there any real difference in the last two statements?

return 0;
}

Riara University Lecture 2 92



cout << "5 % 3 = " << (5 % 3) << endl;
cout << "4 - 3 = " << (4 - 3) << endl;
cout << "5.0 / 2.0 = " << (5.0 / 2.0) << endl;
cout << "5 / 2 = " << (5 / 2) << endl;

>arithmetic.exe
5%3=2
4-3=1
5.0 / 2.0 = 2.5
5/2=2

>

Riara University Lecture 2 93

You might also like