0% found this document useful (0 votes)
5 views25 pages

Lecture 7 Methods

Uploaded by

sharlynenyaboke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views25 pages

Lecture 7 Methods

Uploaded by

sharlynenyaboke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Methods in C++

▪ A named block of statements that perform a particular task in the program.


▪ A method declaration tells the compiler about a method name and how to call the
method.
Syntax:
Return type method_name(parameters);
▪ A C++ method definition consists of a method header and a method body. Here are all
the parts of a method:
Syntax:
return type method_name(parameters){
Function definition
}
▪ Return Type: A method may return a value. The return_type is the data type of the
value the method returns. If the method doesn’t return anything then use keyword
void.
▪ method Name: This is the actual name of the method.
BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I

▪ Parameters: A parameter is like a placeholder. When a method is invoked, you pass a


value to the parameter.
▪ method Body: The method body contains a collection of statements that define what
the method does.
Notes:
Method prototype:
▪ This is a declaration of the method that tells the program about the type of the value
returned by the method and the number and type of parameters.
▪ Consists of return type, method name, parameter list.
▪ Method prototype=method declaration.
▪ Importance of a method prototype:
✓ Gives the return type of the data that the method will return.
✓ Specifies the number of arguments to be passed to the method.
✓ Gives the data types of the each of the passed arguments.
✓ Gives the order in which the arguments are passed to the method.
Method signature:
▪ Refer combination of a method name and the parameter list.
▪ It the is part of the method declaration

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

• Helps to avoid code duplication.


• Individual methods can be easily tested.
• Makes it easier to make modification in the code.
• Makes it possible for programmers to collaborate in large projects.

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

Method calling… continued


BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I

b) call by value
Method calling…continued.
BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I

c). call by reference...using pointers


Method calling…continued.
BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I

c). Call by reference…using array


Method overloading: The same name of the function used more than once but each time
with different number and/or type of parameters. Overloading deals with multiple methods
in the same class with the same name but different signatures.
BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I
Method overiding: The same name of the function/method used more than once in base class and in derived class(es).
Overriding deals with two methods, one in a parent class and one in a child class, that have the same signature
BAC 2101/BBIT 203/BIT 2108: Object Oriented Programming I
Method overriding vs Function overloading
▪ Method overloading is the ability to use two or more methods in the
same class with the same name but different arguments.
▪ Method overriding on the other hand is the use of two methods with
same arguments but different implementations in the base class and
derived class.
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

You might also like