Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Day Class Notes

Download Compiler

Evening Class Notes

Lab Notes

Linux For Windows

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
Answers to the Final Exam Sample Questions - Fall 2008

Note: these questions are based on material since the second midterm  The final exam will have questions from the earlier material as well. There will be around ten questions on the exam.  The answers to these sample problems will be posted next week.

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.

class PC
{
  private:
    string name;
    double speed;
    double memory;
    double disk;
    double price;

  public:
   PC();
   PC(string, double, double, double, double);
   void showSpecs(ostream &);
   void addMemory(double, double);

};




b) Complete the following definition for the constructor that will initialize the data members using parameters by writing the statements on the lines provided.  Use a member initialization list.

PC::PC(string N, double S, double M, double D, double P) :

name(N), speed(S), memory(M), disk(D), price(P)

{}



c) Complete the definition for the member function addMemory() which increases the PC object's memory size and price.

void PC::addMemory(double mem, double cost)
{
    memory += mem;

   price += cost;
}


 
2. Overloading Operators

Now you will 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.

PC operator*( double );

b) Complete the operator function definition by filling in the lines (you can leave lines blank or add new ones).

PC PC::operator*( double discount )
{
       PC temp;

       temp.name = name;
       temp.speed = speed;
       temp.memory = memory;
       temp.disk = disk;
       temp.price = price*discount;
  
       return temp;
}   



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) Complete the following declaration section of the Notebook class by filling in the lines.  Include the constructors and showSpecs() as member functions. (You can leave lines blank or add new ones.)

class Notebook :  public PC
{
   protected:

     double screen;
     double weight;

   public:

     Notebook();
     Notebook( string, double, double, double,  
           double,double, double);
     void showSpecs( ostream & );
};



b) Complete the following constructor function definition by writing the statements for the header and function body on the lines provided. This constructor should initialize the data members to values passed into the parameters.  Use the member initialization list.

Notebook::_Notebook( string N, double S, double M, double D, double P, double Scr, double Wt)

: PC(N, S, M, D, P), screen(Scr), weight(Wt)

{}


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.

   PC pc1("Dell 4700", 2.3, 512, 80, 1350.);


// display the data members of this PC object on the  
// terminal screen.

   pc1.showSpecs( cout );


// 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.  

   Notebook note1("Dell 3800", 1.5, 512, 40, 1825.0, 15.1, 5.0);



// display the data members of this Notebook object on
// ofstream object file_out.

   ofstream file_out( "computers.dat");

   note1.showSpecs( file_out );


}


 
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.

   friend double rating( PC& );   

b) Write the definition of function rating().

    double rating(PC &P)
    {
          double rat;
 
          rat = P.speed + (P.memory/512.) + (P.disk/40.);
 
          return rat;
    }



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.

   pc_rating = rating( pc1 );

 
5. Pointers

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.

    double *final;

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.

    final[3] = 451.0;

c) Consider the following struct:

struct Student

{

string name;

int ID; // student identification number

double GPA; // grade point average

Student *next_address; // 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:

  • create a pointer named first and assign to it the address of S1

  • assign the address of S2 to the next address member of S1

    Student *first = &s1;
   s1.next_address = &s2;


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.

list<Student> Santa_list;
Santa_list.push_back(s1);
Santa_list.push_back(s2);




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.

bool vectorAscend(vector<double>& vec)
{
double val;

val = vec[0]; // first value

for(int i=1; i < vec.size(); i++)
{
if ( val > vec[i] )
return false;

val = vec[i];

}

return true;

}


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!

string report = "It is a cold winter day!";


b) Wrtie the statements tha will replace cold with warm in the report object.


int index, num_char = 4;

index = report.find("cold", 0);

report.replace(index, num_char, "warm");






 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming