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.
-
the use of function overloading is to save the memory space,consistency and readabiliy.
-
We can develop more than one function with the same name.
-
Function overloading exhibits the behavior of polymorphism which helps to get different behaviour, although there will be some link using same name of function.
-
It speeds up the execution of programme.
-
Function overloading is done for code re-usability, to save efforts, and also to save memory.
-
It helps app to load the class method based on the type of parameter
-
Code maintenance is easy.
-
Function declarations that differ only in the return type cannot be overloaded.
-
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.
#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;
}
If derived class defines same function as defined in its base class, it is known as function overriding.
Overriding of functions occurs when one class is inherited from another class. Overloading can occur without inheritance.
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.
Overridden functions are in different scopes; whereas overloaded functions are in same scope.
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.
-
Helps in writing generic code based on parent class or interface as object resolution happens at runtime.
-
Provides multiple implementation of same method and can invoke parent class overridden method using super keyword.
-
Defines what behavior a class can have and implementation of behavior has been taken care by class which is going to implement.
// 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();
}
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.
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.
#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;
}
The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.
-
It helps programmers reuse the code and classes once written, tested and implemented. They can be reused in many ways.
-
Single variable name can be used to store variables of multiple data types(Float, double, Long, Int etc).
-
Polymorphism helps in reducing the coupling between different functionalities.
-
One of the disadvantages of polymorphism is that developers find it difficult to implement polymorphism in codes.
-
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.
-
Polymorphism reduces the readability of the program. One needs to identify the runtime behavior of the program to identify actual execution time.
// 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 is a mechanism to handle runtime errors
A program throws an exception when a problem shows up. This is done using a throw keyword.
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.
A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks.
#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;
}