Homework-2
Homework 2: Due Sep 24, 9:00 AM.
Note: Please follow the following instructions when you submit
your HW2 solutions:
- You may use multiple files for each problem.
Please use proper names for each file.
(e.g., HW2_Pr1-a.c, HW2_Pr1-b.c, etc.).
- Make a zip file off all your files.
- Submit your zip file into "Course Files" &rarr "Shared Files" &rarr "HW2"
folder.
1. A palindrome is a string that reads the same as the original string
if reversed. E.g. abcba, xyzzyx, 1234321 are palindromes.
abcdef, xyz, etc. are not palindromes.
Write a C program that prompts the user to enter a string.
For simplicity, you can assume the maximum length of the string
is 20 characters.
Then it prints "true" if the string is a palindrome, false otherwise.
E.g., Please enter the string:
abcba should print true.
Note: The function "strlen(str)" returns the length of "str",
that is, the number of characters in the string.
2. Define a class called "Number" that has a private data member called "value"
of type "union" with an "int" and a "double", as shown below:
union value_t
{
int intVal;
double dblVal;
} value;
Note: The data type "union" similar to "struct", but it allocates
only one memory location for all the variables defined inside the "union" type.
The memory allocated is the maximum of all the variables defined inside union.
Typically, we use union when we need to store only one of many
different variables. You can find more details of unions
here or
here.
In this example we use "value" to store only one of the two variables,
i or x.
- Define two "public" functions, "void setIntVal(int val)" that sets
the "intVal" variable in "value" and "int getIntVal()"
that returns the "intVal".
Similarly define two other public functions,
"void setDblVal(double val)" that sets
the "dblVal" variable in "value" and "double getDblVal()"
that returns "dblVal".
- Define a class called "myInteger" derived fom "Number".
Define the following in "myInteger":
- A public constructor that takes an "int val" as parameter,
and uses that to set the "intVal" of "value" in "Number".
- An "int add(int i)" that returns the sum of "intVal"
and the input parameter "i".
- A "subtract (int i)" that subtracts the input parameter "i"
from "intVal" and returns the difference.
- Define a class called "myDouble" derived fom "Number".
Define the following in that class:
- A public constructor that takes a "double val" as parameter,
and uses that to set "dblVal" of "value" in "Number".
- A "double add(double x)" that returns the sum of "dblVal"
and the input parameter "x".
- A "subtract (double y)" that subtracts the input parameter "y"
from "dblVal" and returns the difference.
- In the main program, ask the user to input three "int" values,
say, i, j and k.
- Use i to create an object of type "myInteger".
- Call the member function "add" with j as input,
and print the value.
- Call the member function "subtract" with k as input,
and print the value.
- Then ask the user to input three "double" values,
say, a, b and c.
- Use a to create an object of type "myDouble".
- Call the member function "add" with b as input,
and print the value.
- Call the member function "subtract" with c as input,
and print the value.
3. Question for bonus credit: A copy constructor is a
constructor that takes an object of the same class
as an argument and creates a new object by copying all the attributes
of the object. The example below shows the copy constructor
in a class called myClass.
class myClass
{
...
public:
myClass (const myClass& obj) { /* Code */} // Copy constructor
};
- Write a copy constructor for class "myInteger" and
another copy constructor for class "myDouble".
- In the main program, ask the user to enter an integer value.
Use that to create an object "obj1" of "myInteger".
Create another object "obj2" of "myInteger" by calling the
copy constructor using "obj1" as parameter.
Print the value of the "intVal" of "obj2".
- Next, in the main program, ask the user to enter a double value.
Use that to create an object "obj3" of "myDouble".
Create another object "obj4" of "myDouble" by calling the
copy constructor using "obj3" as parameter.
Print the value of "dblVal" of "obj4".
Ramana Isukapalli
Last modified: Thu Sep 18 10:02:00 EDT 2008