My Blog List

Saturday, December 21, 2019

Function overloding in cpp.

 Function overloading in cpp.
       In this tutorial we learn how to overload the function in c++.
Firstly,

 What is mean by function overloading?

Ans:Function overloading is the feature of cpp language.It means that the two or more function having same name.It can be one of the example of Polymorphism.The function name is same but the return type and parameter given to the function is different.

Syntax:
     function-return-type function name(parameter)
    declare another function as same name but different parameter
 
For Example:
      suppose we have a function for calculate addition of two number and three number.
     int add(int num1,num2);
and another is,
     int add(int num1,int num2 int num3);

Here following code shows a area function overloading concept to calculate the different area for different shape. 

#include<iostream.h>
#include<conio.h>
int area(int s);
int area(int l,int b);
double area(double bs,double hg);
double area(double r);
int main()
{
    clrscr();
    cout<<"\nArea of square="<<area(5)<<endl;
    cout<<"\nArea of Rectangle="<<area(10,5)<<endl;
    cout<<"\nArea of Triangle="<<area(4.0,10.0)<<endl;
    cout<<"\nArea of circle="<<area(6.0);
    getch();
    return(0);
}
int area(int s)
{
    return(s*s);
}
int area(int l,int b)
{
    return(l*b);
}
double area(double bs,double hg)
{
    return(bs*hg/2);
}
double area(double r)
{
    return(3.14*r*r);
}


OUTPUT:



No comments:

Post a Comment

Multiple Inheritance in C++.

     In this tutorial Multiple Inheritance in c++. On the basis of Inheritance concept the another type of inheritance is the Multiple I...