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:
- You may use multiple files for each problem.
Please use proper names for each file.
(e.g., HW3_Pr1-a.c, HW3_Pr1-b.c, etc.).
- Make a zip file off all your files.
- Submit your zip file into "Course Files" &rarr "Shared Files" &rarr "HW3"
folder.
1. Design a class called "MailHeader" that has the following private fields:
To, From, Subject, Date, Content.
- Each field itself is an object of type MailAttribute.
That is, design a class called MailAttribute that has two private fields:
AttributeName and AttributeValue.
- 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.
- 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.
- 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.
- 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:
To: myFriend@cs.columbia.edu
From: myName@cs.columbia.edu
Subject: Hello
Date: Sep 27, 2007
Content: This is a dummy email message.
2. Consider a class called "shape".
- 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.
- "int getCenter_x( ) const" that returns "center_x".
- "int getCenter_y( ) const" that returns "center_y".
- A pure virtual function called "double returnArea( )" in the shape class,
to make the shape an abstract class.
- Define two classes rectangle and circle, each derived from shape.
- 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.
- Define a public method called "double returnArea( )" in the rectangle class
that computes the area (length x width) and returns it.
- 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.
- 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.
- 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).
- 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.
- In the main program,
- 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
- Create another object of the rectangle using the copy constructor,
something like
- 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).
- 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