1
CMP 132
Computer Programming
Introduction to C++ Programming
Lecture 1
© 2003 Prentice Hall, Inc. All rights reserved.
2
Computer Languages
•
Machine language
•
•
•
•
•
•
Only language computer directly understands
Defined by hardware design
– Machine-dependent
Generally consist of strings of numbers
– Ultimately 0s and 1s
Instruct computers to perform elementary operations
– One at a time
Cumbersome for humans
Example:
01110111010001
© 2003 Prentice Hall, Inc. All rights reserved.
3
Computer Languages
•
Assembly language
•
•
•
•
English-like abbreviations representing elementary computer
operations
Clearer to humans
Incomprehensible to computers
– Translator programs (assemblers)
• Convert to machine language
Example:
LOAD BASEPAY
ADD
OVERPAY
STORE GROSSPAY
© 2003 Prentice Hall, Inc. All rights reserved.
4
Computer Languages
•
High-level languages
•
Generally a computer program is written using a high-level
language, Similar to everyday English.
• A high-level language is one that is understandable by us,
humans. This is called source code.
• However, a computer does not understand high-level language.
It only understands the program written in 0's and 1's in binary,
called the machine code.
• To convert source code into machine code, we use either a
compiler or an interpreter.
© 2003 Prentice Hall, Inc. All rights reserved.
5
Compiler Vs. Interpreter
• Both compilers and interpreters are used to convert a program written
in a high-level language into machine code understood by computers.
However, there are differences between how an interpreter and a
compiler works.
• Compiler transforms code written in a high-level programming
language into the machine code, at once, before program runs.
• Interpreter coverts each high-level program statement, one by one,
into the machine code, during program run.
• Compiled code runs faster while interpreted code runs slower.
• Translator programs (compilers)
– Convert to machine language
• Interpreter programs
– Directly execute high-level language programs
© 2003 Prentice Hall, Inc. All rights reserved.
6
Compiler Vs. Interpreter
Interpreter
Compiler
Translates program one statement at a
time.
Scans the entire program and
translates it as a whole into machine
code.
Interpreters usually take less amount
of time to analyze the source code.
However, the overall execution time
is
comparatively
slower
than
compilers.
Compilers usually take a large
amount of time to analyze the source
code. However, the overall execution
time is comparatively faster than
interpreters.
No intermediate object
generated, hence are
efficient.
Generates intermediate object code
which further requires linking, hence
requires more memory.
code is
memory
Programming
languages
JavaScript, Python, Ruby
interpreters.
© 2003 Prentice Hall, Inc. All rights reserved.
like
use
Programming languages like C, C++,
Java use compilers.
7
Basics of a Typical C++ Environment
• C++ systems
– Program-development environment
– Language
– C++ Standard Library
• C++ program names extensions
– .cpp
– .cxx
– .cc
– .C
© 2003 Prentice Hall, Inc. All rights reserved.
8
Setting up C++ Development Environment
• C++ runs on lots of platform like Windows, Linux, Unix, Mac,
etc.
• We need an environment to be set-up on local computer to
compile and run C++ programs successfully.
• We can also use online Integrated Development Environment
(IDEs) for compiling programs.
• IDE is a software application that provides facilities to a
computer programmer for developing software.
• There are many online IDEs available which you can use to
compile and run your programs easily without setting up a local
development environment.
• Example:
– https://www.onlinegdb.com/online_c++_compiler
© 2003 Prentice Hall, Inc. All rights reserved.
9
Setting up local environment
• For setting up your own personal development
environment on your local machine you need to install two
important software:
1. Text Editor: type of programs used to edit or write
texts. A text file containing C++ program should be
saved with ‘.CPP’ or ‘.C’ extension. Files ending with
the extension ‘.CPP’ and ‘.C’ are called source code
files. These extension helps the compiler to identify that
the file contains a C++ program.
2. C++ Compiler: A compiler is a computer program
which converts high-level language (source code) into
machine understandable low-level language.
© 2003 Prentice Hall, Inc. All rights reserved.
10
Basics of a Typical C++ Environment
Phases of C++ Programs:
1.
Edit
2.
Preprocess
3.
Compile
4.
Link
5.
Load
6.
Execute
Editor
Disk
Program is created in
the editor and stored
on disk.
Preprocessor
Disk
Preprocessor program
processes the code.
Disk
Compiler creates
object code and stores
it on disk.
Compiler
Linker
Loader
Disk
Disk
Primary
Memory
Linker links the object
code with the libraries,
creates an executable
file and stores it on disk
Loader puts program
in memory.
..
..
..
Primary
Memory
CPU
© 2003 Prentice Hall, Inc. All rights reserved.
..
..
..
CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
executes.
11
A Simple Program: Printing a Line of Text
• Before writing the programs
– Comments
• Document programs
• Improve program readability
• Ignored by compiler
• Single-line comment
– Use C’s comment /* .. */ OR Begin with // or
– Preprocessor directives
• Processed by preprocessor before compiling
• Begin with #
© 2003 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
// Fig. 1.2: fig01_02.cpp
// A first program in C++.
#include <iostream>Function main returns an
Single-line comments.
Outline
directive to
integer
value.
Left brace
{ begins Preprocessor
function
fig01_02.cpp
include
input/output Statements
stream
// function main begins
execution
Function
main appears
body.program
end with a(1 of 1)
header
file <iostream>.
int main()
exactly once
in every
C++ semicolon ;.
{
program..
fig01_02.cpp
std::cout << "Welcome to C++!\n";
Corresponding right brace }
return 0;
// indicate
thatbody.
program ended successfully
ends function
Stream
insertion
Name cout
belongs
to operator.
} // end function main namespace std.
Keyword return is one of
Welcome to C++!
several means to exit
function; value 0 indicates
program terminated
successfully.
output (1 of 1)
© 2003 Prentice Hall, Inc.
All rights reserved.
12
13
A Simple Program: Printing a Line of Text
• Standard output stream object
– std::cout
– “Connected” to screen
– <<
• Stream insertion operator
• Value to right (right operand) inserted into output stream
• Namespace
– std:: specifies using name that belongs to “namespace”
std
– std:: removed through use of using statements
• Escape characters
– \
– Indicates “special” character output
© 2003 Prentice Hall, Inc. All rights reserved.
14
A Simple Program: Printing a Line of Text
Escape Sequence
Description
\n
Newline. Position the screen cursor to the
beginning of the next line.
\t
Horizontal tab. Move the screen cursor to the next
tab stop.
\a
Alert. Sound the system bell.
\\
Backslash. Used to print a backslash character.
\"
Double quote. Used to print a double quote
character.
© 2003 Prentice Hall, Inc. All rights reserved.
15
Another Simple Program: Adding Two Integers
• Variables
– Location in memory where value can be stored
– Common data types
• int - integer numbers
• Char – characters
• String – stream of characters
• double - floating point numbers
• float – Floating numbers
• bool – true or false
• Void – for all data types
– Declare variables with name and data type before use
int integer1;
int integer2;
int sum;
– Can declare several variables of same type in one declaration
• Comma-separated list
int integer1, integer2, sum;
© 2003 Prentice Hall, Inc. All rights reserved.
16
Another Simple Program: Adding Two Integers
• Input stream object
– >> (stream extraction operator)
• Used with std::cin
• Waits for user to input value, then press Enter (Return) key
• Stores value in variable to right of operator
– Converts value to variable data type
• = (assignment operator)
– Assigns value to variable
– Binary operator (two operands)
– Example:
sum = variable1 + variable2;
© 2003 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Fig. 1.6: fig01_06.cpp
// Addition program.
#include <iostream>
Outline
// function main begins program execution
int main()
Declare integer variables.
{
int integer1; // first number to be input by user
int integer2; // second number to be input by user
Use
stream
int sum;
// variable in
which
sumextraction
will be stored
fig01_06.cpp
(1 of 1)
operator with standard input
stream to obtain
user input.
std::cout << "Enter first integer\n";
// prompt
std::cin >> integer1;
// read an integer
std::cout << "Enter second integer\n"; // prompt
std::cin >> integer2;
an integer
Calculations //
canread
be performed
in output statements: alternative for
Stream manipulator
std::endl outputs a
sum = integer1 + integer2; // assign result to sum
newline, then “flushes output
std::cout << "Sum is " << integer1 + integer2 << std::endl;
std::cout << "Sum is " << sum << std::endl; // print sum buffer.”
lines 18 and 20:
return 0;
// indicate that program ended successfully
} // end function main
Concatenating, chaining or
cascading stream insertion
operations.
© 2003 Prentice Hall, Inc.
All rights reserved.
17
18
Memory Concepts
• Variable names
– Correspond to actual locations in computer's memory
– Every variable has name, type, size and value
– When new value placed into variable, overwrites previous
value
– std::cin >> integer1;
– Assume user entered 45
integer1 45
integer1 45
– std::cin >> integer2;
– Assume user entered 72
integer2 72
integer1 45
– sum = integer1 + integer2;
integer2 72
sum
© 2003 Prentice Hall, Inc. All rights reserved.
117
19
Arithmetic
• Arithmetic calculations
– * : Multiplication
– / : Division
• Integer division truncates remainder
– 7 / 5 evaluates to 1
– % : Modulus operator returns remainder
– 7 % 5 evaluates to 2
Operator(s)
Operation(s)
Order of evaluation (precedence)
()
Parentheses
Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If
there are several pairs of parentheses “on the same level”
(i.e., not nested), they are evaluated left to right.
*, /, or %
Multiplication Division Evaluated second. If there are several, they re
Modulus
evaluated left to right.
+ or -
Addition
Subtraction
© 2003 Prentice Hall, Inc. All rights reserved.
Evaluated last. If there are several, they are
evaluated left to right.
20
Any
Questions
© 2003 Prentice Hall, Inc. All rights reserved.
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )