2nd puc computer science chapter 8 function overloading ,types of function overloading ,syntax function overloading ,example function overloading
inline function, friend function ,
1 of 7
Download to read offline
More Related Content
2nd puc computer science chapter 8 function overloading
1. 1 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
CHAPTER 8
FUNCTION OVERLOADING
Polymorphism is one of the main concepts of object oriented programming. Poly means many, and morph
means form. So Polymorphism is many formed.
There are two types of Polymorphism- Compile time polymorphism and Run time polymorphism.
Compile time polymorphism- in this type, code associated with a function call is known at the compile
time itself. Ex: Function overloading and operator overloading.
Runtime polymorphism- in this type, the code associated with the function call is known only during run
time(execution time). It is also known as Dynamic binding.
Function overloading:
The process of using same function name but different number and type of arguments to perform
different tasks is known as function overloading.
Function signature:
Argument list of a function that gives the number of arguments and types of arguments is called Function
signature.
Two functions are said to have same function signature if they use same number of arguments and type of
arguments. Function name can be different.It doesnot include return type of function.
Ex:
void function2(int x,float y,char ch);
void function5(int a,float b,char c); //function2() and function5() have same signature
Declaration and definition of function overloading:
To overload a function, we must have
1. Function names should be similar
2. All the functions should have different signatures.
Example: a sum() function to find sum of integers, real numbers and double.
In the above question, three member functions are needed.
int sum(int,int);
float sum(float,float);
double sum(double,double);
When functions are overloaded, compiler determines which function has to be executed based on
the number and type of arguments.
void SUM(int x, double y);
int SUM(int x,double y);
//generates syntax error because number.of arguments and their datatypes are same.
2. 2 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
//program using function overloading without class
#include<iostream.h>
void display(int);
void display(float);
void display(char);
void main()
{
int a;
float b;
char c;
clrscr();
a=10;
b=45.78;
c=&;
display(a);
display(b);
display(c);
getch();
}
void display(int x)
{
cout<<integer value=<<x<<endl;
}
void display(float y)
{
cout<<Real number=<<y<<endl;
}
void display(char z)
{
cout<<Character=<<x<<endl;
}
//program using function overloading with class
#include<iostream.h>
int AREA(int);
int AREA(int,int);
float AREA (float,float);
class funcoverload
{
public:
int area(int s)
{
return(s*s);
}
int area(int l,int w)
{
3. 3 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
return(l*w);
}
float area(float b,float h)
{
return(0.5*b*h);
}
};
void main()
{
funcoverload f; //creating object f
clrscr();
f.area(6); //accessing member function and passing integer value as argument
f.area(10,12);
f.area(3.5,6.8);
getch();
}
Advantages of function overloading:
1. Eliminates the use of different function names for same operation.
2. Helps to understand and debug easily
3. Easy to maintain code. Any changes can be made easy.
4. Better understanding of the relation between program and outside world.
5. Reduces complexity of program
Inline functions
Functions are used to save memory space but it creates overhead( passing arguments, returning values,
storing address of next instruction in calling function etc). To reduce the overhead and to increase the
speed of program execution, a new type of function is introduced--- INLINE function.
Inline function is a function whose function body is inserted at the place of function
call.
The compiler replaces the function call with the corresponding function body
Inline function increases the efficiency and speed of execution of program. But more memory is
needed if we call inline function many times.
Inline function definition is similarto a function definition but keyword inline precedes the
function name. It should be given before all functions that call it.
Syntax:
inline returntype functionname(argumentlist)
{
return(expression);
}
Inline function is useful when calling function is small and straight line code with few
statements. No loops or branching statements are allowed.
Advantages:
4. 4 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
1. Very efficient code can be generated.
2. Readability of program increase
3. Speed of execution of program increases
4. Size of object code is reduced
Disadvantage:
1. As function body is replaced in place of function call, the size of executable file increase and
more memory is needed.
Ex: Find cube of a number using inline function
#include<iostream.h>
inline double CUBE(double a)
{
return(a*a*a);
}
void main()
{
double n;
cout<<enter a number<<endl;
cin>>n;
cout<<Cube of <<n<<=<<CUBE(n);
getch();
}
Situations where inline functions may not work:
1. When inline function definition is to long or complicated
2. When inline functionis recursive.
3. When inline function has looping constructs
4. When inline function has switch or any branching statements or goto
Friend function:
Friend function is a non member function that can access the private and protected data
members of a class.
FUNCTION()
Friend function definition
Friend function declaration
Features of Friend function:
private
protected
Data or function
Data or function
friend FUNCTION();
..
..
Class A
5. 5 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
Keyword friend must precede the friend function declaration.
Friend function is a non member function of a class
It can access private and protected members of a class
It is declared inside a class and can be defined inside or outside the class. Scope resolution
operator is not needed to access friend function outside the class.
It receives objects of the class as arguments
It can be invoked like a normal function without using any object
It cannot access data member directly. It has to use object name and dot membership operator
with each member name.
It can be declared either in public or private part of a class.
Use of friend function is rare since it violates the rule of encapsulation and data hiding.
Ex: Program to find average of two numbers using friend function
#include<iostream.h>
class Sample
{
private:
int a,b;
public:
void readdata()
{
cout<<enter value of a and b<<endl;
cin>>a>>b;
}
friend float average(Sample s); //declaring friend function with object as argument
};
float average(Sample s) //defining friend function
{
return((s.a+s.b)/2.0) //accessing member a and b using object & dot operator
}
void main()
{
Sample x; //creating object
x.readdata();
cout<<average value=<<average(x) <<n; //calling friend function
}
A friend function can be used as a bridge between two classes.
#include<iostream.h>
class wife; //forward declaration that tells the compiler about classname before declaring it
class husband
{
private:
char name[20];
int salary;
public:
void readdata()
6. 6 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
{
cout<<input husband name;
cin>> name;
cout<<input husband salary;
cin>>salary;
}
friend int totalsalary(husband,wife);
}; //end of class husband
class wife
{
private:
char name[20];
int salary;
public:
void readdata()
{
cout<<enter wife name
cin>>name;
cout<<enter wife salary;
cin>>salary;
}
friend int totalsalary(husband,wife);
}; //end of class wife
int totalsalary(husband h,wife w)
{
return(h.salary+w.salary);
}
void main()
{
husband h;
wife w;
h.readdata();
w.readdata();
cout<<total salary of the family is <<totalsalary(h,w);
}
**
function over loading (32 question)
1What is a friend function? Write the characteristics of a friend
function.[2019]
2Define function overloading. Mention its advantages.[2019s]
7. 7 YouTube channel: study with me Ashwini e
Ashwini E ashwiniesware@gmail.com
3 Discuss overloaded functions with an example.[2018s]
4 Explain friend function with syntax and programming example.
[2018]
5 When function overloading is needed? Write any two advantages and
restrictions on overloading functions[2017s]
6 Explain inline function with programming example.[2017] [2015]
7 What are the advantages and disadvantages of inline
function?[2016s]
8 Describe briefly the use of friend function in C++ with syntax and
example.[2016]
9 What is function overloading? Explain the need for
overloading.[2015s][2020]