ENG2139 Lecture 1
ENG2139 Lecture 1
Time Allocation
• Lectures 2 hours/week
• Laboratory/ Tutorials 3 hours/week
8
• After the source code is saved to a file, the
process of translating it to machine language
can begin.
• During the first phase of this process, a
program called the preprocessor reads the
source code.
• The preprocessor searches for special lines that
begin with the # symbol.
• These lines contain commands that cause the
preprocessor to modify the source code in
some way.
9
• During the next phase the compiler steps through
the preprocessed source code, translating each
source code instruction into the appropriate
machine language instruction.
• This process will uncover any syntax errors that
may be in the program. Syntax errors are illegal
uses of key words, operators, punctuation, and
other language elements.
• If the program is free of syntax errors, the
compiler stores the translated machine language
instructions, which are called object code , in an
object file.
10
• During the last phase of the translation process, another
program called the linker combines the object file with the
necessary library routines.
• Once the linker has finished with this step, an executable file is
created.
• The executable file contains machine language instructions, or
executable code , and is ready to run on the computer.
• Many development systems, particularly those on personal
computers, have integrated development environments (IDEs) .
• These environments consist of a text editor, compiler, debugger,
and other utilities integrated into a package with a single set of
menus.
• Preprocessing, compiling, linking, and even executing a program
is done with a single click of a button, or by selecting a single
item from a menu.
11
Designing and Creating a Program
• These are the steps recommended for the process of writing a program:
1. Clearly define what the program is to do.
2. Visualize the program running on the computer.
3. Use design tools such as a hierarchy chart, flowcharts, or pseudocode to
create a model of the program.
4. Check the model for logical errors.
5. Type the code, save it, and compile it.
6. Correct any errors found during compilation. Repeat
7. Steps 5 and 6 as many times as necessary.
8. Run the program with test data for input.
9. Correct any errors found while running the program.
10. Repeat Steps 5 through 8 as many times as necessary.
11. Validate the results of the program
12
Hierarchy Chart
• A hierarchy chart is a diagram that graphically
depicts the structure of a program. It has boxes that
represent each step in the program. The boxes are
connected in a way that illustrates their relationship
to one another.
• A hierarchy chart begins with the overall task and
then refines it into smaller subtasks.
• Each of the subtasks is then refined into even
smaller sets of subtasks, until each is small enough
to be easily performed.
13
Flowchart
• A flowchart is a diagram that shows the logical
flow of a program. It is a useful tool for
planning each operation a program performs
and the order in which the operations are to
occur.
14
Pseudocode
• Pseudocode is a cross between human
language and a programming language.
• Although the computer can’t understand
pseudocode, programmers often find it helpful
to write an algorithm in a language that’s
“almost” a programming language, but still
very similar to natural language.
• For example, here is pseudocode that
describes the pay-calculating program:
15
Display “How many hours did you work?”.
Input hours.
Display “How much do you get paid per hour?”.
Input rate.
Store the value of hours times rate in the pay
variable.
Display the value in the pay variable.
16
What is C++?
• C++ is a cross-platform language that can be used to create
high-performance applications.
• C++ is portable and can be used to develop applications that can be adapted
to multiple platforms. This means that a C++ program can be written on one
type of computer and then run on many other types of systems
• As C++ is close to C, C# and Java, it makes it easy for programmers to switch
to C++ or vice versa.
18
Difference between C and C++
• C++ was developed as an extension of C, and
both languages have almost the same syntax.
19
Getting Started
• To start using C++, you need two things:
20
Installing IDE
• An IDE (Integrated Development Environment) is used to edit AND compile
the code.
• Popular IDE's include CodeBlocks, Eclipse, and Visual Studio. These are all
free, and they can be used to both edit and debug C++ code.
• We will use Code Blocks in our lectures, which we believe is a good place to
start.
• Write the following C++ code and save the file as myfirstprogram.cpp (File
> Save File as) Then, go to Build > Build and Run to run (execute) the
program :
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
22
C++ Syntax
• Line 1: #include <iostream> is a header file library that lets us
work with input and output objects, such as cout (used in line
5). Header files add functionality to C++ programs. This
specific file (iostream) includes the declarations of the basic
standard input-output library in C++, and it is included
because its functionality is going to be used later in the
program.
• Line 2: using namespace std means that we can use names for
objects and variables from the standard library.
• Line 3: A blank line. C++ ignores white space. But we use it to
make the code more readable.
23
• Line 4: Another thing that always appear in a C++ program, is int
main(). This is called the main function. Any code inside its curly
brackets {} will be executed. The main function is the point by where
all C++ programs start their execution, independently of its location
within the source code. It does not matter whether there are other
functions with other names defined before or after it – the
instructions contained within this function's definition will always be
the first ones to be executed in any C++ program. For that same
reason, it is essential that all C++ programs have a main function
24
• Note: Every C++ statement ends with a semicolon ;.
• Note: The body of int main() could also been written as:
• int main () { cout << "Hello World! "; return 0; }
int main() {
cout << "Hello World!";
cout << "I am learning C++";
return 0;
}
26
• To insert a new line, you can use the \n character:
• The newline character (\n) is called an escape sequence, and
it forces the cursor to change its position to the beginning of
the next line on the screen. This results in a new line.
• Another way to insert a new line, is with the endl
manipulator:
int main() {
cout << "Hello World! \n";
cout << "I am learning C++";
return 0;
}
int main() {
cout << "Hello World!" << endl;
cout << "I am learning C++";
return 0;
}
27
C++ Comments
• Comments can be used to explain C++ code,
and to make it more readable. It can also be
used to prevent execution when testing
alternative code. Comments can be singled-
lined or multi-lined.
28
Single-line Comments
• Single-line comments start with two forward slashes (//).
30
C++ Variables
• Variables are containers for storing data values.
• int - stores integers (whole numbers), without decimals, such as 123 or -123
• double - stores floating point numbers, with decimals, such as 19.99 or -
19.99
• char - stores single characters, such as 'a' or 'B'. Char values are surrounded
by single quotes
• string - stores text, such as "Hello World". String values are surrounded by
double quotes
• bool - stores values with two states: true or false
31
Declaring (Creating) Variables
• To create a variable, specify the type and assign it a value:
Type variableName = value;
32
Display Variables
• The cout object is used together with the << operator to
display variables.
int x = 5;
int y = 6;
int sum = x + y;
cout << sum;
33
C++ Identifiers
• All C++ variables must be identified with unique names.
34
• The general rules for naming variables are:
36
C++ User Input
• You have already learned that cout is used to output (print)
values. Now we will use cin to get user input.
37
Creating a Simple Calculator
• In this example, the user must input two
numbers. Then we print the sum by calculating
(adding) the two numbers:
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
38
C++ Data Types
Data Type Size Description
boolean 1 byte Stores true or false values
char 1 byte Stores a single
character/letter/number, or ASCII
values
int 2 or 4 bytes Stores whole numbers, without
decimals
float 4 bytes Stores fractional numbers, containing
one or more decimals. Sufficient for
storing 6-7 decimal digits
double 8 bytes Stores fractional numbers, containing
one or more decimals. Sufficient for
storing 15 decimal digits
39
C++ String Data Types
• The string type is used to store a sequence of characters
(text). This is not a built-in type, but it behaves like one in
its most basic usage. String values must be surrounded by
double quotes:
#include <iostream>
#include <string>
using namespace std;
int main() {
string greeting = "Hello";
cout << greeting;
return 0;
}
40
C++ Operators
• Operators are used to perform operations on
variables and values.
• C++ divides the operators into the following groups:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Bitwise operators
41
C++ Arithmetic Operators
Operator Name Description Example
+ Addition Adds together two x+y
values
- Subtraction Subtracts one value x-y
from another
* Multiplication Multiplies two values x * y
/ Division Divides one value by x/y
another
% Modulus Returns the division x%y
remainder
++ Increment Increases the value ++x
of a variable by 1
-- Decrement Decreases the value --x
of a variable by 1
42
• Assignment operators are used to assign values to
variables.
• Comparison operators are used to compare two values
(or variables). This is important in programming,
because it helps us to find answers and make decisions.
43
Comparison Operators
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
44
C++ Logical Operators
Operator Name Description Example
&& Logical and Returns true if both x < 5 && x < 10
statements are true
|| Logical or Returns true if one x < 5 || x < 4
of the statements is
true
! Logical not Reverse the result, !(x < 5 && x < 10)
returns false if the
result is true
45
End of Lecture 1
46