CIS 330 Home Page Last updated 2008/01/17 15:13:58

CIS 330 Assignment 3

C++ Classes

Use the web service e-turnin to submit your work electronically. You may turn in revisions of your homework up to the time it is due.

This assignment focuses on using C++ classes to encapsulate data. Several of the problems are object oriented rewrites of the problems in the last assignment, but you should be using only new and delete for any dynamically allocated objects.


  1. (Rewrite of problem 1 from Assignment 2) Write a C++ class IntList that implements a simple singly linked list of integer values. The functionality should be just like the implementation from Assignment 2. Make sure you define appropriate constructors and member methods. You can define an inner class IntNode to encapsulate the nodes of the linked list. Provide a header file named IntList.h with the definitions of your class IntList and its nested class IntNode. Provide any external implementations of methods in a source file named IntList.c.

    Your code should work with the driver intListTest.c, which can also be used to test your implementation. The output should be the same as in problem 1 from the previous assignment.


  2. Write a C++ class named Rational that behaves like a Rational number, (the quotient of two integers). Your class should define appropriate constructors, arithmetic operators, comparison operator, and an output operator.

    Use the driver code rationalTest.c to test your implementation and determine the behavior you must implement. Note the different forms of the constructor. Also notice that regardless of the initial values used, the Rational objects are always in lowest terms.

    Running this program should produce the following:

    r1 is: 4/5
    r2 is: 3/1
    r3 is: 0/1
    r4 is: 3/7
    r5 is: 0/1
    r1 == r2 is: 0
    r2 is: 4/5
    r1 == r2 is: 1
    r5 = r1 * r4 is: 12/35
    r1 + r5 is: 8/7
    r4++ is: 3/7
    r4 is: 10/7
    ++r4 is: 17/7
    r4 is: 17/7
    r1 * 4 is: 16/5
    7 / r4 is: 49/17
    r2 - 7 / r4 is: -177/85
    


  3. (Rewrite of problem 2 from Assignment 2) Write a C++ class Array2d that implements a two dimensional array of doubles. Objects of this class should behave like built in two dimensional arrays, i.e., you can use the array operator to access and assign elements of the array. Your class must encapsulate the memory allocation and de-allocation, and must also support intelligent assignment of arrays.

    Use the following driver code array2dTest.c to test your implementation. Output should be as in Assignment 2.


  4. Extend the implementation of the class MyString to add a substring operator, comparison operators, and compound assignment operators for strings and characters. The substring operator should behave as shown in the driver code: if s is a MyString object, then s(i, n) is a MyString object that consists of n characters from s, starting with the character at offset i. The comparison operators use ASCII or Unicode ordering of characters.

    You can start with this MyString.h and MyString.c.

    Use the driver code stringTest.c to test your implementation. This driver produces the following results:

    s1 is:<Hello, world>, s1.length() is 12
    s2 is:<and goodbye>, s2.length() is 11
    s3 is:<Hello, world and goodbye>, s3.length() is 24
    Now s1 is:<world>, s1.length() is 5
    s2 comes first
    And now s1 is:<World>, s1.length() is 5
    Now s1 comes first
    Now s3 is:<Hello, world and goodbye >, s3.length() is 25
    Now s3 is:<Hello, world and goodbye for now...>, s3.length() is 35