Many programs written with inheritance could be solved with composition instead, and vice versa. Discuss the relative merits of these approaches in the context of the Point, Circle, Cylinder class hierarchy in this chapter. Rewrite the classes in Figs. 9.6–9.8 (and the supporting programs) to use composition rather than inheritance. After you do this, reassess the relative merits of the two
approaches both for the Point, Circle, Cylinder problem and for object-oriented programs in general.
What will be an ideal response?
The composition approach more accurately reflects the relationships among points, circles and cylinders. Circles and cylinders have points, and cylinders have circles. Circles and cylinders are not points, and cylinders are not circles, which is the relationship that inheritance implies.On the other hand, the code implementing inheritance is shorter and easier to read. When using composition, the clients of the Cylinder class are required to know the interfaces to the Point and Circle classes, as well.
```
# Definition and test function for class Point.
class Point:
"""Class that represents a geometric point"""
def __init__( self, x = 0, y = 0 ):
"""Point constructor takes x and y coordinates"""
self.x = x
self.y = y
def __str__( self ):
"""String representation of a Point"""
return "( %d, %d )" % ( self.x, self.y )
# main program
def main():
point = Point( 72, 115 ) # create Point instance
# print point attributes
print "X coordinate is:", point.x
print "Y coordinate is:", point.y
# change point attributes and output new location
point.x = 10
point.y = 10
print "The new location of point is:", point
if __name__ == "__main__":
main()
```
X coordinate is: 72
Y coordinate is: 115
The new location of point is: ( 10, 10 )
```
# Definition and test function for class Circle.
import math
from PointModule import Point
class Circle( Point ):
"""Class that represents a circle"""
def __init__( self, radius = 0.0, x = 0, y = 0 ):
"""Circle constructor takes radius and center points"""
self.center = Point( x, y )
self.radius = float( radius )
def area( self ):
"""Computes area of a Circle"""
return math.pi * self.radius ** 2
def __str__( self ):
"""String representation of a Circle"""
return "Center = %s Radius = %f" % \
( self.center, self.radius )
# main program
def main():
circle = Circle( 2.5, 37, 43 ) # create Circle instance
# print circle attributes
print "X coordinate is:", circle.center.x
print "Y coordinate is:", circle.center.y
print "Radius is:", circle.radius
# change circle attributes and print new values
circle.radius = 4.25
circle.center.x = 2
circle.center.y = 2
print "\nThe new location and radius of circle are:", circle
print "The area of circle is: %.2f" % circle.area()
if __name__ == "__main__":
main()
```
X coordinate is: 37
Y coordinate is: 43
Radius is: 2.5
The new location and radius of circle are: Center = ( 2, 2 ) Radius =
4.250000
The area of circle is: 56.75
```
# Definition and test function for class CylinderModule.
import math
from PointModule import Point
from CircleModule import Circle
class Cylinder:
"""Class that represents a cylinder"""
def __init__( self, height, radius, x, y ):
"""Constructor for Cylinder takes height, radius, x and y"""
self.bottom = Circle( radius, x, y )
self.height = float( height )
def area( self ):
"""Calculates (surface) area of a Cylinder"""
return 2 * self.bottom.area() + \
2 * math.pi * self.bottom.radius * self.height
def volume( self ):
"""Calculates volume of a Cylinder"""
return self.bottom.area( self ) * height
def __str__( self ):
"""String representation of a Cylinder"""
return "%s; Height = %f" % ( self.bottom, self.height )
# main program
def main():
# create Cylinder instance
cylinder = Cylinder( 5.7, 2.5, 12, 23 )
# print Cylinder attributes
print "X coordinate is:", cylinder.bottom.center.x
print "Y coordinate is:", cylinder.bottom.center.y
print "Radius is:", cylinder.bottom.radius
print "Height is:", cylinder.height
# change Cylinder attributes
cylinder.height = 10
cylinder.bottom.radius = 4.25
cylinder.bottom.center.x, cylinder.bottom.center.y = 2, 2
print "\nThe new location, radius and height of cylinder are:", \
cylinder
print "\nThe area of cylinder is: %.2f" % cylinder.area()
if __name__ == "__main__":
main()
```
X coordinate is: 12
Y coordinate is: 23
Radius is: 2.5
Height is: 5.7
The new location, radius and height of cylinder are: Center = ( 2, 2 )
Radius = 4.250000; Height = 10.000000
The area of cylinder is: 380.53
You might also like to view...
Create the data flow description for the SOFTWARE INSTALLATION LIST. This flow contains information on specific software packages and the machines on which the software should be installed. The composition should include the SOFTWARE INSTALLATION LISTING, a data structure.
What will be an ideal response?
________ refers to the information about your surfing stored by a Microsoft Edge as you browse the Web
A) Web history B) Browsing record C) Site navigation D) Browsing history
________ are functions that manipulate data to clean it up and make it useful
A) Data verification B) Data cleansing C) Data functions D) Text functions
As long as the types are the same, one array can be copied to another using the assignment operator.
Answer the following statement true (T) or false (F)