Homework-3


Homework 3: Since there is a midterm next week on Oct 1, this is DUE on Oct 03.

Note: Please follow the following instructions when you submit your HW3 solutions:
1. Design a class called "MailHeader" that has the following private fields: To, From, Subject, Date, Content.
  1. Each field itself is an object of type MailAttribute. That is, design a class called MailAttribute that has two private fields: AttributeName and AttributeValue.
  2. Provide a constructor in the class MailAttribute that takes two strings as input and sets the first one ( e.g. "To") as the AttributeName and the second one ( "myFriend@cs.columbia.edu" ) as the AttributeValue. Note: You can find examples of C++ strings at http://www.cprogramming.com/tutorial/string.html.
  3. In the MailHeader class, provide a default constructor (with no arguments) and destructor. Also provide another constructor that takes five MailAttributes and sets them for the five fields (in the order shown above) in the MailHeader class.
  4. Create a generic (not member) function called "printMaildata(MailHeader hdr)" that prints each of the five MailAttribute names and values. Define this function as a friend function in the two classes, i.e., MailHeader and MailAttribute.
  5. In the main program, create five objects of MailAttributes, one for each of "To, From, Subject, Date, Content" fields. You can use any meaningful values for each of them. Using the five MailAttribute objects, create an object of MailHeader. Then call the "printMailData (..)" function with the MailHeader object as input. The output should look like:

2. Consider a class called "shape".
  1. Define two private integers, "center_x" and "center_y" in the class shape, that define the center of the shape. Define a public constructor that takes two integers x, y as arguments. The constrcutor should set x as "center_x" and y as "center_y". Also define the following three public functions.
  2. Define two classes rectangle and circle, each derived from shape.
  3. Define two private integer variables "length" and "width" in the rectangle class. Rectangle should have two public constructors, a default constructor (with 0 arguments) that sets the "length", "width", "center_x" and "center_y" to 0. It should have a second constructor that takes four integers as arguments and sets the first two as "center_x" and "center_y" and the last two as the "length" and "width", respectively, in the rectangle class.
  4. Define a public method called "double returnArea( )" in the rectangle class that computes the area (length x width) and returns it.
  5. The class circle should have a private variable called "diameter" of type "int" and two public constructors. The first constructor is a default constructor (with 0 arguments) that sets the "diameter", "center_x" and "center_y" to 0. The second constructor takes three integers as input and sets the first two as "center_x" and "center_y" and the last input as "diameter", respectively.
  6. Each derived class should implement the "double returnArea( )" method that returns the area i.e., length * width for rectangle and pi * d^2/4 for circle.
  7. Define a function called "printCenter(Shape *obj)". Make it a friend function in the class shape. The function should print the "center_x" and "center_y" variables of the shape (don't use the getCenter_x( ) and getCenter_y( ) functions, access "center_x" and "center_y" directly).
  8. Define a copy constructor in each of circle and rectangle. Write the code for the copy constructor for both circle and rectangle. The code for the copy constructor should copy "length", "width", "center_x" and "center_y" in the rectangle class, while it should copy "diameter", "center_x" and "center_y" in the circle class.
  9. In the main program,
    1. Ask the user to input four integer values. Using the first two values as "center_x" and "center_y" and the last two values as the "length" and "width", create a new rectangle object and define it as a pointer to a shape object.
        shape *rect1 = new rectangle(...) // You need to complete this
    2. Create another object of the rectangle using the copy constructor, something like
        rectangle rect2(*rect1);
    3. Print the following values:
      • Compute the area of the rectangle using the "rect2.returnArea( )" function and print it.
      • Then print the center of the rectangle using printCenter(&rect2).
  10. Next, ask the user to enter three integer values. Using the first two as "center_x" and "center_y" and the last as the "diameter", create a new object of circle (circ1) and define it as a pointer to the shape object. Create another object of circle (circ2) using the copy constructor. Compute the area of the circle using the "circ2.returnArea( )" function and print it. Then print the center of the rectangle using the printCenter function as printCenter(&circ2).

Ramana Isukapalli
Last modified: Thu Sep 25 09:47:07 EDT 2008