Separation of Lanthanides/ Lanthanides and Actinides
E7
1. Final Exam -- Object-Oriented Programming (FIT-II), Spring 2008
Software Engineering Track, NTUT, Jun. 20, 2008
Note: There are 7 questions worth a total of 100 points. You have 150 minutes. The
exam is *open* book, but you have to do it on your own. Do NOT talk or discuss with
anyone else.
1
Part I: Hand-written Questions (45%)
(15 pt) 1. Given the definitions
const int x = 17;
class A
{
public:
A();
A(int n);
int f() const;
int g(const A& x);
private:
int i;
};
Each of the three const keywords is a promise to the compiler that the compiler will
enforce. What is the promise in each case?
(10 pt) 2. What are the meanings of overloaded functions (as in operator overloading)
and virtual functions? Explain their differences.
(10 pt) 3. What are the purposes of public:, private: and protected:
sections in a class definition? Explain their differences.
(10 pt) 4. Suppose Child is a class derived from the class Parent and that the class
Grandchild is a class derived from the class Child. This question is concerned with
the constructors and destructors for the three classes Parent, Child, and Grandchild.
When a constructor for the class Grandchild is invoked, what constructors are
invoked and in what order? When the destructor for the class Grandchild is invoked,
what destructors are invoked and in what order?
2. 2
Part II: Programming Questions (55%)
(15 pt) 5. Define a class called Time. The time can be represented as two integers in
24-hour notation. Include the following functions in your class:
(1) two overloaded functions to set the time, in 12-hour and 24-hour notations.
For example, calling set(20, 30) will store the corresponding time 20:30; while calling
set(8, 30, ‘P’) will convert from 12-hour to 24-hour notation and store the same time
20:30.
(2) two overloaded functions to get the current time, in 12-hour and 24-hour
notations.
For example, if the current time is 14:25, then calling get(hr, min) will return two
integers:14 and 25; while calling get(hr, min, ampm) will convert from 24-hour to
12-hour notation and return two integers: 2, 25, and the A.M./P.M. information as a value
of type char (‘A’ for A.M. and ‘P’ for P.M). Thus, this function will have a
call-by-reference formal parameter of type char to record whether it is A.M. or P.M.
(3) two overloaded functions to output the current time, in 12-hour and 24-hour
notations.
For example, if the current time is 23:15, then output() will show “23:15”; while
output(HR12) will show “11:15PM”. (HR12 is defined as a boolean constant with
value of true.)
Finally, include the class in a test program.
(20 pt) 6. Define a class for a type called Fraction. This class is used to represent a
ratio of two integers. Include the following functions in your code:
(1) a member function that allows the user to set the numerator and the denominator.
(2) a member function that returns the value of the numerator divided by the
denominator as a double.
(3) a member function that outputs the value of the fraction reduced to lowest terms.
For example, instead of outputting 20/60 the function should output 1/3. This will
require finding the greatest common divisor (gcd) for the numerator and denominator,
and then dividing both by the number.
(3.1) the function for finding the gcd of two numbers can be implemented as
follows. Working with the two numbers’ absolute values, we find the remainder of
one divided by the other. Then, we calculate the remainder of the old divisor
divided by the remainder found. We repeat this process until the remainder is zero.
The last divisor is the gcd.
Finally, embed your class in a test program.
3. (20 pt) 7. Define a base class called Worker that has two derived classes: Employee
and Manager. For workers, we only have simple attributes such as their names and
working hours. For employees and managers, each has specific attributes unique to
the class. Employees have attributes such as the name of the department that they are
working with, and their salaries. Managers have attributes such as the number of
managed employees and the list of projects that they’re in charge. Also, define
another class called Project that has attributes such as the name and the current
progress (in %) of the project.
(1) Write the corresponding constructors that initialize the attributes for classes.
(2) Write the corresponding destructors that clean up the attributes for classes.
(3) Write the corresponding member functions to set and get the attributes for classes.
(4) Write a virtual function print_record() that will print all attributes related to
the object.
Finally, test the classes in a main program. In the test program, you should
dynamically allocate some employees and managers, print their corresponding records
using the virtual function print_record(), and then release the dynamically
allocated objects in the end.
3