Uploaded by Abdelrahman Nasrallah

1. Chapter 1

advertisement
Chapter 1
Introduction to Computers and C++
Programming
Major Components of Computer
• Two Major Components:
– Hardware
• Devices/electronic components comprising a computer
• Central Processing unit (CPU), memory, peripheral devices
(keyboard, screen, mouse, disks, CD-ROM, …etc.)
– Software
• Collection of statements (Programs) that run on a computer
and instruct it to perform a certain task.
• Two types: System programs/ Operating Systems and
Application programs.
2
3
Main Components
– Five main components
• Input devices
– Allows communication to the computer
– Examples: Keyboard; Mouse
• Output devices
– Allows communication to the user
– Example: Monitor display
• Processor (CPU): Performs instructions as per program
• Main memory
– Memory locations containing the running program
• Secondary memory
– Permanent record of data
– Examples: Hard disk; SSD; Flash; CD
Main Memory
• Main Memory
– Long list of memory locations
• Information is stored binary form (1s and 0s).
• Can change during program execution
– Binary Digit or Bit
• A digit that can only be zero (0) or one (1)
– Byte (a group of 8 bits)
• Each memory location has eight bits
– Address
• A number that uniquely identifies a memory location
High-Level Languages
• Common programming languages include …
C C++ C# Java Python Pascal Visual Basic
FORTRAN COBOL Lisp
• These high – level languages
– Resemble human languages
– Are designed to be easy to read and write
– Use more complicated instructions than
the CPU can follow
– Must be translated to zeros and ones (digital signals) for
the CPU to execute a program. This 1s and 0s
representation is called the machine language. The CPU
can follow only the machine language at hardware level.
Compilers
• Translate high-level language to machine
language
– Source code
• the original program in a high-level language
– Object code
• the translated version in machine language
Linkers
• Some programs we use are already compiled
– Their object code is available for us to use
– For example: Input and output routines
• A Linker combines
– The object code for the programs we write
and
– The object code for the pre-compiled routines
into
the machine language program the CPU can run
Algorithm and Program
• Algorithm
– A sequence of precise instructions (or steps)
which leads to a solution for the problem
• Program
– An algorithm expressed in a certain programming
language the computer can understand
A Sample C++ (Source) Program
// A first program in C++.
#include <iostream>
using namespace std;
void main ()
{
cout << “Hello World”<< endl; // ( “<<“ is an output function)
} // end function main
<<OUTPUT>>
Hello World
11
The Program Development Process
1. EDIT
e.g., hello.cpp
Source Code
e.g., hello.o
2. Preprocess &
COMPILE
Object Code
e.g., hello.exe
3. LINK
Executable
4. Load &
EXECUTE
Programmer/ | Preprocessor/Compiler | Linker
Notepad
| Operating System
12
The Steps …
1. Editing: writing the source file using an editor or a word
processor
• In C++, the name of the source file must end with .cpp (e.g. file.cpp,
example.cpp)
• Common editors: MS wordpad / notepad or MS word (MS windows)
2. Preprocessing/Compiling
– Preprocessor:
• Search for statements with hash (“#” ) sign and replace it with
the statements included within the associated file.
• The file contains the definition of other programs stored in system
libraries (ex. IO programs) and needed by the current program.
– Compiling : translating the source code into object/machine code which
is saved in an object file:
• The name of the object file ends with “.o” (e.g file.o)
• Common C++ compilers: Microsoft visual C++, Borland C++, etc.
13
The Steps …
3. Linking: combining several object files into one executable file
(file.exe)
• Linking is skipped if only one object file is involved
• A user program is typically linked with the object files of
commonly used C++ programs that have been precompiled and
stored.
• In order to use the functions of a given C++ library in your
program, you must include the header file (lib.h or lib) of the
corresponding library into your program.
4. Loading & Executing
–
Loading: Placing the executable (.exe) file in main memory
• This step is implicit (the user does not do anything)
–
Executing: running the executable file
14
Program Development Process
(the Programming Lab)
• In our C++ Lab, we use an Integrated Development
Environment (IDE) tool produced by Microsoft, namely MS
Visual Studio, to write, compile, link and run C++ programs.
• Visual Studio supports the general program development
process, presented above, with the additional step of creating a
project to store the different files that get generated throughout
the development process.
• The program development using MS Visual Studio will be
discussed in the lab
• We can also use one of the several online compilers (e.g.,
cpp.sh , https://www.onlinegdb.com/online_c++_compiler )
and other IDEs (e.g., Eclipse)
15
A Sample C++ Program (hello.cpp)
// A first program in C++.
#include <iostream>
using namespace std;
void main()
{
cout << “Hello World“ << endl;
} // end function main
// OUTPUT
Hello World
16
General Comments on Programming
Languages
• Languages abide by some rules
– Grammar
– Syntax
• A computer program consists of sequential flow of instructions (or
statements), in general. These are step-by-step instructions to computer
to perform certain task (e.g., addition, division, etc.)
Step 1
Step 2
…
Step n
• Two types of statements in a program
– Executable (computer executes the statement)
– Non-executable (computer does not execute the statement). Example:
Comments
17
Sample Program
// A first program in C++.
#include <iostream>
using namespace std;
// function main begins program execution
void main()
{ // start of main
cout << "Welcome to C++!\n";
} // end function main
18
Output
Welcome to C++!
Press any key to continue . . .
(The last line is produced by the system)
19
Comments on Sample Program
 A C++ program must have a main() procedure. This
is the entry point for executing the program.
 There are matching curly brackets (“{“ and “}”) for the
main() procedure.
 Comments start with either


// (every thing up to the end of the current line is
commented), or
Beginning with /* and ending with */. Comments can go
over several lines.
 Comments are needed to document the main
concepts and approach used in the program.
 Comments are optional, but highly recommended
20
Comments on Sample Program
(contd.)
 Each executable statement ends with a semicolon (;)
 C++ is case-sensitive; writing mAIN() instead of
main() will be a syntax error.
 There are several reserved keywords; such as main,
void, etc.
 Line starting with #include.. is a pre-processor
directive. It tells the system to include certain system
file (in our example iostream.h). The file contains the
code for some I/O (Input/Output) related functions.
 The pre-processor directive begins with a number
symbol (#) as its first non-blank character in the line
21
Comments on Sample Program
(contd.)
 Compiler comes with standard “libraries” that
contain useful functions so that the
programmers do not need to re-invent the
wheel.
 Each library has a standard header file (with
extension .h). The header file must be
included in the source file (using directive
#include <..>) to use any function available
through that library.
 void main() means that the function main()
returns nothing (void)
22
Comments on Sample Program
(contd.)
 The header file iostream must be included for any




program that outputs data to the screen or inputs
data from the keyboard
cout << "Welcome to C++!\n"; instructs the computer
to print on the screen the string of characters
contained between the quotation marks.
The operator << is called as the stream insertion
operator. The value to the right of the operator is
inserted in the default output stream (Screen).
\n is a special “escape” character, and it means
newline. It causes the cursor to move to the
beginning of the next line on the screen.
You may have one or more blank lines between
23
statements.
C++ Program Skeleton
#include <iostream>
using namespace std;
void main()
{ // start of main
// C++ statements
// ….
} // end function main
24
Another Sample C++ Program
// program: test1-1.cpp.
#include <iostream>
using namespace std;
void main()
{
cout << "Welcome to AUS";
cout << "in the city of Sharjah \n";
cout << "A B \n C D E \n";
}
25
Output
Welcome to AUSin the city of Sharjah
AB
CDE
26
A Program with Errors
// program: test1-2.cpp.
#include <iostream>
using namespace std;
void main()
{
cout << "Welcome to AUS";
cout << "in the city of Sharjah \n"
cout << "A B \n C D E \n";
}
27
A program with int main()
// test1-4.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Welcome to C++!\n";
return 0;
}
<<OUTPUT>>
Welcome to C++!
28
Comments
 The C++ keyword “return” is one of the
several ways to exit a function.
 int main()  main function returns an integer.
 At the end of main, we need to return an
integer to be consistent with the syntax.
29
Program Errors

Syntax errors


Violation of the grammar rules of the language
Discovered by the compiler


Run-time errors


Error messages may not always show correct location of
errors
Error conditions detected by the computer at run-time when your
program is actually running. Example: Division by 0.
Logic errors




Errors in the program’s algorithm
Most difficult to diagnose
Computer does not recognize an error; program compiles, links and
executes fine, but produces incorrect results.
For example, in order to compute Area of a rectangle, if we say
Area = Width + Length; // logic error
instead of
Area = Width * Length;
Download