overloading in c

Posted under C++ Course On By mohammed.mutawe

Overloading in c

The C language allows you to deal with variables in the same function as it enables you to use values ​​in functions, and reserve a special variable in the same function, and how is that?

 

Definition

The idea of ​​overloading is based on defining values ​​that are passed from the function header, to do its work inside, which greatly reduces the number of retro functions or void functions in our code.

For this, the so-called overloading functions were used.

 

How to use?

First we will make an object that will put the function inside it to make things work fine, then we will call that function from the outside and print more than one variable inside it, let’s start the application:

  • We create an object and name it, so the code is as follows:
class overLoading {
public:
    void print(int i) {
        std::cout << "create int: " << i << std::endl;
    }
    void print(double  f) {
        std::cout << "create float: " << f << std::endl;
    }
    void print(std::string c) {
        std::cout << "create string: " << c << std::endl;
    }
};

 

We note here that the function is repeated inside the object with one name, which is print, and between parentheses we define different variables every time we repeat them, and this is the principle of overload.

 

  • Now we define a variable of type Object and name it ov, for example, so that it becomes as follows:
int main()
{
    overLoading ov;



    return 0;
}

 

  • Ok fine, now we’re going to call the function via this object, and then put a value every time we call it so the code will look like this:
int main()
{
    overLoading ov;


    ov.print(20); // overload int

    ov.print(333.283);//overload float

    ov.print("Hello World C++");//overload string 


    std::cout << "\n";
    system("PAUSE");

    return 0;
}
  • And now that we have finished the program and when it is running, the result of the execution will appear in the console screen as follows:

 

The uses of overload are many in C++ and other languages, but it is flawed by forgetting the function of some of those variables and what each variable does in large projects, so we must use a powerful code editor to help us remember information in addition to writing comments in lines of objects.

 

Example

Please, to understand the overloading well, copy the following code into your code editor.

#include <string>
#include <iostream>



class overLoading {
public:
    void print(int i) {
        std::cout << "create int: " << i << std::endl;
    }
    void print(double  f) {
        std::cout << "create float: " << f << std::endl;
    }
    void print(std::string c) {
        std::cout << "create string: " << c << std::endl;
    }
};


int main()
{
    overLoading ov;


    ov.print(20); //overload int 


    ov.print(333.283);//overload float 


    ov.print("Hello World C++");//overload string 

    std::cout << "\n";
    system("PAUSE");

    return 0;
}

 

Leave a comment

Your email address will not be published. Required fields are marked *