Академический Документы
Профессиональный Документы
Культура Документы
Outline 1.1 1.2 1.3 1.4 1.5 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.6
1.7 1.8 1.9 1.10 1.11 1.12 1.13 1.14 1.15
1.1 Introduction
In this course you will learn
C and C++ Structured programming and object oriented programming
1.2
Computer
What is a Computer?
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
Programs that run a computer
2000 Prentice Hall, Inc. All rights reserved.
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
Perform a small portion of one users job then moves on to service the next user
1.5 Personal Computing, Distributed Computing, and Client/Server Computing Personal computers
Economical enough for individual
Distributed computing
Organizations computing is distributed over networks
Client/server computing
Sharing of information, across computer networks, between file servers and clients (personal computers)
1.6 Machine Languages, Assembly Languages, and High-level Languages Three types of programming languages
Machine languages
Strings of numbers giving machine specific instructions Example: +1300042774 +1400593419 +1200274027
Assembly languages
English-like abbreviations representing elementary computer operations (translated via assemblers) Example: LOAD BASEPAY ADD OVERPAY STORE GROSSPAY
2000 Prentice Hall, Inc. All rights reserved.
10
11
12
13
COBOL
Used to manipulate large amounts of data
Pascal
Used to teach structured programming
14
Multitasking
Many activities to run in parallel
15
More understandable, better organized and easier to maintain than procedural programming Favor modularity
16
Preprocessor
Disk
Compiler
Disk
Linker
Disk
Primary Memory
4. Link
5. Load
Loader
Loader puts program in memory.
Disk
. . . . . .
6. Execute
CPU
Primary Memory CPU takes each instruction and executes it, possibly storing new data values as the program 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
19
20
1.17 General Notes About C++ and This Book Programming clarity is stressed
21
1 2
22
Outline
Comments 1. Comments Written between /* and */ or following a //. Improve program readability and do not cause the computer to perform any action. 2. Load <iostream.h> preprocessor directive 3. main
Message to the C++ preprocessor. 3.1 Print "Welcome to C++\n" Lines beginning with # are preprocessor directives. #include <iostream.h> tells the preprocessor 3.2 exit (return 0) to include the contents of the file <iostream> C++ programs contain one or more functions, one ,of which includes input/output operations (such as which must be main Program Output printing to the screen). Parenthesis are used to indicate a function Prints the string of characters contained between the int means that main "returns" an integer value. quotation marks.
Welcome to C++!
return is a way to exit a function from a function. return 0, in this case, means that the program terminated normally.
The entire line, including cout, the << operator, the A left brace { begins the body of every function string "Welcome to C++!\n" and the semicolon and a right brace } ends it. (;), is called a statement.
All statements must end with a semicolon.
23
<<
Stream insertion operator Value to the right of the operator (right operand) inserted into output stream (which is connected to the screen) cout << Welcome to C++!\n;
\
Escape character Indicates that a special character is to be output
24
\a \\ \"
1 2 3
// Fig. 1.4: fig01_04.cpp // Printing a line with multiple statements #include <iostream.h>
25
Outline
1. Load <iostream.h> 2. main 2.1 Print "Welcome" 2.2 Print "to C++!" 2.3 newline 2.4 exit (return 0) Program Output
4
5 6 7 8 9 10 return 0; // indicate that program ended successfully int main() { cout << "Welcome "; cout << "to C++!\n";
11 }
Welcome to C++!
Unless new line '\n' is specified, the text continues on the same line.
1 2 3 4 5 6 7 8 9
// Fig. 1.5: fig01_05.cpp // Printing multiple lines with a single statement #include <iostream.h>
26
Outline
1. Load <iostream.h> 2. main
2.2 newline
2.3 Print "to"
return 0;
2.4 newline 2.5 newline 2.6 Print "C++!" 2.7 newline 2.8 exit (return 0)
10 }
Welcome to C++!
Program Output
27
28
1.20 Another Simple Program: Adding Two Integers >> (stream extraction operator)
When used with cin, waits for the user to input a value and stores the value in the variable to the right of the operator The user types a value, then presses the Enter (Return) key to send the data to the computer Example:
int myVariable; cin >> myVariable; Waits for user input, then stores input in myVariable
= (assignment operator)
Assigns value to a variable Binary operator (has two operands) Example:
sum = variable1 + variable2;
2000 Prentice Hall, Inc. All rights reserved.
1 3 4 5 6 7 8 9 10 11 12 13 14 15 16
29
2 // Addition program
#include <iostream.h>
Outline
1. Load <iostream.h>
int main() { int integer1, integer2, sum; cout << "Enter first integer\n"; cin >> integer1; cin >> integer2; sum = integer1 + integer2; // // declaration
2. main 2.1 Initialize variables integer1, prompt integer2 , and sum Notice how std::cin is used to get user input. 2.2 Print "Enter first integer" 2.2.1 Get input
// assignment of sum
cout << "Sum is " << sum << endl; // print sum return 0; //
17 }
2.3 Print "Enter second integer" indicate that program ended successfully endl flushes the buffer and prints a 2.3.1 Get input newline. 2.4 Add variables and put result into sum Variables can be output using cout << variableName. 2.5 Print "Sum is" 2.5.1 Output sum 2.6 exit (return 0) Program Output
30
A visual representation
integer1 45
31
1.22
Arithmetic
Arithmetic calculations
Use * for multiplication and / for division
Operator precedence
Some arithmetic operators act before others (i.e., multiplication before addition)
Be sure to use parenthesis when needed
32
1.22
Arithmetic operators:
C++ o p era tio n Arithm etic o p era to r Addition Subtraction Multiplication Division Modulus +
Arithmetic
Alg eb ra ic exp ressio n f+7 pc bm x/y r mod s C++ exp ressio n f + 7 p - c b * m x / y r % s
* /
%
*, /, or % + or -
Multiplication Division Evaluated second. If there are several, they re Modulus evaluated left to right. Addition Subtraction Evaluated last. If there are several, they are evaluated left to right.
33
34
x is greater than y x is less than y x is greater than or equal to y x is less than or equal to y
Equality operators =
== !=
x == y x != y
1 2 3 4 5 66 7 8
// Fig. 1.14: fig01_14.cpp // Using if statements, relational // operators, and equality operators #include <iostream.h>
35
Outline
1. Load <iostream.h> 2. main
9 10 int main() 2.1 Initialize num1 and 11 { num2 12 int num1, num2; 13 2.1.1 Input data 14 cout << "Enter two integers, and I will tell you\n" 15 << "the relationships they satisfy: "; if statements 16 cin >> num1 >> num2; // read two integers Enter two integers, and I will tell2.2 you 17 the relationships they satisfy: 3 7 18 if ( num1 == num2 ) 19 cout << num1 << " is equal to " << num2 << endl; The if statements test the truth 20 of the condition. If it is true, 21 if ( num1 != num2 ) body of if statement 22 cout << num1 << " is not equal to " << num2 << endl; 3 is not equal to 7 is 23 executed. If not, body is 24 if ( num1 < num2 ) skipped. 25 cout << num1 << " is less than " << num2 << endl; 3 is less than 7 26 To include multiple statements 27 if ( num1 > num2 ) in a body, delineate them with 28 cout << num1 << " is greater than " << num2 << endl; braces {}. 29 30 if ( num1 <= num2 ) 31 cout << num1 << " is less than or equal to " 3 is less than or equal to 7 32 << num2 << endl; 2000 Prentice Hall, Inc. All rights reserved. 33
34 35 36 37 38
if ( num1 >= num2 ) cout << num1 << " is greater than or equal to " << num2 << endl;
36
Outline
2.3 exit (return 0)
return 0;
39 }
Enter two integers, and I will tell you the relationships they satisfy: 3 7 3 is not equal to 7 3 is less than 7 3 is less than or equal to 7
Program Output
Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12
Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7
1.24 Thinking About Objects: Introduction to Object Technology and the Unified Modeling Language Object orientation
Natural way to think about the world and to write computer programs Attributes - properties of objects
Size, shape, color, weight, etc.
37
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
Objects usually do not know how other objects are implemented
2000 Prentice Hall, Inc. All rights reserved.
1.24 Thinking About Objects: Introduction to Object Technology and the Unified Modeling Language Abstraction - view the big picture
See a photograph rather than a group of colored dots Think in terms of houses, not bricks
38
1.24 Thinking About Objects: Introduction to Object Technology and the Unified Modeling Language Unified Modeling Language (UML)
Used to model object-oriented systems and aid with their design Complex, feature-rich graphical language
39