Sunday, July 9, 2017

What is function overriding in c++ example,Everything about method overriding

This is a common interview question asked and very important fundamental to understand to visualize how Object Oriented programming works. This feature of OOPS have vast implementation and is used in many design patterns.
First we start with the topic it is expected that you have some basic idea of Inheritance.


What does method overriding means: "Implementing a base class function in derived (child) class is methode overriding" .Basically you if there is a fucntion testFun() in a parent class, and same name function testFun() is also declared in derived class, in this case the child class overrides testFunc behavior.
Overriding Essential requirement:
  1. Inheritance is compulsive for method overriding.
  2. Overriding function  should have same declaration in both base and derived class including exact same name, same return type and same parameter list.
class Base
{
 public:
 void testFunc()
 {
  cout << "Base class";
 }
};
class Derived:public Base
{
 public:
 void testFunc()
 {
  cout << "Derived Class";
 }
}
int main()
{
 Base b;       //Base class object
 Derived d;     //Derived class object
 b.testFunc();     //Early Binding Ocuurs
 d.testFunc();   
}    
Output : Base class    Derived class

What happened here ! Why derived class object is not seeing two declaration of testFunc(). One in base class and other in derived class. Why there are no linking error.

This is because of Early Binding, methods, functions and other properties which are detected and checked during compile time and all the function call are decided at compile time by an object. Linker needs not worry about any function call. The advantage of using early binding is for code performance and ease of development.

Where do we need to use this method overriding:
This is quire interesting that you might have understood the what function overriding means but it is more insetting to know why it is even there and so popular.
First have look at late binding also know as dynamic binding  which is only possible with overridden methods
class Base
{
 public:
 void show()
 {
  cout << "Base class";
 }
};
class Derived:public Base
{
 public:
 void show()
 {
  cout << "Derived Class";
 }
}

int main()
{ 
 Derived d;     //Derived class object
 Base* b = &d;  //Base class pointer to derived class object
 b->show();     //Late Binding will Occur
}
In above program which show method will execute ?

Output: Derived Class

Interesting right! that how a base class pointer is invoking child class function. Actually this is the concept that you must understand well and use your logic to fit this in your program to make it more intelligent.

Analyzing function overriding practically:
To understand overriding practical use, think of mouse operation you use daily with your computer. All you do with this is clicks and windows Handlers do the work for you. How the window handler is doing this, its simple , window handler is a base class pointer which can point to any type of event class(will be all child). So depending upon user request necessary function of child handler is invoked with simple base class pointer control.
How it works internally from compiler to linker would be a long discussion to start here, rather you can explore that in this blog(Navigation)

Explore more about Method overriding in this website:

How does method overriding works in c++
How does method overriding helps in design patterns

Friday, July 7, 2017

What is function overloading and how it works

Function overloading is one of the powerful feature of OOPS programming and it is very essential to know how it works and why it is even there at first place.

Function overloading, what does  definition says: "Its a way by which one can define two function with exact same name in a common scope or namespace. However the function will differ in argument type, No. of arguments passed"

But why do we need function overloading: Because you don't want to confuse the programmers/users who are using your program for some particular functionality or feature. Putting too many function name for calling a single feature in different circumstance will make someone go crazy.
Lets take a easy example where a retailer shop keeper want to use software for doing all type of bill purchase. He sells many diffrent things on his shop  but uses a single software application for billing purpose. All he requuires is a single "Calculate Bill" button for all type of purchase done by customer irrespective of type of goods, quantity, discount of the goods. One button should be able to do all the billing function for the users. But as a programmer of the software you need to use function overloading to achieve this 

Does C language supports Overloading :

Well the answer is No, But one may question how does printf() and scanf() or similar function work in C, because they seems to be doing exact same work ie taking different No./Types of arguments  and printing it on the screen!
Actually these C inbuilt function uses a Variable Argument List feature of C language which allows it to so. But is very different from Overloading function and its capability. 

How does overloading works internally:

Little depth of method overloading :
Lets see how compiler deals with these function, as if you know while compiling all function gets their own unique symbol in object code.
C++ Compiler: The compiler will generated different symbol(mangled) code for the these function, if you load the symbol chart of a object file with overloaded function you would see something like below:
For a overloaded function with name bechmark below symbol will be generated (2nd and 3rd line). You can see both have unique identification code.
symbol table

C compiler:
As mentioned earlier overloading does not exist in the C language, so if you try to declare two menthol with exact same name, the symbol code generated will have same identification for them, hence the linker would fail to link the function calls.

Please keep reading this site article for more details on this.