Skip to content

Instantly share code, notes, and snippets.

@hasinur1997
Created April 22, 2019 05:58
Show Gist options
  • Select an option

  • Save hasinur1997/eb1b05487b7fc455a134402543aa4402 to your computer and use it in GitHub Desktop.

Select an option

Save hasinur1997/eb1b05487b7fc455a134402543aa4402 to your computer and use it in GitHub Desktop.

What is function overloading ?

Function overloading is a feature in where two or more functions can have the same name but different parameters. Function overloading can be considered as an example of polymorphism feature.

Advantages of function overloading.

  1. the use of function overloading is to save the memory space,consistency and readabiliy.

  2. We can develop more than one function with the same name.

  3. Function overloading exhibits the behavior of polymorphism which helps to get different behaviour, although there will be some link using same name of function.

  4. It speeds up the execution of programme.

  5. Function overloading is done for code re-usability, to save efforts, and also to save memory.

  6. It helps app to load the class method based on the type of parameter

  7. Code maintenance is easy.

Disadvantages of function overloading.

  1. Function declarations that differ only in the return type cannot be overloaded.

  2. Member function declarations with the same name and the same parameter types cannot be overloaded if any of them is a static member function declaration.

Example of function overloading

#include <iostream> 
using namespace std; 
  
void print(int i) { 
  cout << " Here is int " << i << endl; 
} 
void print(double  f) { 
  cout << " Here is float " << f << endl; 
} 
void print(char const *c) { 
  cout << " Here is char* " << c << endl; 
} 
  
int main() { 
  print(10); 
  print(10.10); 
  print("ten"); 
  return 0; 
}

What is function overriding ?

If derived class defines same function as defined in its base class, it is known as function overriding.

Function Overloading VS Function Overriding

Inheritance:

Overriding of functions occurs when one class is inherited from another class. Overloading can occur without inheritance.

Function Signature:

Overloaded functions must differ in function signature ie either number of parameters or type of parameters should differ. In overriding, function signatures must be same.

Scope of functions:

Overridden functions are in different scopes; whereas overloaded functions are in same scope.

Behavior of functions:

Overriding is needed when derived class function has to do some added or different job than the base class function. Overloading is used to have same name functions which behave differently depending upon parameters passed to them.

Advantages of function overriding

  1. Helps in writing generic code based on parent class or interface as object resolution happens at runtime.

  2. Provides multiple implementation of same method and can invoke parent class overridden method using super keyword.

  3. Defines what behavior a class can have and implementation of behavior has been taken care by class which is going to implement.

Example of function overriding

// CPP program to illustrate 
// Function Overriding 
#include<iostream> 
using namespace std; 
  
class BaseClass 
{ 
public: 
    virtual void Display() 
    { 
        cout << "\nThis is Display() method"
                " of BaseClass"; 
    } 
    void Show() 
    { 
        cout << "\nThis is Show() method "
               "of BaseClass"; 
    } 
}; 
  
class DerivedClass : public BaseClass 
{ 
public: 
    // Overriding method - new working of 
    // base class's display method 
    void Display() 
    { 
        cout << "\nThis is Display() method"
               " of DerivedClass"; 
    } 
}; 
  
// Driver code 
int main() 
{ 
    DerivedClass dr; 
    BaseClass &bs = dr; 
    bs.Display(); 
    dr.Show(); 
} 

What is abstract class ?

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.

Advantages of Abstract class

The purpose of an abstract class is to define a common protocol for a set of concrete subclasses. This is useful when defining objects that share code, abstract ideas, etc.

Example of abstract class


#include<iostream> 
using namespace std; 
  
class Base 
{ 
   int x; 
public: 
    virtual void fun() = 0; 
    int getX() { return x; } 
}; 
  
// This class inherits from Base and implements fun() 
class Derived: public Base 
{ 
    int y; 
public: 
    void fun() { cout << "fun() called"; } 
}; 
  
int main(void) 
{ 
    Derived d; 
    d.fun(); 
    return 0; 
} 

What is polymorphism ?

The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

Advantages of polymorphism

  1. It helps programmers reuse the code and classes once written, tested and implemented. They can be reused in many ways.

  2. Single variable name can be used to store variables of multiple data types(Float, double, Long, Int etc).

  3. Polymorphism helps in reducing the coupling between different functionalities.

Disadvanges of polymorphism

  1. One of the disadvantages of polymorphism is that developers find it difficult to implement polymorphism in codes.

  2. Run time polymorphism can lead to the performance issue as machine needs to decide which method or variable to invoke so it basically degrades the performances as decisions are taken at run time.

  3. Polymorphism reduces the readability of the program. One needs to identify the runtime behavior of the program to identify actual execution time.

Example of polymorphism

// C++ program for function overloading 
#include <bits/stdc++.h> 
  
using namespace std; 
class Geeks 
{ 
    public: 
      
    // function with 1 int parameter 
    void func(int x) 
    { 
        cout << "value of x is " << x << endl; 
    } 
      
    // function with same name but 1 double parameter 
    void func(double x) 
    { 
        cout << "value of x is " << x << endl; 
    } 
      
    // function with same name and 2 int parameters 
    void func(int x, int y) 
    { 
        cout << "value of x and y is " << x << ", " << y << endl; 
    } 
}; 
  
int main() { 
      
    Geeks obj1; 
      
    // Which function is called will depend on the parameters passed 
    // The first 'func' is called  
    obj1.func(7); 
      
    // The second 'func' is called 
    obj1.func(9.132); 
      
    // The third 'func' is called 
    obj1.func(85,64); 
    return 0; 
} 

Exception Handling

Exception Handling is a mechanism to handle runtime errors

Use of exception handling

throw −

A program throws an exception when a problem shows up. This is done using a throw keyword.

catch −

A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

try −

A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.

Example of Exception Handling

#include <iostream> 
using namespace std; 
  
int main() 
{ 
   int x = -1; 
  
   // Some code 
   cout << "Before try \n"; 
   try { 
      cout << "Inside try \n"; 
      if (x < 0) 
      { 
         throw x; 
         cout << "After throw (Never executed) \n"; 
      } 
   } 
   catch (int x ) { 
      cout << "Exception Caught \n"; 
   } 
  
   cout << "After catch (Will be executed) \n"; 
   return 0; 
} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment