Test 2 Review
Contents
type conversions
- implicit/automatic type conversions
- one implicit conversion for each argument
- casting (explicit type conversions)
- AخA
static_cast<new_type>(expression)
- traditional casting (C-style)
-
(new_type) expression
new_type (expression)
-
- other *_cast operators are available in C++ but will not be covered on this test
- Example:
using namespace std;
void fn(int)
{
cout << "fn(int)" << endl;
}
void fn(double)
{
cout << "fn(double)" << endl;
}
int main()
{
int arg = 12;
fn(arg);
fn(static_cast<double>(arg));
fn(static_cast<double>(12));
fn(double(arg));
fn((double)arg);
return 0;
}
Streams (File I/O, stringstream)
- We have used stream extraction operator >> and the stream insertion operator << with
andcin
cout
- file I/O and
can be used these stream operatorsstringstream
- difference between cout/cin and file I/O is primarily with the object creation
- must open a file before reading/writing:
x
ofstream output; // create file output streams, called out1 and bob
output.open("outfile1.txt"); // for ofstreams, these calls create
output << "hello world" << endl;
- check if
succeedsopen()
when finished (close()
implicitly called when stream object goes out of scopeclose()
- append mode:
output.open("file.txt", ios::app); // open file in append mode
- lecture notes
- Example:
using namespace std;
int from_str(const string &s)
{
istringstream iss(s);
int rtn = 0;
iss >> rtn;
return rtn;
}
int main()
{
ifstream input("numbers.txt");
if (!input) {
// open failed
cerr << "input file could not be opened";
return EXIT_FAILURE;
}
int sum = 0;
for (string line; getline(input, line);) {
// be aware that no error checking is done on the input
// (e.g., non-numeric characters)
sum += from_str(line);
}
ofstream output("output.txt");
if (!output) {
cerr << "output file could not be opened";
return EXIT_FAILURE;
}
output << "sum: " << sum << endl;
return 0;
}
Stream Formatting
- functions and flags for controlling output formatting
- E.g.,
andwidth()
setw()
- E.g.,
is a stream manipulator that prints a newline and flushes the output buffer (e.g., write buffered characters to the screen)endl
causes the output buffer to be flushed to the output device before processing proceedsflush
- other common formatting features can be found in the leture notes and your assignment #4 code
Pointers
- description of values stored in/read from pointer variables
- declaration syntax
- dereference operator (*), address-of operator (&), subscripting operator ([])
- recall that as with other operators these also have associativity and precedence
- pointer arithmetic
- passing to/from functions
const
Arrays
- what is an array?
- description of values stored in/read from arrays
- declaration syntax
unary operators*, &, and []
- indexing into arrays
- similarities/differences to pointers
- passing to/from functions
const
memory allocation with new and delete
- array allocation
- similarities/differences to arrays
- passing allocations to/from functions
const
algorithms
- given an algorithm written in pseudo-code, convert it to reasonably valid C++ code
- create simple algorithms and write it in pseudo-code
- Example: selection sort
- input - array of numbers
- output (result) - input list is sorted
- steps
- find smallest number in input array and swap it with the 1st element
- find 2nd smallest number in input array and swap it with 2nd element
- repeatedly find ith smallest number in input array and swap it with the ith array element until (i-1)th element is placed