C++/c++
Expert: Joydeep Bhattacharya - 6/16/2008
Questionwhat is function overriding and function overloading in c++?
what are the differences between them?
sir please give examples for those two concepts
AnswerHi Rajendra
First try n understand the basic difference between the two
OVERLOADING:
Both the function has same name but difference signature e.g.
void function_name(int x)
int function_name(float y)
NOTE: Function name should be same but the passing parameters to the function should be different. You cannot keep exactly same signature for any two the methods. Functions cannot be separated on the basis of its return type e.g.
Example #1(Functions having exactly same signature)
void function_name(int x) //WRONG
void function_name(int y) //WRONG
Example #2(Functions separated on the basis of return type)
void function_name(int y) //WRONG
int function_name(int y) //WRONG
OVERRIDING:
In this case both have the exactly same signature but you need to have class for overriding. A method in the base class is overridden in the child class with some added or extra functionality e.g.
class Base {
void function_name(){
printf("Base");
}
};
class Derived extends Base{
void function_name(){
printf("Derived");
}
};
NOTE: In this case the method name function_name from the base class is derived and then overridden in the child class, this comes handy for inheritance.
Please lemme know in case if you have an doubt regarding the same.
regards
Joydeep Bhattacharya
http://www.scodz.com