Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Chapter 1
Introduction to Computers and C++ Programming
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Overview
1.1 Computer Systems
1.2 Programming and Problem Solving
1.3 Introduction to C++
1.4 Testing and Debugging
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
1.1
Computer Systems
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Computer Systems
n A computer program is…n A set of instructions for a computer to follow
n Computer software is …n The collection of programs used by a computer
n Includes:n Editorsn Translatorsn System Managers
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Hardware
n Three main classes of computers
n PCs (Personal Computer)n Relatively small used by one person at a time
n Workstationn Larger and more powerful than a PC
n Mainframen Still largern Requires support staffn Shared by multiple users
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Networks
n A number of computers connected to share resourcesn Share printers and other devicesn Share information
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Computer Organization
n Five main componentsn Input devices
n Allows communication to the computer
n Output devicesn Allows communication to the user
n Processor (CPU)
n Main memoryn Memory locations containing the running program
n Secondary memoryn Permanent record of data often on a disk Display 1.1
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Computer Memory
n Main Memoryn Long list of memory locations
n Each contains zeros and onesn Can change during program execution
n Binary Digit or Bitn A digit that can only be zero or one
n Byten Each memory location has eight bits
n Address n Number that identifies a memory location
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Larger Data Items
n Some data is too large for a single byten Most integers and real numbers are too large
n Address refers to the first byte
n Next few consecutive bytes can store the additionalbits for larger data
Display 1.2
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Data or Code?
n ‘A’ may look like 01000001n 65 may look like 01000001n An instruction may look like 01000001
n How does the computer know the meaningof 01000001?n Interpretation depends on the current instruction
n Programmers rarely need to be concerned with this problem.n Reason as if memory locations contain letters and
numbers rather than zeroes and ones
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Secondary Memory
n Main memory stores instructions and data while a program is running.
n Secondary memoryn Stores instructions and data between sessionsn A file stores data or instructions in
secondary memory
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Secondary Memory Media
n A computer might have any of thesetypes of secondary memoryn Hard disk
n Fastn Fixed in the computer and not normally removed
n Floppy diskn Slown Easily shared with other computers
n Compact diskn Slower than hard disksn Easily shared with other computersn Can be read only or re-writable
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Memory Access
n Random Access n Usually called RAM
n Computer can directly access any memory location
n Sequential Accessn Data is generally found by searching through
other items firstn More common in secondary memory
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
The Processor
n Typically called the CPUn Central Processing Unitn Follows program instructions n Typical capabilities of CPU include:
addsubtractmultiplydividemove data from location to location
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Computer Software
n The operating system n Allows us to communicate with the computern Is a program n Allocates the computer’s resourcesn Responds to user requests to run other
programs
n Common operating systems include…n UNIX Linux DOS
Windows Macintosh VMS
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Computer Input
n Computer input consists of n A program
n Some data
Display 1.3
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
High-level Languages
n Common programming languages include …
C C++ Java Pascal Visual Basic FORTRAN PerlPHP Lisp Scheme Ada C# Python
n These high – level languages n Resemble human languagesn Are designed to be easy to read and writen Use more complicated instructions than
the CPU can follown Must be translated to zeros and ones for the CPU
to execute a program
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Low-level Languages
n An assembly language command such as
ADD X Y Z
might mean add the values found at x and y in memory, and store the result in location z.
n Assembly language must be translated to machine language (zeros and ones)
0110 1001 1010 1011n The CPU can follow machine language
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Compilers
n Translate high-level language to machine language
n Source coden The original program in a high level language
n Object coden The translated version in machine language
Display 1.4
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Linkers
n Some programs we use are already compiledn Their object code is available for us to usen For example: Input and output routines
n A Linker combinesn The object code for the programs we write
andn The object code for the pre-compiled routines
inton The machine language program the CPU can
runDisplay 1.5
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
History Note
n First programmable computern Designed by Charles Babbagen Began work in 1822n Not completed in Babbage’s life time
n First programmern Ada Augusta, Countess of Lovelace
n Colleague of Babbage
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Section 1.1 Conclusion
n Can you…n List the five main components of a computer?
n List the data for a program that adds two numbers?
n Describe the work of a compiler?
n Define source code? Define object code?
n Describe the purpose of the operating system?
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
1.2
Programming and Problem-Solving
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Algorithms
n Algorithmn A sequence of precise instructions that
leads to a solution
n Programn An algorithm expressed in a language the
computer can understand
Display 1.6
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Program Design
n Programming is a creative processn No complete set of rules for creating a program
n Program Design Processn Problem Solving Phase
n Result is an algorithm that solves the problemn Implementation Phase
n Result is the algorithm translated into a programminglanguage
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Problem Solving Phase
n Be certain the task is completely specifiedn What is the input? n What information is in the output? n How is the output organized?
n Develop the algorithm before implementationn Experience shows this saves time in getting
your program to run.n Test the algorithm for correctness
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Implementation Phase
n Translate the algorithm into a programming languagen Easier as you gain experience with the language
n Compile the source coden Locates errors in using the programming language
n Run the program on sample datan Verify correctness of results
n Results may require modification of the algorithm and program
Display 1.7
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Object Oriented Programming
n Abbreviated OOP
n Used for many modern programs
n Program is viewed as interacting objectsn Each object contains algorithms to describe
its behaviorn Program design phase involves designing
objects and their algorithms
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
OOP Characteristics
n Encapsulationn Information hidingn Objects contain their own data and algorithms
n Inheritancen Writing reusable coden Objects can inherit characteristics from other objects
n Polymorphismn A single name can have multiple meanings depending
on its context
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Software Life Cycle
n Analysis and specification of the task (problem definition)
n Design of the software (object and algorithm design)
n Implementation (coding)n Maintenance and evolution of the systemn Obsolescence
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Section 1.2 Conclusion
n Can you…n Describe the first step to take when creating
a program?
n List the two main phases of the program design process?
n Explain the importance of the problem-solving phase?
n List the steps in the software life cycle?
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
1.3
Introduction to C++
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Introduction to C++
n Where did C++ come from?n Derived from the C languagen C was derived from the B languagen B was derived from the BCPL language
n Why the ‘++’?n ++ is an operator in C++ and results in a cute pun
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
C++ History
n C developed by Dennis Ritchie at AT&TBell Labs in the 1970s.n Used to maintain UNIX systemsn Many commercial applications written in c
n C++ developed by Bjarne Stroustrup at AT&TBell Labs in the 1980s.n Overcame several shortcomings of Cn Incorporated object oriented programmingn C remains a subset of C++
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
A Sample C++ Program
n A simple C++ program begins this way
#include <iostream>using namespace std;
int main(){
n And ends this way
return 0;}
Display 1.8
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Explanation of code (1/5)
n Variable declaration line
int number_of_pods, peas_per_pod, total_peas;
n Identifies names of three variables to name numbersn int means that the variables represent integers
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Explanation of code (2/5)
n Program statement
cout << “Press return after entering a number.\n”;
n cout (see-out) used for output to the monitor
n “<<“ inserts “Press…a number.\n” in the databound for the monitor
n Think of cout as a name for the monitorn “<<“ points to where the data is to end up
n ‘\n’ causes a new line to be started on the monitor
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Explanation of code (3/5)
n Program statement
cin >> number_of_pods;
n cin (see-in) used for input from the keyboard
n “>>” extracts data from the keyboard
n Think of cin as a name for the keyboardn “>>” points from the keyboard to a variable where the data
is stored
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Explanation of code (4/5)
n Program statement
total_peas = number_of_pods * peas_per_pod;
n Performs a computationn ‘*’ is used for multiplicationn ‘=‘ causes total_peas to get a new value based on
the calculation shown on the right of the equal sign
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Explanation of code (5/5)
n Program statement
cout << number_of_pods;
n Sends the value of variable number_of_pods to the monitor
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Program Layout (1/3)
n Compiler accepts almost any pattern of linebreaks and indentation
n Programmers format programs so they are easy to readn Place opening brace ‘{‘ and closing brace ‘}’
on a line by themselvesn Indent statements n Use only one statement per line
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Program Layout (2/3)
n Variables are declared before they are usedn Typically variables are declared at the beginning of
the programn Statements (not always lines) end with a semi-colon
n Include Directives#include <iostream>n Tells compiler where to find information about items
used in the programn iostream is a library containing definitions of cin and
cout
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Program Layout (3/3)
n using namespace std;n Tells the compiler to use names in iostream in
a “standard” way
n To begin the main function of the programint main(){
n To end the main functionreturn 0;
}n Main function ends with a return statement
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Running a C++ Program
n C++ source code is written with a text editor
n The compiler on your system converts source code to object code.
n The linker combines all the object codeinto an executable program.
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
C++11
n C++11 (formerly known as C++0x) is the most recent version of the standard of the C++ programming language.n Approved on August 12, 2011 by the International
Organization for Standardization.n C++11 language features are not supported by older
compilersn Check the documentation with your compiler to determine
if special steps are needed to compile C++11 programsn e.g. with g++, use extra flags of –std=c++11
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Run a Program
n Obtain code in Display 1.10n Compile the coden Fix any errors the compiler indicates and
re-compile the coden Run the program n Now you know how to run a program on
your system
Display 1.10
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Section 1.3 Conclusion
n Can you…
n Describe the output of this line?
cout << “C++ is easy to understand.”;
n Explain what this line does?
cin >> peas_per_pod;
n Explain this? #include <iostream>
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
1.4
Testing and Debugging
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Testing and Debugging
n Bugn A mistake in a program
n Debuggingn Eliminating mistakes in programsn Term used when a moth caused a failed relay
on the Harvard Mark 1 computer. Grace Hopper and other programmers taped the moth in logbook stating:
“First actual case of a bug being found.”
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Program Errors
n Syntax errorsn Violation of the grammar rules of the languagen Discovered by the compiler
n Error messages may not always show correct location of errors
n Run-time errorsn Error conditions detected by the computer at run-time
n Logic errorsn Errors in the program’s algorithmn Most difficult to diagnosen Computer does not recognize an error
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Section 1-4 Conclusion
n Can you…n Describe the three kinds of program errors?
n Tell what kind of errors the compiler catches?
n What kind of error is produced if you forget a punctuation symbol such as a semi-colon?
n Tell what type of error is produced when a program runs but produces incorrect results?
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Chapter 1 -- End
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Display 1.1 Back Next
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Display 1.2 Back Next
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Display 1.3 Back Next
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Display 1.4 Back Next
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Display 1.5 Back Next
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Display 1.6 Back Next
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Display 1.7 Back Next
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Display 1.8
NextBack
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Display 1.9 Back Next
Copyright © 2015 Pearson Education, Ltd.. All rights reserved.
Display 1.10 NextBack