Introduction to Computers and C++
Introduction to Computers and C++
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
3 4
• Software • Computer
– Instructions to command computer to perform actions and – Device capable of performing computations and making
make decisions logical decisions
• Hardware • Computer programs
• Standardized version of C++ – Sets of instructions that control computer’s processing of
data
– United States
• American National Standards Institute (ANSI) • Hardware
– Worldwide – Various devices comprising computer
• International Organization for Standardization (ISO) • Keyboard, screen, mouse, disks, memory, CD-ROM,
processing units, …
• Structured programming
• Software
• Object-oriented programming
– Programs that run on computer
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
1
5 6
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
7 8
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
2
9 10
1.5 Personal Computing, Distributed
1.4 Evolution of Operating Systems
Computing, and Client/Server Computing
• Multiprogramming • Personal computers
– Many jobs or tasks sharing computer’s resources – 1977: Apple Computer
– “Simultaneous” operation of many jobs – Economical enough for individual
• Timesharing – 1981: IBM Personal Computer
– 1960s – “Standalone” units
– Special case of multiprogramming
– Users access computer through terminals
• Computer networks
• Devices with keyboards and screens – Over telephone lines
• Dozens, even hundreds of users – Local area networks (LANs)
– Perform small portion of one user’s job, then moves on to • Distributed computing
service next user
– Organization’s computing distributed over networks
– Advantage:
• User receives almost immediate responses to requests
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
11 12
1.5 Personal Computing, Distributed 1.6 Machine Languages, Assembly
Computing, and Client/Server Computing Languages, and High-level Languages
• Workstations • Three types of computer languages
– Provide enormous capabilities 1. Machine language
• Only language computer directly understands
– Information shared across networks
• “Natural language” of computer
• Client/server computing • Defined by hardware design
– File servers – Machine-dependent
• Offer common store of programs and data • Generally consist of strings of numbers
– Ultimately 0s and 1s
– Client computers
• Instruct computers to perform elementary operations
• Access file servers across network
– One at a time
• UNIX, Linux, Microsoft’s Window-based systems • Cumbersome for humans
• Example:
+1300042774
+1400593419
+1200274027
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
3
13 14
1.6 Machine Languages, Assembly 1.6 Machine Languages, Assembly
Languages, and High-level Languages Languages, and High-level Languages
• Three types of computer languages • Three types of computer languages
2. Assembly language 3. High-level languages
• English-like abbreviations representing elementary computer • Similar to everyday English, use common mathematical
operations notations
• Clearer to humans • Single statements accomplish substantial tasks
• Incomprehensible to computers – Assembly language requires many instructions to
– Translator programs (assemblers) accomplish simple tasks
• Convert to machine language • Translator programs (compilers)
• Example: – Convert to machine language
LOAD BASEPAY • Interpreter programs
ADD OVERPAY – Directly execute high-level language programs
STORE GROSSPAY • Example:
grossPay = basePay + overTimePay
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
15 16
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
4
17 18
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
19 20
1.10 Visual Basic, Visual C++ and C# 1.10 Visual Basic, Visual C++ and C#
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
5
21 22
1.10 Visual Basic, Visual C++ and C# 1.11 Other High-level Languages
• C# • FORTRAN
– Anders Hejlsberg and Scott Wiltamuth (Microsoft) – FORmula TRANslator
– Designed specifically for .NET platform – 1954-1957: IBM
– Roots in C, C++ and Java – Complex mathematical computations
• Easy migration to .NET • Scientific and engineering applications
– Event-driven, fully object-oriented, visual programming • COBOL
language
– COmmon Business Oriented Language
– Integrated Development Environment (IDE)
– 1959: computer manufacturers, government and industrial
• Create, run, test and debug C# programs
computer users
• Rapid Application Development (RAD)
– Precise and efficient manipulation of large amounts of data
– Language interoperability
• Commercial applications
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
23 24
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
6
25 26
1.13 The Key Software Trend: Object
1.14 Basics of a Typical C++ Environment
Technology
• Objects • C++ systems
– Reusable software components that model real world items – Program-development environment
– Meaningful software units – Language
• Date objects, time objects, paycheck objects, invoice objects, – C++ Standard Library
audio objects, video objects, file objects, record objects, etc.
• Any noun can be represented as an object
– More understandable, better organized and easier to maintain
than procedural programming
– Favor modularity
• Software reuse
– Libraries
• MFC (Microsoft Foundation Classes)
• Rogue Wave
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
27 28
1.14 Basics of a Typical C++ Environment 1.14 Basics of a Typical C++ Environment
Program is created in
Editor Disk
Phases of C++ Programs: the editor and stored
on disk. • Input/output
Preprocessor Disk Preprocessor program – cin
1. Edit processes the code.
Compiler creates • Standard input stream
Compiler Disk object code and stores
2. Preprocess it on disk. • Normally keyboard
Linker links the object
Linker Disk code with the libraries,
– cout
3. Compile Primary
creates [Link] and
stores it on disk • Standard output stream
Memory
Loader • Normally computer screen
4. Link – cerr
Loader puts program
in memory.
5. Load Disk ..
..
• Standard error stream
..
7
29 30
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
31 32
1.16 History of the Internet 1.17 History of the World Wide Web
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
8
33 34
1.18 World Wide Web Consortium (W3C) 1.18 World Wide Web Consortium (W3C)
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
35 36
1.19 General Notes About C++
1.20 Introduction to C++ Programming
and This Book
• Book geared toward novice programmers • C++ language
– Stress programming clarity – Facilitates structured and disciplined approach to computer
– C and C++ are portable languages program design
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
9
37 38
1 // Fig. 1.2: fig01_02.cpp
1.21 A Simple Program: 2 // A first program in C++. Single-line comments. Outline
3 Function main
#include <iostream> returns an
Printing a Line of Text 4 integer { begins Preprocessor
value.
Left brace function directive to
fig01_02.cpp
5 // function main Function
body. program
begins include input/output Statements
main appears
execution stream end with a(1 of 1)
6 int main() header file <iostream>.
exactly once in every C++ semicolon ;.
• Comments 7 {
program..
8 std::cout << "Welcome to C++!\n"; fig01_02.cpp
– Document programs 9 Corresponding right brace } output (1 of 1)
10 return 0; // ends function
indicate thatbody.
program ended successfully
– Improve program readability 11 Stream
Name cout insertion
belongs to operator.
12 } // end function main
– Ignored by compiler namespace std.
Keyword return is one of
– Single-line comment Welcome to C++!
several means to exit
• Begin with // function; value 0 indicates
program terminated
• Preprocessor directives successfully.
39 40
1.21 A Simple Program: 1.21 A Simple Program:
Printing a Line of Text Printing a Line of Text
• Standard output stream object
– std::cout Escape Sequence Description
– “Connected” to screen
\n Newline. Position the screen cursor to the
– << beginning of the next line.
• Stream insertion operator \t Horizontal tab. Move the screen cursor to the next
• Value to right (right operand) inserted into output stream tab stop.
\r Carriage return. Position the screen cursor to the
• Namespace beginning of the current line; do not advance to the
– std:: specifies using name that belongs to “namespace” next line.
std \a Alert. Sound the system bell.
– std:: removed through use of using statements \\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote
• Escape characters character.
– \
– Indicates “special” character output
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
10
41 42
1 // Fig. 1.4: fig01_04.cpp 1 // Fig. 1.5: fig01_05.cpp
2 // Printing a line with multiple statements.
Outline 2 // Printing multiple lines with a single statement
Outline
3 #include <iostream> 3 #include <iostream>
4 4
fig01_04.cpp fig01_05.cpp
5 // function main begins program execution Multiple stream insertion 5 // function main begins program execution Using newline characters to
6 int main()
(1 of 1) 6 int main() print on multiple lines. (1 of 1)
statements produce one line
7 { 7 {
of output. fig01_04.cpp fig01_05.cpp
8 std::cout << "Welcome "; 8 std::cout << "Welcome\nto\n\nC++!\n";
9 std::cout << "to C++!\n"; output (1 of 1) 9 output (1 of 1)
10 10 return 0; // indicate that program ended successfully
11 return 0; // indicate that program ended successfully 11
12 12 } // end function main
13 } // end function main
Welcome
Welcome to C++! to
C++!
43 44
1.22 Another Simple Program: 1.22 Another Simple Program:
Adding Two Integers Adding Two Integers
• Variables • Variables
– Location in memory where value can be stored – Variable names
– Common data types • Valid identifier
• int - integer numbers – Series of characters (letters, digits, underscores)
• char - characters – Cannot begin with digit
• double - floating point numbers – Case sensitive
– 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. © 2003 Prentice Hall, Inc. All rights reserved.
11
45 46
1 // Fig. 1.6: fig01_06.cpp
1.22 Another Simple Program: 2 // Addition program.
Outline
3 #include <iostream>
Adding Two Integers 4
fig01_06.cpp
5 // function main begins program execution
6 int main()
(1 of 1)
• Input stream object 7 { Declare integer variables.
8 int integer1; // first number to be input by user
– >> (stream extraction operator) 9 int integer2; // second number to be input by user
10 int sum; Usewhich
// variable in stream extraction
sum will be stored
• Used with std::cin operator with standard input
11
• Waits for user to input value, then press Enter (Return) key 12 stream to obtain
std::cout << "Enter first integer\n"; user input.
// prompt
13 std::cin >> integer1; // read an integer
• Stores value in variable to right of operator 14
15 std::cout << "Enter second integer\n"; // prompt
– Converts value to variable data type
16 std::cin >> integer2; Calculations can be performed
// read in output
an integer statements: alternative
Stream manipulator
• = (assignment operator) 17
18 sum = integer1 + integer2;
for lines 18 and 20:
// assign result to sum
std::endl outputs a
– Assigns value to variable 19 std::cout << "Sum is " <<
newline, then “flushes output
integer1 + integer2 << std::endl;
20 std::cout << "Sum is " << sum << std::endl; // print sum buffer.”
– Binary operator (two operands) 21
22 return 0; // indicate that program ended successfully
– Example: 23
sum = variable1 + variable2; 24 } // end function main Concatenating, chaining or
cascading stream insertion
operations.
47 48
Enter first integer
45
Outline
Enter second integer 1.23 Memory Concepts
72
fig01_06.cpp
Sum is 117
output (1 of 1)
• 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
– Reading variables from memory nondestructive
12
49 50
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
51 52
1.25 Decision Making: Equality and
1.24 Arithmetic
Relational Operators
• Rules of operator precedence • if structure
– Operators in parentheses evaluated first – Make decision based on truth or falsity of condition
• Nested/embedded parentheses • If condition met, body executed
– Operators in innermost pair first • Else, body not executed
– Multiplication, division, modulus applied next • Equality and relational operators
• Operators applied from left to right
– Equality operators
– Addition, subtraction applied last • Same level of precedence
Operator(s) • Operators applied fromOrder
Operation(s) leftoftoevaluation
right (precedence)
– Relational operators
() Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first. If • Same level of precedence
there are several pairs of parentheses “on the same level”
(i.e., not nested), they are evaluated left to right. – Associate left to right
*, /, or % Multiplication Division Evaluated second. If there are several, they re
Modulus evaluated left to right.
+ or - Addition Evaluated last. If there are several, they are
Subtraction evaluated left to right.
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
13
53 54
1.25 Decision Making: Equality and 1.25 Decision Making: Equality and
Relational Operators Relational Operators
Equality operators
= == x == y x is equal to y
≠ != x != y x is not equal to y
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
55 56
1 // Fig. 1.14: fig01_14.cpp 26 if ( num1 < num2 )
2 // Using if statements, relational
Outline 27 cout << num1 << " is less than " << num2 << endl;
Outline
3 // operators, and equality operators. 28
4 #include <iostream> 29 if ( num1 > num2 )
fig01_14.cpp fig01_14.cpp
5 30 cout << num1 << " is greater than " << num2 << endl;
(1 of 2) Statements may
(2 of 2) be split over
6 using std::cout; // program uses cout 31
7 using std::cin; // program uses cin 32 if ( num1 <= num2 )
several lines.
using statements eliminate
8 using std::endl; // program uses endl 33 cout << num1 << " is less than or equal to " fig01_14.cpp
need for std:: prefix.
9 34 << num2 << endl; output (1 of 2)
10 // function main begins programDeclare variables.
execution 35
11 int main() 36 if ( num1 >= num2 )
12 { 37 cout << num1 << " is greater than or equal to "
13 Can write
int num1; // first number to be readand
cout cin
from user 38 << num2 << endl;
14 withouttostd::
int num2; // second number prefix.
be read from user 39
15 40 return 0; // indicate that program ended successfully
16 cout << "Enter two integers, and I will tell you\n" 41
17
if structure compares values
<< "the relationships they satisfy: "; 42 } // end function main
18 cin >> num1 >> num2; of num1
// read and num2
two integers to test for
If condition is true (i.e.,
19 equality. values are equal), execute this Enter two integers, and I will tell you
20 if ( num1 == num2 ) if structure compares
[Link] the relationships they satisfy: 22 12
21 of num1
cout << num1 << " is equal to " << If condition
andnum2
num2 << to is true
test for
endl; (i.e., 22 is not equal to 12
22 inequality. values are not equal), execute 22 is greater than 12
23 if ( num1 != num2 ) this statement. 22 is greater than or equal to 12
24 cout << num1 << " is not equal to " << num2 << endl;
25
14
57 58
Enter two integers, and I will tell you
the relationships they satisfy: 7 7
Outline 1.26 Thinking About Objects: Introduction to
7 is equal to 7
7 is less than or equal to 7
Object Technology and the Unified Modeling
fig01_14.cpp
7 is greater than or equal to 7
output (2 of 2) Language
• Object oriented programming (OOP)
– Model real-world objects with software counterparts
– Attributes (state) - properties of objects
• Size, shape, color, weight, etc.
– Behaviors (operations) - actions
• A ball rolls, bounces, inflates and deflates
• Objects can perform actions as well
– Inheritance
• New classes of objects absorb characteristics from existing classes
– Objects
• Encapsulate data and functions
• Information hiding
– Communicate across well-defined interfaces
© 2003 Prentice Hall, Inc.
All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
59 60
1.26 Thinking About Objects: Introduction to 1.26 Thinking About Objects: Introduction to
Object Technology and the Unified Modeling Object Technology and the Unified Modeling
Language Language
• User-defined types (classes, components) • Object-oriented analysis and design (OOAD)
– Data members process
• Data components of class – Analysis of project’s requirements
– Member functions – Design for satisfying requirements
• Function components of class – Pseudocode
– Association • Informal means of expressing program
– Reuse classes • Outline to guide code
© 2003 Prentice Hall, Inc. All rights reserved. © 2003 Prentice Hall, Inc. All rights reserved.
15
61
1.26 Thinking About Objects: Introduction to
Object Technology and the Unified Modeling
Language
• Unified Modeling Language (UML)
– 2001: Object Management Group (OMG)
• Released UML version 1.4
– Model object-oriented systems and aid design
– Flexible
• Extendable
• Independent of many OOAD processes
• One standard set of notations
– Complex, feature-rich graphical language
16