Overloaded Functions...Can someone explain it to me

This is a discussion on Overloaded Functions...Can someone explain it to me within the C++ Programming forums, part of the General Programming Boards category; I don't really understand it that well, can someone explain it or give me a link to a good tutorial, ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    Overloaded Functions...Can someone explain it to me

    I don't really understand it that well, can someone explain it or give me a link to a good tutorial, I'd really appreciate it. A sample code of how overloaded functions work would also be nice.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Function overloading allows you to choose the same name for functions, and give them different signatures ("signature" refers to argument list). This allows the compiler to choose which "version" of a particular function to use based on the arguments passed to it.

    For example:

    int func(int i, char *string);
    double func(char *string);
    int func(char code);

    Careful of ambiguosity, something like this is quite confusing:

    int max(int i1, int i2 = 0);
    int max(int i1);

    Calling max(4); will generate an error, as the function call matches two function signatures.

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    In addition to what Elbro said you can use overloading to overload operators for user defined datatypes, which is also very useful.

  4. #4
    PuterPaul
    Guest
    Is this also known as Polymorphism????

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    No that would be something like this

    // Dog is ofcourse derived from mamel

    Mamel *theDog = new Dog;
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  6. #6
    PuterPaul
    Guest
    I thought that Polymorphism was defined as having functions with the same names but different funtionalities such as

    Code:
    door.open();
    account.open();

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    class animal
    {
      public:
        virtual void speak() {};
    };
    
    class dog : class animal
    {
      public:
        void speak() {cout << "bow wow" << endl;}
    };
    
    class cat : class animal
    {
       public:
         void speak() {cout << "prrrrrr...." << endl;}
    };
    When I think of polymorphism I think of a function with the same name and the same parameters, but different response based on the class that it is being used in. See the speak() function above. Thus it polyorphism is similar to overloading--the functions have the same name but different output for in both-- but different as well--polymorphic functions have the same parameters whereas overloaded functions don't.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  2. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  3. Plz explain difference between functions returning ref
    By dudeomanodude in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2008, 02:03 PM
  4. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 11:56 AM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21