Homework: Composition

  1. Write the definition for a class called Sample which has the following data members:
    first (an int)
    second (a float)
    third (a string)
    and the following member functions:
    1. a default constructor that initializes first to 2, second to -10.5, and third to the empty string
    2. one (with one int parm, returns void), which adds the parm and the data member first and stores the result in the data member second
    3. two, (with one string parm, returns int), which sets the data member third equal to the parm and returns true if second is less than zero, and false otherwise
    4. show (with no parms, returns void), which prints all data members on the screen
  2. Write the member function definitions for class Sample.
  3. Write the definition for a class called Other which has the following data members:
    alpha (a string)
    beta (a Sample)
    gamma (a Sample)
    delta (an int)
    and the following member functions:
    1. a default constructor which sets data member delta to -1
    2. othone (with two int parms, returns void), which uses the first parm to call one for data member beta and the second parm to call one for data member gamma
    3. othtwo (with one string parm, returns void), which sets data member alpha equal to the parm
    4. print (with no parms, returns void), which prints all data members
  4. Write the member function definitions for class Other.
  5. What does the following program print:
          int main()
          {
             Sample mine;
             Other yours;
             string words;
             cout "Enter a string: ";
             words.read();
             mine.one(2);
             if (mine.two(words))
                cout << “Ready\n”;
             else
                cout << “Not yet\n”;
             yours.print();
             yours.othone(5,6);
             yours.othtwo(words);
             yours.print();
             mine.one(8);
             mine.show();
          }
        
  6. Write the class definition for a class to describe a book. The data members are title (a string), author (a string), price (a float), and the count of the number of copies in stock (an int). The member functions are:
    1. a function to initialize the data members (the values to use in initialization are passed to this function as parms)
    2. a function to print all the data members
    3. a function to update a data member (the parm is a code (T, A, P, or C) that tells which data member) which prompts the user to enter the new value for the data member and then changes the data member to the new value
  7. Write the member functions for the book class.
  8. Write the class definition for a class to describe a bookstore. The data members are an array of book objects and a count of how many books are actually stored in the array. The member functions are:
    1. a default constructor (you decide what this constructor should do)
    2. a function to add a book to the bookstore (the parm is a book object), which returns true if the book was added or false if there was no room for the book
    3. a function to print all the books in the bookstore
    4. a function to update a book in the bookstore (the parm is the title of the book), which returns true if the book was updated and false if the book was not found (hint: use the update function from the book class)
  9. Write the member functions of the bookstore class.

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2003