Question
Lab #4: Classes, Operator Overloading, Destructors, and C++ Streams Learning Outcomes: After completing this lab, you will have demonstrated the ability to: 1. Edit, compile,
Lab #4: Classes, Operator Overloading, Destructors, and C++ Streams
Learning Outcomes:
After completing this lab, you will have demonstrated the ability to:
1. Edit, compile, and run C++ programs.
2. Declare, create, initialize, assign values to, and pass
class objects to functions.
3. Declare and use .h and .cpp files to declare and define classes.
4. Use constructors, destructors to properly initialize and perform
cleanup routines on class objects in memory.
5. Use operator overloading to be able to use primitive operators
(+, -, *, /, etc) with class object types.
6. Use C++ streams (cout) to perform basic output in C++ using
the ostream cout.
QUESTION:
MAKE a class called "Formatter", which is designed to display,
on the standard output device (using ONLY cout << NOT printf( )),
any text that is sent to it. The text is displayed with no more
than a specified number of characters per line (default 50),
wrapping around to the next line at the start of any word that
would otherwise cause the output line to be too long.
A "word", for the purpose of this question, is defined to be any
string that is sent to the object at one time. The Formatter object
itself does NOT store any string(s) in memory!
The Formatter class has the following publicly available members:
a). When creating a default Formatter object, the output width is
set to 50.
b). Alternatively, a Formatter object may be created by supplying
a width (an integer) which is then used for the output width.
c). Contains a function called "setWidth" that accepts an
integer "n" and returns nothing. The "setWidth" function
sets the width for subsequent output lines to "n". If more
than n characters have already been output on the current line,
then a newline is output.
d). A function called "display" that accepts a constant character
string "s" and returns nothing. The "display" function outputs
the string "s", if it will fit on the current line. If it will
not fit, a newline is output, thus starting another line, and
then the string is output (unless the string is longer than the
maximum width of a line, in which case only as many characters
as will fit are displayed and the rest are ignored). After
outputting the string, if the current line is not already full,
a space is output.
A constant character string is specified using the const keyword
(eg. const char s[ ]).
NOTE: This function does not output a newline after outputting
the string. Also note that you may assume that only normal
printable characters will be part of the string, and not
invisible characters such as tabs or newlines.
HINT: To achieve this, keep track of how many characters have
been output on the current line, so that future calls to
display( ) can accurately determine how much room is left
on the current line.
Be careful with the code and logic for this function!
e). A function called "newline" that accepts nothing and returns
nothing. This function outputs a newline, thus beginning
another line of output.
f). It is possible to send a constant string to a Formatter object
using the << operator with a Formatter object on the left and a
constant string on the right. This operator performs the same
operation as the display( ) function specified earlier (see above)
and returns no value.
g). It is possible to increase a Formatter object's width by 1 using
the prefix operator ++. This operator returns no value.
h). It is possible to decrease a Formatter object's width by 1 using
the prefix operator --. This operator NEVER sets the width of a Formatter
object to a value less than 1. This operator returns no value.
i). Also, when a formatter is destroyed, the string "~END" is sent to the
Formatter, unless no characters have been output on the current
line, in which case only the string "FINISHED!" is sent to the
Formatter object.
After each string is sent to the Formatter, this function also
makes a call to the newline( ) function.
For example, the following program:
#include
#include
#include "lab4.h"
using namespace std;
// your code for the Formatter member function definitions goes here...
// ...
// ...
// local Formatter variable 'p' in this fnuction, will be destroyed
// (trigger the destructor) when this function ends and 'p' goes out of scope
void testFormatter(Formatter p, int n) {
char lines[16][51] = { "Now", "we", "finally", "understand", "what", "the",
"heck", "is", "happening.", "It", "has", "only", "taken",
"the", "whole", "semester!" };
int i;
for(i=0; i<16; i++) {
if(i == n)
p.newline( );
p << lines[i];
}
}
int main( ) {
Formatter x, x2(11);
x.display("12345678901234567890");// this call should display the entire string
x.setWidth(14);
++x;// sets x's width to 15
testFormatter(x, 9);// calling the testFormatter( ) function
cout << "--------------------------------------------------";
--x2;// sets x2's width to 10
x2.display("12345678901234567890");// this call should ONLY display the first 10 characters of the
// string
testFormatter(x2, -1);// calling the testFormatter( ) function
return 0;
}
outputs (beginning at the left edge of the screen):
12345678901234567890
Now we finally
understand what
the heck is
happening.
It has only
taken the whole
semester! ~END
--------------------------------------------------
1234567890
Now we
finally
understand
what the
heck is
happening.
It has
only taken
the whole
semester!
~END
FINISHED!
FINISHED!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started