Page 228 - Computer_Science_F5
P. 228

of which method to invoke is made based  depending  on the  arguments  that  are
              on the actual type of the object. Runtime  supplied to the method.  A class can
              is the period of time that a computer’s  have numerous methods with the same
              processor    executes    a    compiled   name but different parameters or distinct
              program.  During this  time,  the  CPU  types of parameters, this is what method     Chapter Four: Object oriented programming with C++
          FOR ONLINE READING ONLY
              executes out the program’s instructions,  overloading  can do in  OOP. Because
              communicates  with  memory and  other  several  versions of the  method  can  be
              system  resources,  and  generates  the  called  based  on  the  unique  data  types
              desired output.                          or values provided as arguments, this
                                                       enables more flexible and intuitive code.

              (d) Method overloading behaviour in      In this case,  Python does not support
                   polymophism                         method overloading by default.
              Method  overloading  is  a  concept  in   The decision on which method  to
              object-oriented   programming      that  execute is made at compile time, based
              permits  the coexistence  of multiple    on the method signature.
              methods in a class with the same name but
              different parameters. As a result, a class   Compile-time polymorphism, sometimes
              may have more than one method with the   referred to as static polymorphism, is a
              same name, but each method must have     type  of  method  overloading.  It  offers
              a unique set of parameters. One type of   convenience  and  flexibility  in  method
              polymorphism is method overloading,      invocation by enabling a class to have
              which allows for the definition of many
              methods in a class with the same name    many methods with the same name but
              but distinct  parameters.  Depending on   distinct capabilities. Compile time is the
              the  arguments given,  the  compiler  or   period of time that a compiler uses to
              interpreter decides which version of the   translate  a program’s source code  into
              method to execute when a call is made.   machine code or an intermediate form.
              As an example, imagine we have a class   The activities done during compile time,
              named Calculator.                        are the compiler to do syntax checking,
                                                       type checking, and code optimisation.
              There  may  be more  than  one “add”
              method in this class, and the parameter       Exercise 4.4
              types used by each method may vary.
              We can, for instance, take two different   1.  Give an example of how
              “add” methods: one that accepts  three        polymorphism can improve code
              integer  parameters  and returns their        reusability and maintainability
              sum, and  another  that  accepts  three    2.  Imagine a class named “Animal”
              double  parameters  and  returns  their       with a method “makeSound()”.
              sum.  Various behaviors  are  allowed         In Object-Oriented Programming

                                                    219
               Student’s Book  Form Five



     Computer Science Form 5.indd   219                                                     23/07/2024   12:33
   223   224   225   226   227   228   229   230   231   232   233