chapter summaries

8
Chapter Summaries Chapter 1 Summary * The collection of programs used by a computer is referred to as the software for that computer. The actual physical machines that make up a computer installation are referred to as hardware. * The five main components of a computer are: the input device, the output device, the central processing unit, the main memory, and the secondary memory. * A computer has two kinds of memory: main memory and secondary memory. Main memory is only used while the program is running. Secondary memory is used to hold data that will stay in the computer before and after the program is run. * A computer‟s main memory is divided into a series of numbered locations called bytes. The number associated with one of the bytes is called the address of the byte. Often several of these bytes are grouped together to form a larger memory location. In that case, the address of the first byte is used as the address of this larger memory location. * A byte consists of eight binary digits, each either zero of one. A digit that can only be a zero or one is called a bit. * A compiler is a program that translates a program written in a high-level language like C++ into a program written in the machine language which the computer can directly understand and execute. * A sequence of precise instructions that leads to a solution is called an algorithm. Algorithms can be written in English or in a programming language, like C++. However, the word algorithm is usually used to mean a sequence of instructions written in English. * Before writing a C++ program, you should design the algorithm that the program will use. * Programming errors can be classified into three groups: syntax errors, run-time errors, and logic errors. The computer will usually tell you about errors in the first two categories. You must discover logic errors yourself. * The individual instructions in a C++ program are called statements. * A variable in a C++ program can be used to name a number. * A statement in a C++ program that begins with cout << is an output statement which tells the computer to output to the screen whatever follows the <<. * A statement in a C++ program that begins with cin >> is an input statement. *Chapter 2 Summary* * Use meaningful names for variables. * Be sure to check that variables are declared to be of the correct data type. * Be sure that variables are initialized before the program attempts to use their value. This can be done when the variable is declared or with an assignment statement before the variable is first used. * Use enough parentheses in arithmetic expressions to make the order

Upload: omerqas

Post on 01-May-2017

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter Summaries

Chapter Summaries

Chapter 1 Summary

* The collection of programs used by a computer is referred to as the

software for that computer. The actual physical machines that make

up a computer installation are referred to as hardware.

* The five main components of a computer are: the input device, the

output device, the central processing unit, the main memory, and the

secondary memory.

* A computer has two kinds of memory: main memory and secondary

memory. Main memory is only used while the program is running.

Secondary memory is used to hold data that will stay in the computer

before and after the program is run.

* A computer‟s main memory is divided into a series of numbered

locations called bytes. The number associated with one of the bytes

is called the address of the byte. Often several of these bytes are

grouped together to form a larger memory location. In that case, the

address of the first byte is used as the address of this larger

memory location.

* A byte consists of eight binary digits, each either zero of one. A

digit that can only be a zero or one is called a bit.

* A compiler is a program that translates a program written in a

high-level language like C++ into a program written in the machine

language which the computer can directly understand and execute.

* A sequence of precise instructions that leads to a solution is

called an algorithm. Algorithms can be written in English or in a

programming language, like C++. However, the word algorithm is

usually used to mean a sequence of instructions written in English.

* Before writing a C++ program, you should design the algorithm that

the program will use.

* Programming errors can be classified into three groups: syntax

errors, run-time errors, and logic errors. The computer will usually

tell you about errors in the first two categories. You must discover

logic errors yourself.

* The individual instructions in a C++ program are called statements.

* A variable in a C++ program can be used to name a number.

* A statement in a C++ program that begins with cout << is an output

statement which tells the computer to output to the screen whatever

follows the <<.

* A statement in a C++ program that begins with cin >> is an input

statement.

*Chapter 2 Summary*

* Use meaningful names for variables.

* Be sure to check that variables are declared to be of the correct

data type.

* Be sure that variables are initialized before the program attempts

to use their value. This can be done when the variable is declared

or with an assignment statement before the variable is first used.

* Use enough parentheses in arithmetic expressions to make the order

Page 2: Chapter Summaries

of operations clear.

* Always include a prompt line in a program whenever the user is

expected to enter data from the keyboard, and always echo the user‟s

input.

