what is fuanction overriding?
This is a discussion on c++ within the C and C++ forums, part of the Software Development category; what is fuanction overriding?...
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 overloading and function overriding.
You can overload a function or method by declaring or defining multiple functions or methods with the same name in the same scope, but with different signatures:
The compiler figures out which of these to call based on the number and types of parameters; if you write foo(), the first version will be called. If you write foo(3.14159), then the third version will be called.Code:int foo(void); int foo(int); int foo(double); void foo(int, int);
You can override a method by inheriting it from a base class, then changing the implementation in the child class:
The definition of foo in derived overrides the definition in base. The method signature (return type and parameters) is the same between the parent and child class, but the implementation in the child class is different, and they occur in different scopes.Code:class base { public: void foo() { cout << "in base::foo" << endl; } }; class derived : public base { public: void foo() { cout << "in derived::foo" << endl; } };
Hopefully that made sense.