CMPS-2020 Programming II: Data Structures and Algorithms
Lab-2

Overview:

Step 1:
Do your work in your Odin /2020/2 folder.

Name your program /2020/2/lab2.cpp

We will start by building a simple program together on the big screen.

Our program will be the start of a class to manage character strings.
The functionality will be similar to the C++ string class.

int main()
{
    //Write constructors to make this code work.
    Mystring a;
    Mystring b("CSU Bakersfield");
    Mystring c('A');
    Mystring copy(b);

    cout << "display the strings...\n" << endl;
    cout << "The value of a **" << a << "**" << endl;
    cout << "The value of b **" << b << "**" << endl;
    cout << "The value of c **" << c << "**" << endl;
    cout << "The value of copy **" << copy << "**" << endl;

    cout << "Enter some new text...\n" << endl;
    cout << "Enter a string for c: ";
    cin >> c;
    cout << "The value of c **" << c << "**" << endl;

    cout << "Overloaded operators...\n" << endl;
    a = b;
    cout << "assignment: a = b **" << a << "**" << endl

    cout << "Comparison operators...\n" << endl;
    cout << "Equality test...\n";
    if (a == b)
        cout << "(a == b) is true\n";
    else
        cout << "(a == b) is false\n";

    cout << "Lengths...\n" << endl;
    cout << "Length of each string...\n";
    cout << "length of a: " << a.length() << endl;
    cout << "length of b: " << b.length() << endl;
    cout << "length of c: " << c.length() << endl;
    cout << "length of copy: " << copy.length() << endl;


    cout << "More testing code is below...\n" << endl;
    Mystring add1("add1");
    Mystring add2("add2");
    Mystring add3(add1 + add2);
    cout << "The value of add3 **" << add3 << "**" << endl;

    Mystring add4;
    add4 = add1 + add2;
    cout << "The value of add4 **" << add4 << "**" << endl;

    Mystring add5("-end-");
    add4 += add5;
    cout << "The value of add4 **" << add4 << "**" << endl;

    cout << "Test of getline...\n" << endl;
    cin.ignore();
    Mystring g;
    cout << "Please enter a sentence...\n";
    getline(cin, g);
    cout << "The value of g **" << g << "**" << endl;

    return 0;
}


Homework
Your Mystring class should have the following private data members.

  A char pointer for the c-string.
  An int for the amount of memory currently allocated to the string.

Your Mystring class should have the following public member functions.

  Default constructor
  Allocate memory for a character array that can hold a string of length 20.
  Set the string to be empty. 

  Copy constructor
  A default copy constructor will not work because it does a memberwise copy.
  Your class has a data member that points to memory that was allocated
  dynamically (not statically) so you must copy the data manually.
  Allocate memory based on source size and copy the data in.

  Conversion constructor for single character ('A')
  Allocate memory based on source size and copy data in.

  Conversion constructor for C-string ("CSUB")
  You have one of these already, but now you must allocate memory based
  on the size that you need to hold the text passed in.

  Assignment operator = 
  The default version of this operator will no longer work, because it does
  a memberwise copy of member data elements.
  You are copying text, so some re-allocation of memory could be needed.

  Input Operator >>
  You have one of these already, but now you might have to re-allocate the
  size of your array, depending on the length of the user input.

  Output operator << 
  If your string has no memory allocation yet, don't try to output it.

  Concatenation operator +
  This function should output the left operand followed by the right operand.
  You might setup a temporary string within the function to hold the
  combined length, then allocate enough memory to move it in.

  Append operator +=
  Append the contents of the right-hand operand to the current string.
  You may use strcat. You may need to resize the array.

  Less than operator < 
  Return true if the left-hand string is lexicographically smaller than
  the right-hand string. You may use strcmp.

  Greater than operator > 
  Return true if the left-hand string is lexicographically larger than
  the right-hand string. You may use strcmp.

  Equality operator == 
  Return true if the strings are the same. You may use strcmp.

  Not Equal operator != 
  Return true if the strings are different. You may use strcmp.

  length()
  Returns the length of the string. You may use strlen().

  erase()
  Clear the string. Handle the memory allocation in the way you see fit.

  getline(istream &in, Mystring &str)
  Get size-1 characters from the stream and store it in your object.

  reverse()
  This function reverses the order of the characters in the string.

  rtrim()
  Remove white-space from the right side of the character string.
  No change in allocated memory is needed.


To do the work above, you should plan on working at least 10-hours.


Testing
Write the class members above.
You may write more code in main to test your class functions.
I may post more code for main.


Number class
This is part of lab-2 homework.

Write a program: /2020/2/number.cpp

Start with the code below.

Do not change the main() function.

Do change the Number class.
You must overload the ++ and -- operators.

Starting code is here...

class Number {
    int value;
public:
    Number(int a) {
        value = a;
    }
    //add overloaded operators here...


    friend ostream& operator<<(ostream& os, const Number &n) {
        os << n.value;
        return os;
    }
};

int main(void)
{
    //Do not change this function.
    Number num = 1;
    ++num;
    num--;
    cout << num   << " ";
    cout << ++num << " ";
    cout << num++ << " ";
    cout << num-- << " ";
    cout << --num << " ";
    cout << num;
    cout << endl;
    return 0;
}

Output must look like this:
1 2 2 3 1 1
We will review this on Friday.


What to turn in...
Your instructor will find your work out on Odin.