* An if-else-statement allows your program to choose one of two

alternate actions. An if-statement allows your program to decide

whether or not to perform some one particular action.

* A do-while-loop always executes its loop body at least once. in some

situations a while-loop might not execute the body of the loop at

all.

* Almost all number constants in a program should be given meaningful

names that can be used in place of the numbers. This can be done by

using the modifier const in a variable declaration.

* Use an indenting, spacing and line break pattern similar to the

sample programs.

* Insert comments to explain major subsections or any unclear part of

a program.

*Chapter 3 Summary*

* A good plan of attack for designing the algorithm for a program is

to break down the task to be accomplished into a few subtasks, then

decompose each subtask into smaller subtasks, and so forth until the

subtasks are simple enough that they can easily be implemented as

C++ code. This approach is called top-down-design.

* A function that returns a value is like a small program. The

arguments to the function serve as the input to this “small program”

and the value returned is like the output of the “small program.”

* When a subtask for a program takes some values as input and produces

a single value as its only result, then that subtask can be

implemented as a function.

* A function should be defined so that it can be used as a black box.

The programmer who uses the function should not need to know any

details about how the function is coded. All the programmer should

need to know is the function prototype and the accompanying comment

that describes the value returned. This rule is sometimes called the

principle of procedural abstraction.

* A variable that is declared in a function definition is said to be

local to the function.

* Global named constants are declared using the const modifier.

Declarations for global named constants are normally placed at the

start of a program after the include directives and before the

function prototypes.

* Call-by-value formal parameters are variables that are local to the

function. Occasionally it is useful to use a formal parameter as a

local variable.

* When you have two or more function definitions for the same function

name, that is called overloading the function name. When you

overload a function name, the function definition must have

different numbers of formal parameters or some formal parameters of

different types.

Page 3: Chapter Summaries

*Chapter 4 Summary*

* All subtasks in a program can be implemented as functions, either as

functions that return a value or as void-functions.

* A formal parameter is a kind of place holder that is filled in with

a function argument when the function is called. There are two

methods of performing this substitution: call-by-value and

call-by-reference.

* In the call-by-value substitution mechanism, the value of an

argument is substituted for its corresponding formal parameter. In

the call-by-reference substitution mechanism, the argument should be

a variable and the entire variable is substituted for the

corresponding argument.

* The way to indicate a call-by-reference parameter in a function

definition is to attach the ampersand sign & to the type of the

formal parameter.

* An argument corresponding to a call-by-value parameter cannot be

changed by a function call. An argument corresponding to a

call-by-reference parameter can be changed by a function call. If

you want a function to change the value of a variable, then you must

use a call-by-reference parameter.

* A good way to write a function prototype comment is to use a

precondition and a post condition. The precondition states what is

assumed to be true when the function is called. The postcondition

describes the effects of the function call; that is, the

postcondition tells what will be true after the function is executed

in a situation in which the precondition holds.

* Every function should be tested in a program in which every other

function in that program has already been full tested and debugged.

* A driver program is a program that does nothing but test a function.

* A simplified version of a function is called a stub. A stub is used

in place of a function definition that has not yet been tested (or

written) so that the rest of the program can be tested.

*Chapter 5 Summary*

* A stream of type ifstream can be connected to a file with a call to

the member function open. Your program can then take input from that

file./ifstream fin; fin.open(“infile.dat”);/

* A stream of type ofstream can be connected to a file with a call to

the member function open. Your program can then send output to that

file. /ofstream fout; fout.open(“out.dat”);/

* You should use the member function fail to check whether a call to

open was successful. /if (fin.fail( ))/ {cout << “Fail to open”;

exit(1);}

* An object is a variable that has functions associated with it. These

functions are called member functions. A class is a type whose

variables are objects. A stream is an example of an object. The

types ifstream and ofstream are examples of classes.

* The following is the syntax you use when you write a call to a

member function of an object:

/Calling_Object.Member_Function_Name(Argument_List);/

Page 4: Chapter Summaries

An example with the stream cout as the calling object and precision as

the member function is the following: /cout.precision(2);/

* Stream member functions such as width, setf, and precision can be

