C++ Method Overloading In Hindi

📔 : C++ 🔗

same name के function को different parameters और different return type के साथ define करना method overloading कहलाता है। इसे operator overloading भी कहते हैं। क्योंकि इसमें method के parameters different होते हैं। Method overloading के लिए method की definition different होनी चाहिए।


जब भी किसी overloaded method को call किया जाता है , compiler method में pass किये गए arguments के according सबसे appropriate method को पहचानकर call करता है। Compiler द्वारा सबसे appropriate overloaded method को determine (पहचानने) की इस process को ही Overload Resolution कहते हैं।

C++ Method Overloading Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class A{
  // define a public method to print string.
  public :
  void print(string s){
    cout << "String : " << s << endl;
  }
	
  // define another method with same name but different definition.
  void print(double d){
    cout << "Double Number : " << d;
  }
};


int main() {
  A aObj;

  aObj.print("learnhindituts");
  aObj.print(23.23);
  return 0;
}
Output
String : learnhindituts
Double Number : 23.23

आप same scope में different definition के साथ same method define कर सकते हैं। लेकिन ध्यान रहे वो same scope में होने चाहिए।

Method Overloading in Inheritance

अगर आप method overloading inheritance में achieve करना चाहोगे तो वो नहीं हो पाएगा , वहां पर हर inherit करने की वजह से method का scope change हो जाता है। जिससे हर बार Base / Parent class का ही overloaded method call होगा अगर आप इसकी child / derived class में method define कर भी देते हो।
For Example :

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define class.
class A{
  // define a public method to print string.
  public :
  void print(string s){
    cout << "String : " << s << endl;
  }
};

// define another class B that inherits class A.
class B : public A{
  // define same name method but different definition.
  public :
  void print(double d){
    cout << "Double Number : " << d;
  }
};


int main() {
	B bObj;

	bObj.print("learnhindituts");
	bObj.print(23.23);
	return 0;
}
Output
In function 'int main()':
26:14: error: cannot convert 'const char [15]' to 'double'
26 |   bObj.print("learnhindituts");
	 |              ^~~~~~~~~~~~~~~~
	 |              |
	 |              const char [15]
17:21: note:   initializing argument 1 of 'void B::print(double)'
17 |   void print(double d){
	 |       ~~~~~~~^

I Hope, आप C++ में method overloading के बारे में अच्छे से समझ गए होंगे। :)

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers