Final Exam
Sample Questions - Fall 2008
Note: these questions are based on material since the lab
midterm The final exam will have questions from the earlier
material as well.
1. PC Class
You are to write part of a class named PC to represent features of
personal computers. The data members of the class and default
values will be:
| Data member |
Description |
Default value |
| name |
name of the computer (string)
|
"PC" |
| speed |
processor speed in gigahertz
(double) |
1.0
|
| memory |
memory size in megabytes (double) |
512.0
|
| disk |
disk drive size in gigabytes
(double)
|
40.0
|
price
|
price in dollars (double) |
1.0
|
Initially, there will be four member functions: a default constructor
and a constructor with parameters, a function called showSpecs to display the values
of the data members, and addMemory
which increases a PC object's memory size and its price.
a) Write the declaration section of the class showing the data
member declarations and the function declarations for the constructors,
showSpecs(), and addMemory(). Member function showSpecs() should have as a
parameter a reference to an ostream object. Function addMemory() will have
parameters for
the additional memory and the price of that memory.
b) Write the definition for the constructor that will
initialize the data members using parameters. Use a constructor
initialization list.
c) Writethe definition for the member function addMemory() which increases the
PC object's memory size and price.
2. Overloading Operators
Now extend the PC class by overloading the * symbol to
multiply the price of a PC object by a discount factor. For
example, if pc1
is a PC object that has a price of $1000.00, pc2 is a PC
object with default data member values, and discount is a double with
value 0.80, then
pc2 = pc1*discount;
will create a new PC object with price $800.00 and assign it to pc2.
The other data member values for pc2 will be
the same as the pc1 values.
a) Write the declaration for the operator function.
b) Write the operator function definition.
3. Inheritance
Using the PC class from problem 1 as the base class, you need to derive
class Notebook for notebook (laptop) computers that has
additional
data members screen and weight, both of type
double. screen
will hold the diagonal dimension of the notebook's screen in inches and
weight will be the weight of the notebook in pounds.
The redefined member function showSpecs()
will now display the screen and weight values for the notebook computer
in addition to the name, speed, memory, disk, and price values.
Note: you can assume that the base class PC now has protected
instead of private data members.
a) Write the declaration of the Notebook class . Include the
constructors and showSpecs() as
member
functions.
b) Write the definition for the
constructor that initializes the data members to values passed
into
the
parameters. Use the constructor initialization list.
c) Write the statement in the space provided to perform the specified
action.
int main()
{
// declare a PC object named pc1 for a Dell 4700 with a 2.3
// Ghz processor, 512 Mbytes of memory, and an 80 Gbyte
// disk drive that costs $1350.00.
// display the data members of this PC object on the
// terminal screen.
// declare a Notebook object named note1 for Dell 3800 with a
// 1.5 Ghz processor, 512 Mbytes of memory, and a 40 Gbyte
// disk drive. The diagonal dimension of the screen is
// 15.1 inches, the weight is 5.0 pounds, and the
// price is $1825.00.
// display the data members of this Notebook object on
// ofstream object file_out.
ofstream file_out( "computers.dat");
}
4. Friend Function
In order to compare PCs, we can define a rating method as follows:
speed_in_ghz memory_in_mbytes
disk_in_gbytes
rating =
------------ + ---------------- +
---------------
1
512
40
For example, the pc1 object from problem 3 would have a rating of
2.3
512 80
--- +
---- + --- = 5.3
1 512
40
For the PC class from problem 1, you are to create a non-member friend
function named rating() that returns the rating for the
computer.
Function rating() should have a reference to an PC object
as its only parameter.
a) Write the statement that should appear in the declaration part of
the PC class that identifies rating() as a friend
function of
the class.
b) Write the definition of function rating().
c) Write the statement or statements that will use function rating()
to determine the rating for PC object pc1 and assign it to double
variable
pc_rating.
5. Pointers and Linked Lists
a) Write the statement
that creates a pointer named final
that can contain the address of a variable of type double.
b) The new
operator can be used to allocate space for NUM_EL
(an int) numbers of type double; it will return a pointer to the
beginning of the allocated space. Given the statement:
final
= new double[NUM_EL];
write a statement that
will assign the value 451.0 to the fourth location in the allocated
array.
c) Consider the following
struct:
struct Student
{
string name;
int ID; // student identification number
double GPA; //
grade point average _______________________________________ // next
address
};
In preparation for
creating a linked list, a pointer to the next struct in the list must
be added to the definition of the Student struct. Write the
declaration of this pointer on the line provided above in the body of
the struct.
d) Given the following
declarations
Student
S1 = {"Bugs Bunny", 823, 2.75},
S2
= {"Michael Mouse", 007, 3.98};
write the statements that
will:
Part e uses the
list<T> class in the Standard Template Library (STL). For this
part you can use one or more of the following member functions:
push_back(
value )
push_front(
value )
insert(
iterator, value )
begin()
end()
size()
e) Suppose Student
structs
S1 and S2
are defined as in part d. Using the STL list class, write the
statements to declare a list of Student structs named Santa_list
and then add S1 and S2
to the list.
6. vector<T>
Class
Write the definition of a function named
vectorAscend() that
returns true if the values
stored in a vector<double>
are in ascending order and false otherwise. The vector is in
ascending order if each value is less than or equal to the next
value.
You should use the size()
member function to determine the number of elements in the vector.
7. string Class
Here are some member
functions of the string class that you can use in this problem, if
necessary:
-
size() -
returns the number of characters in the string
-
substr(first,
num_chars) - extracts num_chars characters starting at
index first
-
replace(first,
num_chars, replacement_string) - replaces num_chars
characters starting at index first with replacement_string
-
insert(position,
new_string) - inserts new_string before the character in
index position
-
find(pattern,
position) - look for pattern starting at index position.
The index where the pattern is found is returned; if the search fails,
the function will return string::npos, a special constant
a) Write the declaration
for a string object named report
and initialize it to the character string It is a cold winter day!
b) Wrtie the statements tha will replace cold with warm in the report object.
|