Practice

Indexed Record


Record

Design and code a class named Record that holds information about a text record.  Upon instantiation, a Record object may receive a null-terminated C-style string.  If the object does not receive a string, the object stores a safe empty state.  Your class does not impose any limitation on the number of characters stored in a Record 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 includes the following member functions:

  • void display(ostream&) - a query that sends the text stored in the Record object to the output stream specified in the parameter;
  • Record& operator=(const char*) - a modifier that replaces the text stored in the Record object with the text specified through the parameter;
Your class also includes the following helper function:
  • an insertion operator that inserts a Record object into an output stream.
For example, the following program produces the output shown on the right

 #include <iostream>
 #include "Record.h"
 using namespace std;

 int main() {
    Record rec1("inheritance"), rec2 = rec1;

    cout << rec1 << endl;
    cout << rec2 << endl;
    rec1 = "overloading";
    cout << rec1 << endl;
    rec2 = rec1;
    cout << rec2 << endl;

    return 0;
 }









 inheritance
 inheritance

 overloading

 overloading





Indexed Record

Design and code a class named IRecord that holds information about a text record that is part of an index.  Derive your class from the Record.  Upon instantiation, a IRecord object may receive a triple consisting of

  • a null-terminated C-style string,
  • an array of integer values holding page numbers and
  • an integer holding the number of elements in the array. 
If the object does not receive this triple or if any of the values are invalid, the object stores a safe empty state.  Your class does not impose any limitation on the number of pages stored in an IRecord 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:

  • void display(ostream&) - a query that sends the text stored in the IRecord object to the output stream specified in the parameter followed by the space and comma delimited page numbers in the format shown below;
  • void operator+=(int) - a modifier that inserts the value received into the list of page numbers stored by the object.
For example, consider the following program that uses your class

 #include <iostream>
 #include "IRecord.h"
 using namespace std;

 int main() {
    int p[] = {23, 45, 67}, n = 3;
    IRecord rec1("inheritance"), rec2("overloading", p, n);

    rec1 += 78;
    cout << rec1 << endl;
    rec1 += 121;
    cout << rec1 << endl;
    rec2 += 89;
    rec1 = rec2;
    cout << rec1 << endl;

    return 0;
 }

This program produces the following output

 inheritance 78
 inheritance 78, 121
 overloading 23, 45, 67, 89







  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License