Concepts of Programming Languages

(Sean Pound) #1

532 Chapter 12 Support for Object-Oriented Programming


The use of multiple inheritance can easily lead to complex program organi-
zations. Many who have attempted to use multiple inheritance have found that
designing the classes to be used as multiple parents is difficult. Maintenance
of systems that use multiple inheritance can be a more serious problem, for
multiple inheritance leads to more complex dependencies among classes. It is
not clear to some that the benefits of multiple inheritance are worth the added
effort to design and maintain a system that uses it.
Interfaces are an alternative to multiple inheritance. Interfaces provide
some of the benefits of multiple inheritance but have fewer disadvantages.

12.3.4 Allocation and Deallocation of Objects


There are two design questions concerning the allocation and deallocation
of objects. The first of these is the place from which objects are allocated. If
they behave like the abstract data types, then perhaps they can be allocated
from anywhere. This means they could be allocated from the run-time stack
or explicitly created on the heap with an operator or function, such as new. If
they are all heap dynamic, there is the advantage of having a uniform method of
creation and access through pointer or reference variables. This design simpli-
fies the assignment operation for objects, making it in all cases only a pointer
or reference value change. It also allows references to objects to be implicitly
dereferenced, simplifying the access syntax.
If objects are stack dynamic, there is a problem with regard to subtypes. If
class B is a child of class A and B is a subtype of A, then an object of B type can
be assigned to a variable of A type. For example, if b1 is a variable of B type and
a1 is a variable of A type, then

a1 = b1;

is a legal statement. If a1 and b1 are references to heap-dynamic objects, there
is no problem—the assignment is a simple pointer assignment. However, if
a1 and b1 are stack dynamic, then they are value variables and, if assigned the
value of the object, must be copied to the space of the target object. If B adds
a data field to what it inherited from A, then a1 will not have sufficient space
on the stack for all of b1. The excess will simply be truncated, which could be
confusing to programmers who write or use the code. This truncation is called
object slicing. The following example and Figure 12.4 illustrates the problem.

class A {
int x;

...
};
class B : A {
int y;
...
}

Free download pdf