used to format output. These output functions work the same for the

stream cout, which is connected to the screen, and for output

streams connected to files.

* A function may have formal parameters of a stream type, but they

must be call-by-reference parameters. They cannot be call-by-value

parameters. The type ifstream can be used for an input-file stream

and the type ofstream can be used for an output-file stream.

* If you use istream (spelled without the „f‟) as the type for an

input stream parameter, then the argument corresponding to that

formal parameter can be either the stream cin or an input-file

stream of type ifstream. If you use ostream (spelled without the

„f‟) as the type of an output stream parameter, then the argument

corresponding to the formal parameter can be either the stream cout

or an output-file stream of type ofstream.

* Every input stream has a member function named get that can be used

to read one character of input. The member function get will not

skip over whitespace. Every output stream also has a member function

named put that can be used to write one character to the output

stream.

* The member function eof can be used to test when a program has

reached the end of an input file. The member function eof works well

for text processing. However, when processing numeric data, you

might prefer to test for the end of a file by using the other

methods we discussed in this chapter. /while (! fin.eof( )) {…}/

* Function parameters can have default arguments that provide values

for parameters if the corresponding argument is omitted in the call.

These arguments must follow any parameters that are not provided

default arguments. Calls to such a function must supply arguments

for parameters without default arguments first. Arguments beyond

this are used instead of defaults, up to the number of parameters

the function has.

For example:

/void new_line(istream& instream_par = cin)/

/{/

/ char symbol;/

/ do/

/ {/

/instream_par.get(symbol);/

/ } while (symbol != „|n‟);/

Page 5: Chapter Summaries

/ }/

/ /

If we call this function as /new_line( );/ the formal

parameter takes the default argument /cin/. If we call this as

/new_line(fin);/ the formal parameter takes the argument provided in the

call /fin/.

* Chapter 7 Summary*

* Boolean expressions are evaluated similar to the way arithmetic

expressions are evaluated

* Most modern compilers have a bool type having the values true and

false. There are still compilers in use that do not implement the

full ANSI Standard C++. With these older compilers, true is

represented as 1 and false is represented as 0.

* You can write a function so that it returns a value of true or

false. A call to such a function can be used as a Boolean expression

in an if-else-statement or anywhere else that a Boolean expression

is permitted.

* One approach to solving a task or subtask is to write down

conditions and corresponding actions that need to be taken under

each condition. This can be implemented in C++ as a multiway

if-else-statement.

* A switch-statement is a good way to implement a menu for the user of

your program.

* Use function calls in multiway branch statements, such as

switch-statements and multiway if-else-statements.

* A block is a compound statement that contains variable declarations.

The variables declared in a block are local to the block. Among

other uses, blocks can be used for the action in one branch of a

multiway branch statement, such as a multiway if-else-statement.

* A for-loop can be used to obtain the equivalent of the instructions

“repeat the loop body n times.” /for (n = 1; n<= 10; n++)/

* There are four commonly used methods for terminating an input loop:

list headed by size, ask before iterating, list ended with sentinel

value, and running out of input.

* It is usually best to design loops in pseudocode that does not

specify a choice of C++ looping mechanism. Once the algorithm has

been designed, the choice of which C++ loop statement to use is

usually clear.

* One way to simplify your reasoning about nested loops is to make the

loop body a function call.

* Always check loops to be sure that the variables used by the loop

are properly initialized before the loop begins.

* Always check loops to be certain they are not iterated one too many

or one too few times.

* When debugging loops, it helps to trace key variables in the loop

body.

* If a program or algorithm is very difficult to understand or

performs very poorly, do not try to fix it. Instead, throw it away

and start over.

Page 6: Chapter Summaries

*Chapter 6 Summary*

* A structure can be used to combine data of different types into a

single (compound) data value.

* A class can be used to combine data and functions into a single

(compound) object.

* A member variable or a member function for a class may be either

public or private. If it is public, it can be used outside of the

class. If it is private it can be used only in the definition of

another member function.

* A function may have formal parameters of a class or structure type.

A function may return values of a class or structure type.

