Week 11 - Gemini Flashcards
(38 cards)
What is the main purpose of ‘output formatting’ in C++?
To control how data (numbers, strings, etc.) is displayed on an output stream (like the console or a file), ensuring it’s readable, aligned, and presented in a specific style (e.g., number of decimal places, base).
Which C++ header file must typically be included to use output manipulators that take arguments (e.g., setw
, setprecision
)?
<iomanip>
What is the difference between using endl
and '\n'
for newlines with cout
? What additional action does endl
perform?
Both endl
and '\n'
insert a newline character. However, endl
also flushes the output buffer, ensuring the output is immediately displayed. '\n'
typically does not force a flush on its own.
What is the purpose of the flush
manipulator (e.g., cout << flush;
)? In what scenario might it be particularly useful?
flush
forces the contents of the output buffer to be written to the output device immediately. It can be useful in debugging (to ensure print statements appear before a potential crash) or when immediate output is critical, though cerr
is often preferred for error messages as it’s unbuffered.
What is the cerr
output stream, and how does it differ from cout
in terms of buffering?
cerr
is the standard error stream, typically directed to the console. Unlike cout
, output to cerr
is usually unbuffered or flushed automatically after each insertion, making it suitable for error messages that should appear immediately.
Explain the setw(n)
manipulator. Does its effect persist for subsequent output operations?
setw(n)
sets the minimum field width for the next item to be printed to n
characters. If the item is shorter, it’s padded (usually with spaces). If longer, the field expands. Its effect is not persistent; it only applies to the immediately following output.
What is the default justification for output within a field set by setw(n)
? Which manipulators can change this?
The default justification is usually right-justified. std::left
changes it to left-justified. std::right
explicitly sets it back to right-justified. These justification manipulators are persistent.
What is the purpose of the showpos
and noshowpos
manipulators?
showpos
forces a plus sign (+
) to be displayed before positive numerical values. noshowpos
(the default) omits the plus sign for positive values (negative values always show a minus sign).
Which manipulators are used to change the numerical base for printing integer values? What are the common bases supported?
std::dec
(decimal - base 10, default), std::oct
(octal - base 8), and std::hex
(hexadecimal - base 16). These only affect integer output, not floating-point numbers.
Are floating-point values affected when std::oct
or std::hex
manipulators are active?
No, floating-point values are always printed in decimal format, regardless of the active integer base manipulator.
Describe the three floating-point output formats in C++ and the manipulators to select them.
- General (default): Mix of fixed and scientific; uses fixed for small numbers, scientific for large. 2. Fixed (
std::fixed
): Always displays a number, decimal point, and fractional part (e.g., 123.450). 3. Scientific (std::scientific
): Always displays in scientific notation (e.g., 1.234500e+02).
How can you revert the floating-point output format to ‘general’ after setting it to fixed
or scientific
?
Using cout.unsetf(std::ios::fixed | std::ios::scientific);
(requires using namespace std;
or qualifying ios
). There isn’t a single ‘general’ manipulator.
What does the setprecision(n)
manipulator control for floating-point numbers in (a) general format and (b) fixed or scientific format?
(a) General format: n
is the maximum number of significant digits displayed. (b) Fixed/Scientific format: n
is the number of digits displayed after the decimal point.
What is the default output precision for floating-point numbers if not set by setprecision
?
The default precision is typically 6.
What is the purpose of the showpoint
and noshowpoint
manipulators, especially in ‘general’ floating-point format?
showpoint
forces the decimal point and any trailing zeros (up to the current precision) to be displayed for floating-point numbers, even if they are whole numbers (e.g., 4.00000). noshowpoint
(default) omits unnecessary trailing zeros and the decimal point for whole numbers (e.g., 4).
What is a ‘namespace’ in C++, and what is its primary purpose?
A namespace provides a scope for identifiers (names of types, functions, variables, etc.). Its primary purpose is to prevent name collisions that can occur when different libraries or parts of a large project use the same identifier for different things.
How do you define a namespace MyLibrary
containing a function void func();
?
namespace MyLibrary { void func(); /* other declarations/definitions */ }
How can you access func()
from the MyLibrary
namespace? (Show two ways)
- Using the scope resolution operator:
MyLibrary::func();
2. With ausing
directive:using namespace MyLibrary; func();
(the latter brings all names fromMyLibrary
into the current scope).
Why is it generally discouraged to use using namespace std;
(or any other namespace) in header files?
Because header files can be included in many translation units. A using namespace
directive in a header file pollutes the global namespace for all files that include it, increasing the risk of name collisions and making it harder to see where names originate.
What are the two primary ways C++ handles strings, as mentioned in ‘Last_week.docx’?
- The C-style character string (a null-terminated array of characters). 2. The
std::string
class type (part of the C++ Standard Library).
What is the key characteristic of a C-style character string in terms of its storage in memory?
It is a one-dimensional array of characters that is terminated by a null character (\0
).
How does the std::string
class (from <string>
header) generally improve upon C-style strings?
std::string
objects manage their own memory, automatically handle resizing, provide many useful member functions (e.g., .size()
, .length()
, .append()
, .find()
), and support intuitive operations like concatenation with +
and assignment with =
without the risks of buffer overflows common with C-style string functions.
How do you declare a std::string
variable message
and initialize it with “Hello, C++”? How do you get its length?
#include <string> std::string message = "Hello, C++"; int len = message.size();
(or message.length();
)
How do you concatenate two std::string
objects, s1
and s2
, into a new string s3
?
std::string s3 = s1 + s2;