Search This Blog

Tuesday 19 January 2016

Simple Function overloading in C++

 In C++ we are allowed to make only one function in class and we can use it with different ways.
just look over an example below I have created class name gajjar and in main its object is k I've used function karan() to overload in different requirements!

#include<iostream.h>
#include<conio.h>
class gajjar
{
    public:
    void karan(int i,int j)
    {
        cout<<"Addition is:"<<i+j<<endl;
    }
    void karan(float k)
    {
        cout<<"Division by 2 is:"<<k/2<<endl;
    }
    void karan(char k[])
    {
        cout<<"Hi "<<k<<endl;
    }
};
void main()
{
    gajjar k;
    cout<<"\nEnter two values to add:";
    int a,b;
    cin>>a;
    cin>>b;

    k.karan(a,b);

    cout<<"\nEnter float value to divide by 2:";

    float z;
    cin>>z;
    k.karan(z);

    char str[10];
    cout<<"\nEnter your first name:\n";
    cin>>str;
    k.karan(str);
    getch();
}

No comments:

Post a Comment