Lecture 7 Methods
Lecture 7 Methods
Method call:
▪ A statement that invoking the name of a method.
▪ It may pass arguments
▪ Argument=The values passed to the method parameters ▪ Message=
A method call on an object.
Notes:
Advantages of methods:
• The program can be divided into logical blocks. This makes the code
clear and easy to understand.
BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I
Disadvantges of methods
• Prone to errors related to data types, their conversions while passing
arguments to functions.
• Function call overhead
• Challenges when naming variables
Example: In the example below, the method Add() will add two integers then
return the result to the calling method, in this case main().
BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I
Method calling:
▪ Process of invoking the method name so the method can perform the required
task.
▪ Can be done in three ways:
a) call and pass nothing
b) call by value
Method calling…continued.
BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I
class Calculations
class Calculations {
{ int Add(int a,int b) int Add(int a,int b)
{
{ return a+b;
return a+b; }
} int Add(int x,int y,int };
z) class Arithmetic: public Calculations
{ int Add(int x,int y,int z)
{ {
return x+y+z; return x+y+z;
} }
}; };
Method overriding vs Function overloading
• Inheritance: Overloading can occur without inheritance. Overriding of
methods occurs when one class is inherited from another class.
• Method signature: Overloaded methods must differ in method
signature .i.e. either number of parameters or type of parameters
should differ. In overriding, method signatures can be same.
• Scope of methods: Overridden methods are in different scopes;
whereas overloaded methods are in same scope.
• Behavior of methods: Overriding is needed when derived class
methods has to do some added or different job than the base class
method. Overloading is used to have same name methods which
behave differently depending upon parameters passed to them.
• Polymorphism: Method Overloading is used in compile time
polymorphism while method overriding is used for run time
polymorphism.
BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I
Recursive method: This is a method which calls itself. Can be used in cases where some repetitive work is
required. The recursion continues until some condition is met(base condition/case).
Example: A method to calculate factorial
BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I
Inline method: This is a method in which the compiler places a copy of the code of the method at each
point where the method is called at compile time.
Syntax: inline return-type function-
name(parameters){ // function code
}
Example: An inline method to calculate the sum of two numbers.
Friend method: A function defined or declared using keyword 'friend' before the function prototype
inside the class. It takes objects as parameter and access their private members using object name
and dot(.) operator. Friend function is used when we need to operate on data of two or more
objects of same or different classes.
BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I