functions – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 25 Apr 2021 06:28:03 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg functions – Programmerbay https://programmerbay.com 32 32 Difference between Function Overloading and Function Overriding in C++ https://programmerbay.com/difference-between-function-overloading-and-function-overriding-in-c/ https://programmerbay.com/difference-between-function-overloading-and-function-overriding-in-c/#respond Wed, 16 Dec 2020 07:32:53 +0000 https://www.programmerbay.com/?p=2579 In C++, function overloading and function overriding provides a way to achieve Polymorphism concept ( ability to take multiple forms) which is one of the OOP’s feature.

Function overloading vs Function overriding

The main difference is, Function Overloading enables us to define multiple functions with a similar name within the same class, whereas Function overriding allows us to redefine a method having same name and signature in inheriting class.

Difference between Function Overloading and Function Overriding in Tabular form :

BasisFUNCTION OVERLOADINGFUNCTION OVERRIDING
DefinitionIt allows us to declare two or more function having same name with different number of parameters or different datatypes of argumentIt allows us to declare a function in parent class and child class with same name and signature
Inheritance ConceptIt can occur without inheritance conceptOverriding can only be done when one class is inherited by other class
SignatureThe overloaded functions must have different signature, for example, either the number of parameters or sort of parameters should be differentFunction signatures must have same
Polymorphism TypeIt is an example of compile-time PolymorphismIt is an example of run-time Polymorphism
ScopeThe overloaded functions are always in the same scopeFunctions are always in a different scope
PurposeOverloading is utilized to have the same names of various functions which act distinctively relying on parameters handed over to themOverriding is required when a determined class function needs to perform some additional or unexpected job in comparison to the base class function
AccomplishOverloading is accomplished at compile timeOverriding is accomplished at runtime
ConstructorA constructor can be overloadedA constructor cannot be overridden
UsageA function can be overloaded any number of timesA function can be overridden only single time in the respective inheriting class

Function Overloading

Function overloading is a form of static polymorphism where at least two functions can have a similar name with different arguments and can be considered as an instance of polymorphism.

In other words, functions with a similar name but having a different number of parameters or types of arguments are said to function overloading.

Function overolading

A compiler calls correct function out of others on the basis of their number of parameters or datatypes in arguments.

However, changing only the return type of the function and keeping parameters and other aspects the same would make compiler confuse in choosing the correct function.

Function Overriding

Function overriding is a form of dynamic polymorphism that enables us to have an equivalent function in child class which is already defined in the parent class.

A child class acquires member variables and member functions of its parent class. When there is a need to override the functionality of the parent’s member function then we use the concept of function overriding.

It resembles making another adaptation of an old method, in the child class.

Function overriding 1

If we talk about C++, a virtual keyword or non-virtual way (without writing the virtual keyword in front of function) in order to achieve function overriding.

C++ program to show Function Overloading :

#include <iostream>
using namespace std;
class Shape{
public:
// area() function to calculate area of square
void area(int side) 
{
  cout<<"Area of square :"<< side*side;
}
// Overloading area() function to calculate area of rectangle
void area(int len, int width)
{
  cout<<"Area of Rectangle :"<< len * width<<"\n";

}

};
int main() {
   Shape obj;
   	cout<<"Example of function overloading \n";
    obj.area(10);  // Calling it to calculate square area
    	cout<<"\n";
    obj.area(5,10);  // Calling it to calculate rectangle area
    return 0;
}

Output:

Area of square :100                                                                                                 
Area of Rectangle :50

Explanation: 

Here in the above example, area function was overloaded, one for calculating square’s area and the second one for calculating the rectangle’s area.

C++ program to show Function Overriding:

#include <iostream>
using namespace std;
// Parent class
class Shape{
public:
void display(){
cout<<" I am base class";	
}

};
// Child Class
class Rectangle: public Shape
{
  public:
void display(){
  cout<<"I am overriding display function of parent class \n";
}
};
int main() {
   Rectangle obj;
   	cout<<"Example of function overriding \n";
    obj.display();
    return 0;
}

Output:

Example of function overriding 
I am overriding display function of parent class

Explanation: 

Here, we overrode the display method, enabling us to define or redefine the body of that function in the child class.

[Related Article: Difference between method overloading and method overriding in Java]

]]>
https://programmerbay.com/difference-between-function-overloading-and-function-overriding-in-c/feed/ 0
Difference between Inline function and Normal function https://programmerbay.com/difference-between-inline-function-and-normal-function/ https://programmerbay.com/difference-between-inline-function-and-normal-function/#respond Sat, 03 Aug 2019 16:36:30 +0000 https://www.programmerbay.com/?p=4464 A function is a piece of code that is used to perform a specific task. It reduces the complexity and improves the readability of the code.

In  C++, a concept named inline function comes into play where a function call is replaced with its function definition by the compiler

Difference between Inline Function and Normal Function

1. Syntax

The syntax of an inline function and a normal function is the same except that inline function has the keyword “inline” in its definition.

Inline Function:

#include <iostream>
using namespace std;
inline int square(int s)
{
     return s*s;
}
void main()
{
     cout << square(5) << "\n";
}

Normal Function:

#include <iostream>
using namespace std;
int square(int s)
{
     return s*s;
}
void main()
{
     cout << square(5) << "\n";
}

 

2. Execution

At the time of program execution, the code is compiled first and then after converted to executable code.

If in that executable code any function call is found, the compiler stores the address of the instruction immediately and jumps to its associated function definition.  Then after, the compiler executes the function code and gets back to the stored instruction address after the function call.

In inline function, the compiler replaces the function call with the function definition code and compiles the entire code. This process is called expansion.

3. Overhead

The normal function creates overheads as and when a function call encounters.

An inline function does not create function call overhead as the function definition code is replaced in the program.

4. Memory Usage

Inline function in the repeated calls such as in a loop wastes memory but save CPU time to execute that call.

Normal function in such calls does not waste memory but uses more CPU time to execute that call.

5. Function call Time

In normal functions which have small size of code, the time needed to make the function call’s jumps is more than the time needed to execute the function’s code.

In the inline function, no such case occurs.

6. Too many Inline functions

Using too many inline functions increases the executable file size because of the duplication of the same code.

Using too many normal functions does not affect the file size after compilation.

7. Inline function in a class

All functions inside a class in C++ are implicitly inline.

All functions outside a class in C++ are normal functions.

More about virtual functions

Virtual functions are not inline because the call to virtual function is resolved at the run time but inline functions call is resolved at compile time.

]]>
https://programmerbay.com/difference-between-inline-function-and-normal-function/feed/ 0