Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

6CCYB040 OBJECT ORIENTED PROGRAMMING (Sample Exam)

Part A

Compulsory part. Answer all questions in Part A.

Question A.1

(a)  Briey describe what is meant by the procedural and object-oriented pro-

gramming paradigms.

(b)  List four basic data types available in C++.

(c)  Describe what a constant is in C++, and explain why it is sometimes de- sirable to use them.

(d)  Explain what is meant by variable scope in C++. What are the three types of scope?

(e)  Explain the meaning of the  public ,  private and  protected keywords

in C++ classes. What access level does each of them provide?

Question A.2

The program listing below is intended to compute the volume of a sphere, according to the formula V =  .  However, the program contains 5 errors. State what each of the errors are (indicating the line number), and clearly describe how you would correct each one.

Question A.3

Below is shown the contents of a text le called data.txt.

78

56

123

97

83

84

91

79

100

84

The le contains data about hospital patients’ blood pressures. It is known that there are 10 values in the le.  Write a C++ program to read the contents of this file into a 1-D array of integers called pressures. Make sure your program includes all appropriate header les.   It should also check if there are any problems when opening the le, and display an appropriate error message if there is a problem.

Question A.4

Write an iterative C++ function that nds and returns the maximum value in an array of integers.  The function should take the array to be searched as an

argument. Also use a pass-by-reference argument to return the index of the maximum element (i.e. which position it was at in the array).

Part B

Answer any 2 of the following 4 questions.

Question B.1

(a)  Explain the difference between a class and an instance in C++.

(b) When are class constructors and destructors called?

(c)  Explain how C++ implements the key object-oriented concept of informa- tion hiding. Why is this seen as being a useful concept in object-oriented programming?

(d)  Explain the meaning of the const keyword when used after a C++ class

member function.

(e) Write a C++ class to store information about polygons.

A polygon is represented by a sequence of 2D points (you can assume that a Point class exists to store these), although different polygons have different numbers of points.  You can assume that no polygon will have more than 100 points. Each polygon should be able to compute its area.

You don’t need to write any function bodies, just write the class definition showing the data members and any member function signatures.  Use information hiding in your class, include a mutator to set the points and an inspector to read a point given an index. Include a single default con- structor.

(f)  Modify the polygon class to add an extra data member that should store the total number of polygon instances that exist. Add an inspector func-

tion to access this data member’s value (write the function body for this inspector).

Question B.2

(a)  Explain the difference between a unary and binary operator in C++ and give an example of each.

(b)  Explain the difference between inx, prex and postx operators in C++

and give an example of each.

(c) When overloading operators for classes in C++, the overloaded function can either be a global function or a class member function. What factors should you consider when making this choice?

(d)  If an overloaded operator function is global how can it be given access to private and protected class members?

(e)  Below is shown a definition for a class to represent patient data.  The class contains three string data members and two constructors: one de-

fault constructor and a second one that takes three string values to ini- tialise the data members.

class   Patient   {

public :

Patient ();

Patient ( string  name ,   string   NHSnum ,   string   GPname ); private :

string   _name ;

string   _ NHSnum ;

string   _ GPname ;

};

Write overloaded operator functions for the output and input operators, i.e.  << and >>.  Write both the function signatures and bodies.  Indicate clearly whether the functions are global or member functions, and if any further changes to the class definition are required.

(f)  Is it necessary to overload the assignment operator for the Patient class?

Explain your answer.

Question B.3

(a)  Explain the motivation for using templates in C++. Are templates instanti- ated at compile-time or run-time?

(b)  Below is shown an implementation for a function called Swap that takes two integer arguments. The function is intended to swap the values of the

two arguments.

void   Swap   ( int  x ,   int  y )

{

int   temp ;

temp  =  x ;

x  =  y ;

y  =  temp ;

}

The function currently does not work. Identify the error(s) in the code and describe how you would x the code to make it work as intended.

(c)  Now modify the Swap function to make it a function template. Use a type template parameter for the types of the two arguments.

(d) What argument types will it be possible to instantiate your Swap function for?

(e) Write a class definition for a class template to represent 2 × 2 matrices.

There should be a type template parameter to specify the type of the matrix coefficients.  The matrix coefficients should be private.   Include two constructors: one default constructor and one that takes four values to initialise the matrix coefficients. You do not need to write the function bodies – just write the class definition showing the data member(s) and function signatures.

(f)  Explain the meaning of the term polymorphism in object-oriented pro-

gramming.

Do templates offer a way of achieving polymorphism in C++?

Question B.4

(a)  Explain the three main types of relationships between objects in object- oriented design, and describe how each one can be implemented in C++.

(b) You are a member of a team that is developing a computer adventure

game.

In the game there is a maze, consisting of a number of rooms and cor- ridors. All rooms have 4 doors, each of which can lead to another room or corridor.  Corridors contain only 2 doors.  Each location (i.e.  room or corridor) has a number and a name. Both rooms and corridors can con- tain items. There are a number of different types of item in the maze but all items have a name and a monetary value.  Doors can be either open or closed. The aim of the game is for a player to move around the maze and collect certain items to win the game. At any time, the player has a current location (the number of the location) and a set of items that she is carrying. The program should also store the players name.

i. Give a list of objects for the adventure game. Show clearly what rela- tionships there are between the objects and what type of relationships they are. Briefly justify why you chose particular relationships.

ii. Write a UML class diagram for the objects and relationships you iden- tified in part (i).

You only need to show class names, attributes and relationship types in your diagram (i.e. don’t include any behaviours).