| 1. |
Write a program to find out volume of a cube, cuboid and cylinder using function overloading. |
|
Answer» from functools import wraps import math def overloaded(func): @wraps(func) def overloaded_funcfargs, **kwargs): for f in overloaded_func.overloads: try: return ff args, **kwargs) except TypeError: pass 1 else: # it will be nice if the error message prints a list of # possible signatures here raise TypeError(“No compatible signatures”) def overload_with(func): overloaded_func.overloads.append(func) return overloaded_func overloaded_func.overloads = [func] overloaded_func.overload_with = overload_with return overloaded_func ############# @overloaded def volume(): print ‘Volume’ pass @volume.overload_with def _(a): print ‘Volume of cube=’,a*a*a, ‘cubic units’ pass @volume.overload_with def _(a,b): print ‘Volume of cylinder, 3.14*a*a*b,‘cubic units’ pass @volume.overload_with def _(a,b,c): print ‘Volume of cuboid=’, a*b*c, ‘cubic units’ pass choice = input(“Enter 1-cube 2-cylinder 3-cuboid”) if choice==1: side = input(“Enter side”) volume (side) elif choice ==2: radius = input(“Enter radius”) height = input( “Enter height”) volume(radius,height) elif choice==3: length = input(“Enter length”) breadth = input(“Enter breadth”) height = input(“Enter height”) volume (length, breadth, height) else: print “Invalid choice” |
|