Questions: 1. Define function overloading with a simple example. 2. What do you mean by function overloading and exaplain it with an example ? 3. Discuss about function overloading with varying number of arguments and data types.
Overloading
The
overloading is a most important feature of object oriented programming. This
feature enhances the capability of the method by allowing it to define it for
different types of data.
Normally
there are two types of overloading used in C++ ‒
1.
Operator overloading
2.
Function overloading
The
function overloading is a concept in which one can use many functions having the same
function name but can pass different
number of parameters or different types of parameters. For example, if there is
a function sum which performs addition operation then one can use overloading
functions like this ‒
int
sum(int a,int b);
int
sum(int a,int b,int c);
int
sum(int a,int b,int c,int d);
Similarly
the function overloading can be achieved like this ‒
int
sum(int a,int b);
float
sum(float a,float b);
double
sum(double a,double b);
char
sum(char a,char b);
That
means we can handle different number of parameters or different types of
parameters using the same function name.
Following
program takes different number of parameters and performs the task of addition.
Note that name of these functions is same and that is sum.
#include <iostream>
using namespace std;
class test {
private:
int
a,b,c,d;
public:
int
sum(int,int);
int
sum(int,int,int);
int
sum(int,int,int,int);
};
int test::sum(int a,int b)
{
retum
(a+b);
}
int test::sum(int a,int b,int c)
{
return
(a+b+c);
}
int test::sum(int a,int b,int c,int d)
{
return
(a+b+c+d);
}
void main()
{
test obj;
int a, b,
c, d,choice;
int
result=0;
cout<<"\n\t\t
Main Menu":
cout<"\n\t
1.Addition of two numbers";
cout<"\n\t
2.Addition of three numbers":
cout<<"\n\t
3.Addition of four numbers" <endl;
cout<"\n
Enter Your choice: ";
cin
choice;
switch(choice)
{
case 1:
cout<<"\nEnter 2 numbers: ";
cin >> a >> b;
result = obj.sum(a,b);
break;
case 2: cout<<"\nEnter 3 numbers: ";
cin >> a >> b >> c;
result = obj.sum(a,b,c);
break;
case 3:
cout<<"\nEnter 4 numbers: ";
cin
>> a >> b >>c>> d;
result = obj.sum(a,b,c,d);
break;
default:
cout<<"\nWrong Choice";
break;
}
cout<<"\n\nresult: " << result <<
endl;
getch();
}
Main Menu
1.Addition of two
numbers
2.Addition of three
numbers
3.Addition of four
numbers
Enter Your choice: 2
Enter 3 numbers: 10 20
30
result: 60
Now
following program will take the parameters of different data types. This makes
the function to overload.
#include<iostream>
using namespace std;
class test{
public:
int
sum(int,int);
float
sum(float,float);
double
sum(double,double);
};
int test::sum(int a,int b)
{
return
(a+b);
}
float
test::sum(float a,float b)
{
return
(a+b);
}
double test::sum(double a,double b)
{
return
(a+b);
}
void main()
{
test obj;
int
choice;
int a,b;
float
x,y;
double
m,n;
double
result=0;
cout<<"\n\t\t
Main Menu"
cout<<"\n\t
1.Addition of two integer numbers";
cout<<"\n\t
2.Addition of float numbers
cout<<"\n\t
3.Addition of double numbers"<<endl;
cout<<"\n
Enter Your choice: ";
cin
>> choice;
switch(choice)
{
case 1:
cout <<"\nEnter 2 numbers:
cin >> a >> b;
result = obj.sum(a,b);
break;
case 2:
cout<<"\nEnter 2 numbers: ";
cin
>> x >> y;
result = obj.sum(x,y);
break;
case 3:
cout <<"\nEnter 2 numbers: ";
cin
>> m >> n;
result = obj.sum(m,n);
break;
default:
cout<<"\nWrong Choice";
break;
}
cout
<<"\n\nresult: " << result << endl;
getch();
}
Main Menu
1.Addition of two integer
numbers
2.Addition of float
numbers
3.Addition of double
numbers
Enter Your choice: 2
Enter 2 numbers: 1.13
5.6
result: 6.73
Write a program to find
the area of a rectangle and triangle using function overloading
Solution :
#include <iostream>
using namespace std;
class Shape
{
public:
int
area(int length, int breadth);
float
area(float, int len, int br);
};
int Shape::area(int 1,int b)
{
int
result;
result =
1*b;
return
result;
}
float Shape::area(float half,int 1,int b)
{
float result;
result =
half*1*b;
return
result;
}
int main()
{
Shape
obj;
cout<<"\n
Area of Rectangle: " << obj.area(10, 20);
cout<<"\n
Area of Triangle: "<<obj.area(0.5, 10, 20);
return 0;
}
Area of Rectangle: 200
Area of Triangle: 100
1. Define function
overloading with a simple example.
2. What do you mean by
function overloading and exaplain it with an example ?
3. Discuss about
function overloading with varying number of arguments and data types.
Data Structures using C PlusPlus: Chapter 1: Data Abstraction and Overloading : Tag: Data Structure, C++, C Programing : Data Structures using C ++ Program - Function Overloading
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Electron Devices
EC25C01 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Circuits and Network Analysis
EC25C02 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Engineering Drawing - Laboratory
ME25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Data Structures using CPlusPlus - Laboratory
CS25C05 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Devices and Circuits Laboratory
EC25C03 2nd Semester ECE Dept | 2025 Regulation | 2nd Semester 2025 Regulation