August 1999
OP216 : OBJECT ORIENTED PROGRAMMING

QUESTION 4

Total Marks: 20 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 4

(a) Define the term inheritance with respect to object oriented programming. [2]
  • Inheritance is creating a new data type;
  • Which contains all of the members of a parent (original) data type;

 

(b) The government requires a program to keep records of all the buildings in the city. Each building has a number of floors (an integer), and address (a string) and a rental cost (an integer).

Implement the class building that contains :

  • These protected data items;
  • An accessor method for the rent;
  • A method Advertise, which prints out the address of the building and its rental cost.
[5]
A sample definition of Building follows:

class Building {
protected:
    int floors;
    char* address;
    int cost;

public:
    int rental_cost( ) {return cost;} ;
    void Advertise( ) {
        cout << address << ": $" << cost << endl ;
    } ;
} ;

 

(c) A House is a building that has a number of residents, and rent of 200 dollars per resident. Use inheritance to implement a class House, with a constructor that takes the parameters the number of residents, the address, and the length of the address, and initializes all of its members appropriately. All members which are not passed as parameters to the constructor should be initialized to 0. [5]
A sample definition of House follows:

class House : public Building {
protected:
    int residents;

public:

    House(int res, int fl, char* add, int address_length) {
        address= (char*)malloc(address_length);
        strcpy(add, address);
        residents= res;
        floors= fl;
       cost= 200*residents;
    } ;
} ;

 

(d) An estate is a collection of buildings. Consider the following definition of a class Estate :

class Estate {
private :
   ...
public :
   Estate();                //The constructor
   Building current();  // Returns the current building in the estate.
   void next();           // Move to the next building in the estate.
   int finished();        // Return 1 if all the buildings have been examined
                             // 0 otherwise
}

Give an implementation of a function total_rental_value that takes a reference to an estate, and returns the total rental value of all the buildings on the estate.

[6]
A sample definition of total_rental_value follows:

int total_rental_value(Estate &e) {
   int total= 0;
   for(; !e.finished( ); e.next( ) )
      total += (e.current( ) ).rental_cost( );
   return total;
}

 

(e) In the Estate class, there is a problem that relates to calculating the rental cost of houses. Identify, and explain the nature of this problem. [2]
The problem is that
  • The Estate holds objects of type Building; while the rental cost of a House is dependent upon a member variable in the House class; (1 mark)
  • And the inheritance tree does not allow us to look down the tree for types which are not defined in the base class; (1 mark)