Homework-2

Homework 2: Due Sep 24, 9:00 AM.

Note: Please follow the following instructions when you submit your HW2 solutions:
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.
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: In this example we use "value" to store only one of the two variables, i or x.
  1. 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".
  2. Define a class called "myInteger" derived fom "Number". Define the following in "myInteger":
  3. Define a class called "myDouble" derived fom "Number". Define the following in that class:
  4. In the main program, ask the user to input three "int" values, say, i, j and k.
  5. Then ask the user to input three "double" values, say, a, b and c.

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.
  1. Write a copy constructor for class "myInteger" and another copy constructor for class "myDouble".
  2. 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".
  3. 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