CPSC 246 Exam 1
Fall 2016
Name:_______________________
1. (4 points)Assume a data member has been defined as private in a class. Circle the appropriate access(es) to that data member:
Full access from main
No access from main
Full access from methods of the class
No access from methods of the class
2. (4 points) Assume a data member has been defined as public in a class. Circle the appropriate access(es) to that data member:
Full access from main
No access from main
Full access from methods of the class
No access from methods of the class
3. (10 pts) For each of the following, answer true or false:
_____ Name reuse for two or more instance variables declared in the same class is permitted.
_____ Name reuse for two or more local variables declared in different methods is permitted.
_____ Name reuse for a local variable and parameter of the same method is permitted.
_____ Using C++ operators as the name of methods is permitted.
_____ Name reuse for two methods of the same class with different parameter types is permitted.
4. (6 pts) For each of the following, describe the scope for variables defined in that category:
a. Parameters
b. Private instance variables
c. Public instance variables
5. (10 points) Give the C++ code to declare a table of integers named board with 9 rows and 6 columns. Provide the code to request the 54 integers from the user and place them in your table.
6. (5 pts) If the variable row contains a valid row number of board and variable col contains a valid column number, what is the equation to determine the offset of element board[row,col] within the board?
7. (6 pts) Describe the steps the compiler and operating system must accomplish when 1) a method is called, and 2) when a method finishes and a value is returned.
CALL RETURN
8. (5 pts) Assume a class named exam1 is defined and has a method named setproblem that requires an integer value. Write the C++ code to declare and instantiate an object named ex of type exam1, and call the setproblem method with the value of 5.
9. (5 pts) Assume a class named exam1 is defined and one of its private data members is an integer named PointValue and a public method named setPointValue needs to be written to accept an integer value. Write the setPointValue method to set the private data member, PointValue, to the integer-valued parameter which is ALSO named PointValue.
10. (5 pts) Describe several features of C++ that may be used by a programmer to define an abstract data type where information hiding is necessary.
11. (5pts) C++ programmers can define their own header files ( .h ) that can be included (#include) by other programs. Describe what happens when a program includes a .h file AND describe what should and should not be placed in a .h file.
12. (35 pts) Write the C++ code that defines a class named “employee” as describe below. Also write the code to implement the methods for the employee object. The object must include:
ü an integer to hold the number of hours per week the employee works
ü a double number to hold the employee’s hourly pay rate
ü two constructors:
o no parameters that sets the values to 0
o two parameters that sets the id and pay rate to the provided values
ü set methods for both data members
ü get methods for both data members
ü a % (percent) method that calculates the amount of tax that will be withheld based upon an unknown tax rate. This method must be able to be called as:
answer = Fred % taxRate; // Fred of type employee, answer and taxRate are double
You do NOT need to write a main program.