what is fuanction overriding?
Printable View
what is fuanction overriding?
function overidding is noting but a function have same name which present in both base class and derive class,we use virtual mechanisim in function overridding,but overloading occur between a class.not in 2different class.ok
Function overriding enables us to write multiple functions with the same name.The compiler differentiates the function depending on the number of and types of the parameters the function takes.
Example
Class num
{
int sum();
int sum(int i, int j);
int sum=i+j; return(sum);
}
There is a difference between function [i]overloading[/i] and function [i]overriding[/i].
You can [i]overload[/i] a function or method by declaring or defining multiple functions or methods with the same name in the same scope, but with different signatures:
[code]
int foo(void);
int foo(int);
int foo(double);
void foo(int, int);
[/code]
The compiler figures out which of these to call based on the number and types of parameters; if you write [font="Courier New"]foo()[/font], the first version will be called. If you write [font="Courier New"]foo(3.14159)[/font], then the third version will be called.
You can [i]override[/i] a method by inheriting it from a base class, then changing the implementation in the child class:
[code]
class base {
public:
void foo() { cout << "in base::foo" << endl; }
};
class derived : public base {
public:
void foo() { cout << "in derived::foo" << endl; }
};
[/code]
The definition of [font="Courier New"]foo[/font] in [font="Courier New"]derived[/font] [i]overrides[/i] the definition in [font="Courier New"]base[/font]. The method [i]signature[/i] (return type and parameters) is the same between the parent and child class, but the [i]implementation[/i] in the child class is different, and they occur in different scopes.
Hopefully that made sense.