Homework 9, C++

Problem 1.

Create a class called Rational for performing arithmetic with fractions. Write a driver program to test your class. Use integer variables to represent the private data of the class - the numerator and the denominator. Provide a constructor function that enables an object of this class to be initialize when it is declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form (i.e., the fraction 2/4 would be stored in the object as 1 in the numerator and 2 in the denominator). Provide public member functions for each of the following:

Problem 2.

You can declare elements of the class static. It is implemented as one copy of the element, and all other objects of the class just have a pointer to this copy. Find the error(s) in each of the following and explain how to correct it.

 
a)  class Example {
    public:
      Example(int y = 10) {data = y;}
      int getIncrementedData() const {return ++data;}
      static int getCount() {
         cout << "Data is " << data << endl;
         return count;
      }          
      private:
        int data;
        static int count;
   }
b) char *string;
   string = new char[20];
   free(string);

Problem 3.

Create a class IntegerSet Each object of the class can hold integers in the range 0 through 100. a set is represented internally as an array of ones and zeros. Array element a[i] is 1 if integer i is in the set. Array element a[j] is 0 if integer j is not in the set. The default constructor initializes a set to the so-called "empty set", i.e., a set whose array representation contains all zeros.

Provide member functions for the common set operations. For example, provide a unionOfIntegerSets member function that creates a third set which is the set-theoretic union of two existing sets (i.e., an element of the third set's array is set to 1 if that element is 1 in either or both of the existing sets, and an element of the third set's array is set to 0 if that element is 0 in each of the existing sets.)

Provide an intersectionOfIntegerSets member function that creates a third set which is the set-theoretic intersection of two existing sets i.e., an element of the third set's array is set to 0 if that element is 0 in either or both of the existing sets, and an element of the third set's array is set to 1 if that element is 1 in each of the existing sets).

Provide an insertElement member function that inserts a new integer k into a set (by setting a[k] to 1). Provide a deleteElement member function that deletes integer m (by setting a[m] to 0).

Provide a settPrint member function that prints a set as a list of numbers separated by spaces. Print only those elements that are present in the set. Print --- for an empty set.

Provide an isEqualTo member function that determines if two sets are equal.

Provide an additional constructor to take five integer arguments which can be used to initialize a set object. If you want to provide fewer than five elements in the set, use default arguments of -1 for the others.

Problem 4.

Create a definition and member (and friend) functions for a class String.

Overload operators to compare strings (<, <=, >, >= , ==, !=). Create a copy constructor, a conversion constructor that converts char * to String , and destructor. Define input and output operations. Define an assignment ooperator.

Define a concatenation operator += which takes two arguments and concatenates the second String object to the right of the first String object, thus modifying the first String object. In some applications, it is desirable to produce a concatenated String object without modifying the two String arguments. Implement operator+ to allow operations such as

   string1 = string2 + string3