c++ – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 04 Jul 2022 06:06:04 +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 c++ – 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 New and Malloc in C++ https://programmerbay.com/malloc-vs-new-in-c/ https://programmerbay.com/malloc-vs-new-in-c/#respond Fri, 04 Dec 2020 07:53:01 +0000 https://programmerbay.com/?p=7849 MALLOC FUNCTION:

The main job of the malloc function is to gear up and allow the memory at the runtime. The malloc() function can be used in both C as well as C++ programming language.

ptr = (type*) malloc(size)

NEW:

The new operator has also the same job like malloc function, that is, to allot the memory at the runtime. The new operator is most commonly used in C++ programming.

type variable = new dataType()

 

DIFFERENCE BETWEEN NEW AND MALLOC IN C++ :

Basismallocnew
Refers to A Library functionAn operator
Returnsvoid*fully typed pointer
OverridingNot PossiblePossible
Execution speedslowfast
  1. The malloc function is basically a library function which resides in the stdlib.h header file.The new on the other hand is an operator which is most commonly used in C++ programming.
  2. In the case of malloc function, there is no constructor call made to allocate the memory at the runtime. In case of new operator, there is a constructor call made to the memory at the runtime.
  1. In case of malloc function, the allotment of the memory at runtime is done from the heap. In case of new operator, the allotment of the memory at the runtime is done from the free store.

]]>
https://programmerbay.com/malloc-vs-new-in-c/feed/ 0
Difference Between Continue And Break Statement in C++ in Tabular Form https://programmerbay.com/break-and-continue-statement-in-c/ https://programmerbay.com/break-and-continue-statement-in-c/#respond Sun, 22 Mar 2020 09:14:07 +0000 https://www.programmerbay.com/?p=2627 In C or C++, break and continue statements are the control statements that can change the flow of program execution.

The main difference between break and continue is, break statement is used to break the loop execution based on some conditional expression, whereas the continue statement skips the code that comes after it in a loop and begins a new iteration based on a certain condition.

Difference Between Continue And Break Statement in C++ in Tabular Form

BasisBreak StatementContinue Statement
DefinitionIt is a control statement that is responsible for immediate termination of current enclosing loop (innermost loop in case of nested loop statements)It is a control statement that is responsible for termination of current iteration of the loop and start next iteration again, without processing statements that come after it within the enclosing loop body
UseIt is widely used in switch and loopsIt is used in loops only
Functionality It terminates iteration of the loop or switch statement( in case of loops, it forces to exit from the loop and execute the code which immediate loop, in case of Switch it terminates the statement)It skips the statements that come after it and goes for the next iteration of the loop
BehaviourWhen the break statement is encountered within the loop or switch statement, the execution is stopped immediatelyWhen the continue statement is encountered within the loop during program execution, the statements after it is skipped and the control of loop jumps to the next iteration
TerminationIt causes early termination of the current loopIt causes an early execution of the next iteration of the loop
Program flowIt typically stops the continuation of the current loop of the programIt only stops the current loop iteration but does not hampers the continuation of the loop in the program

Break Statement

A break statement is a control statement that terminates the execution of a loop or a block in which it occurs. Therefore, it breaks the flow of a program.

In C++, the break statement is mainly used in these two cases :

a) Within a loop: When a break is triggered in a loop, it stops the loop execution immediately and transfers the control to the very next statement after it.

Figure: Break Statement Working inside a loop
Figure: Break Statement Working inside a loop

In the case of a nested loop, if a break is present in an inner loop, then it would terminate only the execution of that particular loop only.

b) In Switch Statement: In order to prevent consecutive execution of cases, every case in the switch statement comes with a break statement.

Syntax :

break ;

C++ program to show the working of Break Statement : 

#include<iostream> 
using namespace std; 
int main() {
    for(int a=0;a<5;a++) 
    {if(a==3) 
    break; 
    cout<<a<<" ";
    } 
    return 0; 
}

Output :

0 1 2

Continue Statement

It only terminates the current iteration of the loop and starts the next iteration again, without processing statements that come after it within the enclosing loop body. It is used with looping statements only.

continue statement 1
Figure: Continue Statement Working inside a loop

 

In simple words, it skips the current iteration and jumps to the next loop iteration without executing statements in the loop body that comes after it.

Syntax : 

continue ;

C++ program to show the working of Continue Statement : 

#include<iostream> 
using namespace std; 
int main() 
{ 
    for(int a=0;a<5;a++) 
    { 
        if(a==3) 
        continue; 
        cout<<a<<" "; 
    } 
    return 0; 
}

Output : 

0 1 2 4

]]>
https://programmerbay.com/break-and-continue-statement-in-c/feed/ 0