Design and code a class named Paragraph that holds information about a paragraph.  Upon instantiation, a Paragraph object may receive a null-terminated C-style string.  If the object receives such a string, the object accepts the string without modification as a paragraph.  If the object does not receive a string, the object stores a safe empty state.  The object adopts a default output width of ten (10) characters.  Your class does not impose any limitation on the number of characters in a Paragraph object.  Your design includes all of the member functions needed to accomodate this feature under a variety of applications, including the one below.

Your class also includes the following functions:

  • clean() - a query that returns the address of the first non-whitepsace character in the Paragraph object;
  • clean(int i) - a query that returns the address of the first character of the i-th word in the Paragraph object, where a word is a contiguous set of non-whitespace characters; and
  • width(int) - a modifier that receives an integer holding the number of characters to be displayed on a single line of output; if the integer is invalid for any reason, your object adopts the default number of characters;
  • an insertion operator that inserts the entire paragraph (with leading spaces, if any, included) into an output stream.
For example, consider the following program that uses your class

 #include <iostream>
 using namespace std;
 #include "Paragraph.h"
 int main ( ) {
     Paragraph a, b("   This is OOP244.  Next is BTP300.");
     cout << b << endl;
     a = b;
     cout << a.clean() << endl;
     cout << a.clean(2) << endl;
     cout << a.clean(4) << endl;
     return 0;

This program produces the following output

    This is OOP244.
 Next is BTP300.
 This is OOP244.  Next is BTP300.
 is OOP244.  Next is BTP300.
 Next is BTP300.

  Designed by Chris Szalwinski   Copying From This Site   
Creative Commons License