Function Overriding in C++ | difference between function overloading and Overriding | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Friday, July 31, 2020

Function Overriding in C++ | difference between function overloading and Overriding | My CS Tutorial

Function Overriding in C++ | difference between function overloading and  Overriding | Example of function overriding | Requirements of function overriding | My CS Tutorial 

Function overriding in C++ | My CS Tutorial

Method Overriding in C++


If we inherit a class into the derived class and provide a definition for one of the base class's function again inside the derived class, then that function is said to be overridden, and this mechanism is called Function Overriding.

Requirements for Overriding a Function


Inheritance should be there. Function overriding cannot be done within a class. For this we require a derived class and a base class.
Function that is redefined must have exactly the same declaration in both base and derived class, that means same name, same return type and same parameter list.

To override a function you must have the same signature in child class. By signature I mean the data type and sequence of parameters. Here we don’t have any parameter in the parent function so we didn’t use any parameter in the child function.

Example of function overriding


#include <iostream>
using namespace std;
class BaseClass {
public:
   void disp(){
      cout<<"Function of Parent Class";
   }
};
class DerivedClass: public BaseClass{
public:
   void disp() {
      cout<<"Function of Child Class";
   }
};
int main() {
   DerivedClass obj = DerivedClass();
   obj.disp();
   return 0;
}

Output:


Function of Child Class

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.

Function Overriding in C++ | difference between function overloading and  Overriding | Example of function overriding | Requirements of function overriding | My CS Tutorial 

_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment