MidTerm SP13 Solutions

Section A - MCQs

1. C 2. C 3. D 4. C 5.D 6. B
7. B 8. D 9. D 10.D 11.C 12. A



Section B - SAQs

1. Java has new but no delete operator to release the memory taken on the heap (the free store)
delete needs to be called by the programmer in C++ but not in Java.
C++ has no garbage collection but Java has garbage collection which releases the memory on the heap.


2.

int *i1 = new int; *i1 = 7;
int *i2;
i2 = i1;
delete i1; delete i2;

In the above code both pointers i1 and i2 point to the same location (for an int) so just
one delete is enough. Doing delete twice in the above case will cause a double free error.

3.

int *p1 = new int; int *p2 = new int;
p1 = p2;
delete p1; delete p2;

In the above code we are using two different locations on the heap. However due to assignment (p1=p2)
we have two problems:
  1. we loose the pointer to the first location on the heap so the memory which p1 was pointing to is never released causing a memory leak.
  2. the code generates a double free error because we are deleting the same memory twice since both p1 and p2 point to the same location.

The solution for this problem is to save the address to which p1 was pointing into another pointer (say p3) and delete it so that there is no memory leak. And to prevent the double free error delete only p1 or p2 since both of them point to the same location.

int *p1 = new int; int *p2 = new int;
int *p3 = p1; //we loose the address of p1 in the next line so save that into p3
p1 = p2;
delete p1; delete p3;

4. See lecture slides


5. Inheritance and templates.



Section C - LAQs

1. a) see lecture slides for definition of subtype and explanation

b) Both PostgreSQLConn and MySQLConn though subclasses are not subtypes as they have behaviours which are not in the base AbsDBConn class. Because of this we cannot use the derived classes polymorphically as shown below:

AbsDBConn *mysqlConn = new MySQLConn();
int connCount = mysqlConn->getConnectionCount(); //does not work

The answer:
To make the two classes PostgreSQLConn and MySQLConn into proper subtypes of AbsDBConn we need to include all the methods that these classes have into the base AbsDBConn abstract class. See the complete solution below:

class AbsDBConn { //abstract AbsDBConn interface
     public:
          virtual void open() = 0;
          virtual void close() = 0;
          virtual bool isOpen() = 0;
          virtual int getConnectionCount() = 0;
          virtual bool isCached() = 0;
};
 
class PostgreSQLConn : public AbsDBConn {
     public:
         void open() { ........}
         void close() {........}
         bool isOpen() {bool bOpen = .....; return bOpen;}
         int getConnectionCount() { int connCount;..........return connCount;}
         bool isCached() { bool bCached = ...; return bCached;}    
};
 
class MySQLConn : public AbsDBConn {
     public:
           void open() { ........}
           void close() {........}
           bool isOpen() {bool bOpen = .....; return bOpen;}
           int getConnectionCount() { int connCount;..........return connCount;}
           bool isCached() { bool bCached = ...; return bCached;}
};

2.
a) For covariant types in C++, the return type of the method overridden in the derived class is a subtype of the return type of the (overridden) method in the base.
So the method makeDoor() in the derived GlassDoorFactory returns a GlassDoor which is a subtype of Door.

class DoorFactory {
    public:
        virtual Door *makeDoor() { return new Door(); }
};
 
class GlassDoorFactory : public DoorFactory {
    public:
         GlassDoor *makeDoor() { return new GlassDoor(); } //the answer - return type GlassDoor is subtype of Door
};

b) For contravariance in C++, first remember that it is usually applied to the arguments of methods in derived classes. C++ allows contravariance as discussed in the new slides for this topic on the website.

What contravariance means is that whenever there is a subtype S (of a supertype T) used as an argument in a method of the parent class then in derived class's overridden method that argument can be replaced with a supertype of S which is T. This is allowed in C++ even though it may not look very logical since the derived class's method arguments are more general than the base class.

class DoorQACheck {
    public:
        virtual bool doQACheck(GlassDoor *glassDoor) { bool b; ......; return b;}
};
 
class DoorISO9420QACheck : public DoorQACheck {
    public:
         bool doQACheck(Door *door) { bool b; ......; return b;} //the answer - the argument Door is supertype of GlassDoor
};

c) Diamond problem due to multiple inheritance of the common ancestor (Door) twice.
This is solved by using virtual inheritance as shown below:

class Door { };
class GlassDoor : public virtual Door { };
class WoodenDoor : public virtual Door { };
class WoodenGlassDoor : public GlassDoor, public WoodenDoor { };
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License