English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

3 answers

Function Overloading:

two or more functions may have the same name, so long as the parameter types are sufficiently different enough to distinguish which function is intended. A function may not be overloaded on the basis of its return type.

Example:
void f(int) {}

void f(long) {}

Function Overriding:
Suppose you declare a virtual function named f in a class A, and you derive directly or indirectly from A a class named B. If you declare a function named f in class B with the same name and same parameter list as A::f, then B::f is also virtual (regardless whether or not you declare B::f with the virtual keyword) and it overrides A::f

Example:
class A {
virtual void f() { cout << "Class A" << endl; }
};

class B: A {
void f() { cout << "Class B" << endl; }
};

Hope this helps!

2007-03-07 15:00:00 · answer #1 · answered by Rainmaker 2 · 0 0

Function Overloading :

void SetAString(const char* val) {m_someString = val;}
void SetAString(const std::string& val) {m_someString = val;}
etc..
the functions are distiguinished based on the parameters.

Function Overiding :

class B
{
virtual void SaySomething(){::MessageBox(NULL, "HELLO", NULL, MB_OK);}

}


class A : public class B
{
virtual void SaySomething(){::MessageBox(NULL, "NO", NULL, MB_OK);}
}

the result of calling SaySomething depends on the subtype of
the object

1. Overload - two functions that appear in the same scope are overloaded if they have the same name but have different parameter list
2. main() cannot be overloaded
3. notational convenience - compiler invokes the functions that is the best match on the args – found by finding the best match between the type of arg expr and parameter
4. if declare a function locally, that function hides rather than overload the same function declared in an outer scope
5. Overriding - the ability of the inherited class rewriting the virtual method of a base class - a method which completely replaces base class FUNCTIONALITY in subclass
6. the overriding method in the subclass must have exactly the same signature as the function of the base class it is replacing - replacement of a method in a child class
7. writing a different body in a derived class for a function defined in a base class, ONLY if the function in the base class is virtual and ONLY if the function in the derived class has the same signature
8. all functions in the derived class hide the base class functions with the same name except in the case of a virtual functions which override the base class functions with the same signature

2007-03-07 15:13:45 · answer #2 · answered by Rishi 3 · 0 0

Overloading - multiple instances of the same function that have different signatures by accepting different parameters. For example:

GetUser(int userID)
{
//looks up user by ID number and returns User object
}

GetUser(string username)
{
//looks up user by username and returns User object
}


Overriding functions means exactly what overriding sounds like - replacing the existing function definition with your own. So when the function is called, the overridding code is executed instead of the original.

2007-03-07 14:54:04 · answer #3 · answered by Rex M 6 · 0 0

fedest.com, questions and answers