CS 202 Fall 2013  >  Assignment 2

CS 202 Fall 2013
Assignment 2

Assignment 2 is due at 2 p.m. Wednesday, September 25. It is worth 30 points.

Procedures

Complete each of the exercises below. Then turn in your work as follows.

  1. Run Check. Demonstrate the programs you have written for the exercises below to either the instructor (Glenn G. Chappell) or the T.A. (Zak Williams). Get official approval.
  2. Submission. Submit your code using Blackboard, under Assignment 2 for this class. You should only submit code that has already passed the Run Check. Be sure you attach your source/header files; do not paste them into the text box. Also, send only source & headers; no project files or executables.

We may not look at your homework submission immediately. If you have questions, e-mail me.

Exercises (30 pts total)

Do each of the following exercises.

  1. Write a minimal version of a class Wodget described below. “Minimal” means that all the member functions are there, and the ones that return values have a dummy return statement, but there is no other code written. So most of the member functions should have empty function bodies: “{}”. And there should be no member variables at all.

    Class Wodget should be implemented in files wodget.h and wodget.cpp. It should follow standard conventions regarding the construction of header and source files. It should have the following member functions.

    • A default constructor.
    • A constructor taking two parameters: a string and an int.
    • Member function cat taking an int parameter and returning nothing.
    • Const member function dog taking two int parameters and returning an int.
    • Const member function llama taking one parameter, a vector of Wodget objects, and returning nothing.
    • Accessor and mutator functions for a presumed member variable cow of type string. These should be named getCow and setCow and should be written as usual.

    Demonstrate your class by compiling it with program wodget_main.cpp and executing the result. Do not modify file wodget_main.cpp.
     

  2. Write a class Brackets with the following three public member functions.
    • Const member function hello. This should print “[Hello!]” to cout, followed by a newline.
    • Const member function dollars. This takes an int parameter, and prints that many dollar signs between brackets to cout, followed by a newline. For example, calling function dollar with parameter 4 would print “[$$$$]”.
    • Const member function message. This should take a string parameter. It should print the string between brackets to cout, followed by a newline. For example, calling function dollar with parameter "boat" would print “[boat]”.

    (Just in case it is not clear, these are brackets: “[]”. These are not: “(){}<>”.)

    The common functionality in these three member functions should be implemented in a private member function output which all three of them call. In particular, member function output should be the only member function that does any printing.

    Demonstrate your class by writing a program that uses it and calls all three public member functions.
     

  3. Finish program print_from_ptrs.cpp by writing function printAll. This function takes a vectorof (PrintSomething *). It returns nothing. Each pointer in the vector can be assumed to point to a PrintSomething object. The function is to go through the vector, in order. For each pointer, it should call member function doPrint on the object the pointer points to.

    Do not modify any other part of program print_from_ptrs.cpp.
     

  4. Write a class Greeter with the following member functions.
    • Default constructor. This should generate a random number from 1 to 1000 (use rand?) and save this number in a member variable. The constructor should print “Hello #” followed by the number and a newline to cout.
    • Destructor. This should print “Goodbye #” followed by the saved number and a newline to cout.

    So, for example, here is a program that uses class Greeter.

    [C++]

    #include "greeter.h"
    int main()
    {
        Greeter x;
        return 0;
    }

    This program would produce output something like the this.

    Hello #328
    Goodbye #328

    Demonstrate class Greeter by writing a program that does the following.

    • Ask the user how many objects to create.
    • Create a vector containing that many Greeter objects.
    • End.

    The program should do nothing else. (This ought to result in a number of calls to the Greeter default constructor and destructor, with the result that some friendly messages are printed.)
     


CS 202 Fall 2013: Assignment 2 / Updated: 20 Sep 2013 / Glenn G. Chappell