In this chapter we studied how classes were implemented, with an in-depth look at the class Dice. Member functions of classes are categorized as constructors, accessor, and mutator functions; private...


In this chapter we studied how classes were implemented, with an in-depth look at the class Dice. Member functions of classes are categorized as constructors, accessor, and mutator functions; private data makes up the state of a class. We studied different modes of parameter/argument passing. We saw how (relatively) simple it is to read and write text files in C++ because of the similarity of file streams to cin and cout. We saw a pattern of iteration using functions Init, HasMore, and Next used with both streams and with sets of strings. The pattern was used to permit programs to access the elements of a collection without real knowledge of how the collection is implemented. By using the same names for iterator functions, we’ll make it easier to understand new iterators when we encounter them. We also studied how to solve extreme problems; such as finding the maximum and minimum in a collection. Important topics covered include the following:


Accessor and mutator functions allow a class’ state to be examined and changed, respectively.


Private instance variables are accessible only in member functions, not in client programs.


Coupling and cohesion are important criteria for evaluating functions, classes, and programs.


Reference parameters permit values to be returned from functions via parameters.


This allows more than one value to be returned. Const reference parameters are used for efficiency and safety.


Parameters are passed by value (a copy is made) unless an ampersand, &, is used for pass by reference. In this case the formal parameter identifier is an alias for the memory associated with the associated function argument.


A variable is defined when storage is allocated. A variable is declared if no storage is allocated, but the variable’s type is associated with the variable’s identifier.


Parameters for programmer-defined classes are often declared as const reference parameters to save time and space while ensuring safety.


Programs are best designed in an iterative manner, ideally by developing a working program and adding pieces to it so that the program is always functional to some degree. Writing pseudocode first is often a good way of starting the process of program development.


The extraction operator, >>, uses white space to delimit, or separate, one string from another.


In sentinel loops, the sentinel value is not considered part of the data.


The extraction operator returns a value that can be tested in a loop to see whether the extraction succeeds, so while (cin » word) is a standard idiom for reading streams until there is no more data (or until the extraction fails). The stream member function fail can be used too.


Files can be associated with streams using ifstream variables. The extraction operator works with these streams. The ifstream member function open is used to bind a named disk file to a file stream. An ofstream variable is used to associate an output file stream with a named disk file.


If you enter a nonnumeric value when a numeric value (e.g., an int or a double) is expected, the extraction will fail and the nonnumeric character remains unprocessed on the input stream.


Types sometimes need to be cast, or changed, to another type. Casting often causes values to change; that is when casting from adoubleto an int, truncation occurs.


A new cast operator, static_cast, should be used if your compiler supports it.


Constants for the largest int and double values are accessible and can be found in the header files and , respectively. The constants defining system extreme values are INT_MAX, INT_MIN, LONG_MAX, LONG_MIN, DBL_MAX, and DBL_MIN.

Finding extreme (highest and lowest) values is a typical fence post problem. Initializing with the first value is usually a good approach, but sometimes a value of “infinity” is available for initialization (e.g., INT_MAX).


The class CTimer can be used to time program segments. The granularity of it’s underlying clock may differ among different computers.


The WordStreamIterator class encapsulates file-reading so that the same file can be easily read many times within the same program.


The StringSet class is used to represent sets of strings (no duplicates). An associated class StringSetIterator allows access to each value in a set.


Nov 19, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here