Professional Documents
Culture Documents
1.7
1.8
1.9
1.10
1.11
1.12
1.13
1.14
1.15
Introduction
What is a Computer?
Computer Organization
Evolution of Operating Systems
Personal Computing, Distributed Computing
and Client/Server Computing
Machine Languages, Assembly Languages,
and High-level Languages
History of C and C++
C++ Standard Library
Java and Java How to Program
Other High-level Languages
Structured Programming
The Key Software Trend: Object Technology
Basics of a Typical C++ Environment
Hardware Trends
History of the Internet
1.16
1.17
1.18
1.19
1.20
1.21
1.22
1.23
1.24
1.1 Introduction
In this course you will learn
C and C++
Structured programming and object oriented programming
1.2
What is a Computer?
Computer
A device capable of performing computations and making
logical decisions
Computer programs
Sets of instructions that control a computers processing of
data
Hardware
Various devices comprising a computer
Examples: keyboard, screen, mouse, disks, memory, CDROM, and processing units
Software
Output unit
Outputs information (to screen, to printer, to control other
devices)
Memory unit
Rapid access, low capacity, stores input information
Operating systems
Manage transitions between jobs
Increased throughput
Amount of work computers process
Multiprogramming
Many jobs or tasks sharing a computers resources
Timesharing
2. Assembly languages
10
ANSI C
Established worldwide standards for C programming
Objects
Objects are reusable software components that
model things in the real world
Object-oriented programs better organized and are
easy to understand, correct and modify
Meaningful software units
Date objects, time objects, paycheck objects,
invoice objects, audio objects, video objects, file
objects, record objects, etc.
Any noun can be represented as an object
Favor modularity
2000 Prentice Hall, Inc. All rights reserved.
11
12
13
14
Pascal
Intended for academic use
Today:
C++
Java
Python
Perl
C#
Visual Basic
15
Multitasking
16
Editor
Preprocessor
Compiler
Linker
Loader
Disk
Programiscreatedin
theeditorandstored
ondisk.
Disk
Preprocessorprogram
processesthecode.
Disk
Compilercreates
objectcodeandstores
itondisk.
Disk
Linkerlinkstheobject
codewiththelibraries,
createsa.outand
storesitondisk
Primary
Memory
Loaderputsprogram
inmemory.
Disk
6. Execute
..
..
..
Primary
Memory
CPU
..
..
..
CPUtakeseach
instructionand
executesit,possibly
storingnewdata
valuesastheprogram
executes.
17
18
Packet switching
Transfers digital data via small packets
Allows multiple users to send and receive data
simultaneously
No centralized control
If one part of the Internet fails, other parts can still operate
Bandwidth
Carrying capacity of communications lines
TCP/IP
2000 Prentice Hall, Inc. All rights reserved.
19
20
21
Comments
#include <iostream>
1. Comments
Written between /* and */ or following
a //.
4
5
int main()
8
9
return 0;
Outline
3. main
10 }
Welcome to C++!
22
<<
Stream insertion operator
Value to the right of the operator (right operand) inserted
into output stream (which is connected to the screen)
std::cout << Welcome to C++!\n;
Escape character
Indicates that a special character is to be output
2000 Prentice Hall, Inc. All rights reserved.
23
Description
\n
\t
\r
\a
\\
\"
24
#include <iostream>
1. Load <iostream>
4
5
int main()
2. main
9
10
return 0;
Outline
11 }
Welcome to C++!
Program Output
25
#include <iostream>
Outline
1. Load <iostream>
2. main
int main()
2.2 newline
2.3 Print "to"
8
9
return 0;
10 }
2.5 newline
2.6 Print "C++!"
Welcome
to
2.7 newline
C++!
2.4 newline
Program Output
26
27
Description
Size
Range
char
Character or small
integer
1 byte
Short Integer
2 bytes
Int
Integer
4 bytes
Long integer
4 bytes
bool
1 byte
true or false
float
4 bytes
double
Double precision
floating point number
8 bytes
long double
16 bytes
wchar_t
Wide character
2 or 4
bytes
1 wide character
28
= (assignment operator)
Assigns value to a variable
Binary operator (has two operands)
Example:
29
// Addition program
#include <iostream>
Outline
30
1. Load <iostream>
4
5
int main()
2. main
2.1 Initialize variables
integer1,
prompt
integer2,
Notice how std::cin is used
to get userand sum
// declaration
//
10
// read an integer
11
12
// read an integer
13
// assignment of sum
14
std::cout << "Sum is " << sum << std::endl; // print sum
8
9
15
16
return 0;
//
17 }
input.
Program Output
2000 Prentice Hall, Inc. All rights reserved.
31
4 bytes
A visual representation
integer1
45
32
integer1
integer2
integer1
45
integer2
72
45
72
sum
117
33
1.22
Arithmetic
Arithmetic calculations
Use * for multiplication and / for division
Integer division truncates remainder
7 / 5 evaluates to 1
Operator precedence
Some arithmetic operators act before others (i.e.,
multiplication before addition)
Be sure to use parenthesis when needed
Do not use: a + b + c / 3
Use: (a + b + c ) / 3
2000 Prentice Hall, Inc. All rights reserved.
34
1.22
Arithmetic
Arithmetic operators:
C++ operation Arithmetic
operator
+
Addition
Algebraic
expression
f+7
C++ expression
Subtraction
Multiplication
pc
bm
p - c
b * m
Division
x/y
x / y
Modulus
rmods
r % s
f + 7
Operation(s)
()
Parentheses
*, /, or %
+ or -
Addition
Subtraction
35
Arithmetic Example
Step 1.
y = 2 * 5 * 5 + 3 * 5 + 7;
(Leftmost multiplication)
2 * 5 is 10
Step 2.
y = 10 * 5 + 3 * 5 + 7;
(Leftmost multiplication)
10 * 5 is 50
Step 3.
y = 50 + 3 * 5 + 7;
3 * 5 is 15
Step 4.
y = 50 + 15 + 7;
(Leftmost addition)
50 + 15 is 65
Step 5.
y = 65 + 7;
(Last addition)
65 + 7 is 72
Step 6.
y = 72;
(Last operationplace 72 in y )
using std::cout;
using std::cin;
using std::endl;
36
37
C++ equality
or relational
operator
Example
of C++
condition
Meaning of
C++ condition
Relationaloperators
>
>
x>y
xisgreaterthany
<
<
x<y
xislessthany
>=
x>=y
xisgreaterthanorequaltoy
<=
x<=y
xislessthanorequaltoy
Equalityoperators
==
x==y
xisequaltoy
!=
x!=y
xisnotequaltoy
38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Outline
1. Load <iostream>
Notice the using statements.
2. main
int main()
{
int num1, num2;
num1 3 num2 7
statements
of the condition.
if ( num1 != num2 )
is true,
isit not
equalbody
to 7of if
cout << num1 << " is not equal to " << num2 << endl; 3 If
statement is executed.
39
cout << num1 << " is less than or equal to " 3 is less than or equal to 7
<< num2 << endl;
2000 Prentice Hall, Inc. All rights reserved.
34
35
36
37
38
return 0;
Outline
2.3 exit (return 0)
39 }
Program Output
3 != 7
3 == 7
3 <7
3> 7
3 <= 7
3 >= 7
22 != 12
40
22 == 12
22 > 12 22 < 12
22 >= 12 22 <= 12
7 != 7
7 == 7
7 <= 7
7 <7
7 >= 7
7>7
Behaviors - actions
A ball rolls, bounces, inflates and deflates
Objects can perform actions as well
Inheritance
New classes of objects absorb characteristics from existing classes
Information hiding
41
42
43
44
2.6
Key words
Keywords
auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while