* A member function for a class can be overloaded in the same way as

ordinary functions are overloaded.

* A constructor is a member function of a class that is called

automatically when an object of the class is declared. A constructor

must have the same name as the class of which it is a member

* A data type consists of a collection of values together with a set

of basic operations defined on these values.

* A data type is called an abstract data type if a programmer who uses

the type does not need to know any of the details about how the

values and operations for that type are implemented.

* One way to implement an abstract data type in C++ is to define a

class with all member variables private and with the operations

implemented as public member functions.

*Chapter 9 Summary*

* An array can be used to store and manipulate a collection of data

that is all of the same type.

* The indexed variables of an array can be used just like any other

variables of the base type of the array.

* A for-loop is a good way to step through the elements of an array

and perform some program action on each indexed variable.

* The most common programming error made when using arrays is

attempting to access a nonexistent array index. Always check the

first and last iterations of a loop that manipulates an array to

make sure it does not use an index that is illegally small or

illegally large.

* An array formal parameter is neither a call by value parameter nor a

call by reference parameter, but a new kind of parameter. An array

parameter is similar to a call by reference parameter in that any

change that is made to the formal parameter in the body of the

function will be made to the array argument when the function is

called.

* The indexed variables for an array are stored next to each other in

the computer‟s memory so that the array occupies a contiguous

portion of memory. When the array is passed as an argument to a

function, only the address of the first indexed variable is given to

the calling function. Therefore, a function with an array parameter

usually needs another formal parameter of type int to give the size

of the array.

* When using a partially filled array your program needs an additional

variable of type int to keep track of how much of the array is being

Page 7: Chapter Summaries

used.

* To tell the compiler that an array argument should not be changed by

your function, you can insert the modifier const before the array

parameter for that argument position. An array parameter that is

modified with a const is called a constant array parameter.

* The base type of an array can be a structure or class type. A

structure or class can have an array as a member variable.

*Chapter 10 Summary*

* A cstring variable is the same thing as an array of characters, but

it is used in a slightly different way. A string variable uses the

null character '\0' to mark the end of the string stored in the

array.

* cstring variables usually must be treated like arrays, rather than

simple variables of the kind we used for numbers and single

characters. In particular, you cannot assign a cstring value to a

cstring variable using the equal sign =, and you cannot compare the

values in two cstring variables using the == operator. Instead you

must use special cstring functions to perform these tasks.

* When you define a function that changes the value in a cstring

variable, your function should have an additional int parameter for

the declared size of the cstring variable. That way the function can

check to make sure it does not attempt to place more characters in

the cstring variable than the cstring variable can hold. The

predefined functions in the library with the header file cstring do

not have such a parameter, so extra care must be exercised when

using many of them.

* A very robust input function that will read anything the user types

in must read the input as a string of characters. If numeric input

is desired, the string of digits that is read in will need to be

converted to a number.

* If you need an array with more than one index, you can us a

multidimensional array, which is actually an array of arrays.

* An array of strings can be implemented as a two-dimensional array of

characters.

* The ANSI Standard string library provides a fully featured string

class.

*Chapter 11 Summary*

* A pointer is a memory address, so a pointer provides a way to

indirectly name a variable by naming the address of the variable in

the computer's memory.

* Dynamic variables are variables that are created and destroyed while

a program is running

* Memory for dynamic variables is in a special portion of the

computer's memory called the heap. When a program is finished with a

dynamic variable, the memory used by the dynamic variable an be

returned to the heap for reuse; this is done with a delete statement.

* A dynamic array is an array whose size is determined when the

program is running. A dynamic array is implemented as a dynamic

variable of an array type.

* A destructor is a special kind of member function for a class. A

Page 8: Chapter Summaries

destructor is called automatically when an object of the class

passes out of scope. The main reason for destructors is to return

memory to the heap so the memory can be reused.

* A copy constructor is a constructor that has a single argument that

is of the same type as the class. If you define a copy constructor

it will be called automatically whenever a function returns a value

of the class type and whenever an argument is plugged in for a call

by value parameter of the class type. Any class that uses pointers

and the operator new should have a copy constructor.