← Back to C++ Course | Chapter 10: Polymorphism & Abstraction | Lesson 2 of 11

C++ में Function Overriding

Function overriding तब है जब एक child class पहले से parent की करी चीज़ का अपना version देती है, अपने तरीके से एक chore करते एक child की तरह। Child का version child के लिए इस्तेमाल होता है।
Syntax
cpp
class Base {
public:
  virtual void method_name() { }
};

class Derived : public Base {
public:
  void method_name() {
    Base::method_name();  // optionally call the base version
  }
};

Function Overriding क्या है?

Function overriding तब होता है जब एक derived class exactly same नाम, return type, और parameter list वाला एक method define करती है जो पहले से इसकी base class में defined है — derived version derived object पर call होने पर inherited वाले को replace करता है।

उदाहरण: What is Function Overriding?

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual void speak() { std::cout << "Some sound" << std::endl; }
};

// Define the type Cat
class Cat : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() override { std::cout << "Meow" << std::endl; }
};

// Program execution starts in main()
int main() {
	Cat c;
	c.speak();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Base Class का Overridden Method Call करना

एक base method override करने के बाद भी, आप अभी भी derived class के अंदर से base class नाम और scope resolution operator से call को qualify करके explicitly parent के original version को invoke कर सकते हैं, आपको inherited behavior को पूरी तरह replace करने की बजाय extend करने देते हुए।

उदाहरण: Calling Base Class Overridden Method

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual void speak() { std::cout << "Some sound" << std::endl; }
};

// Define the type Cat
class Cat : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() override {
		Animal::speak();
		// Print to the console with cout
		std::cout << "Meow" << std::endl;
	}
};

// Program execution starts in main()
int main() {
	Cat c;
	c.speak();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Overriding में Access Specifiers

Overriding सही से काम करने के लिए, base class का method derived class को visible होना चाहिए, जिसका आमतौर पर मतलब है यह public या protected declared है — एक private base method meaningfully override नहीं हो सकता क्योंकि derived class इसे देख भी नहीं सकती।

उदाहरण: Access Specifiers in Overriding

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual void speak() { std::cout << "Some sound" << std::endl; }
};

// Define the type Cat
class Cat : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() override { std::cout << "Meow" << std::endl; }
};

// Program execution starts in main()
int main() {
	Cat c;
	c.speak();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Overriding बनाम Overloading

Overloading और overriding अक्सर confuse होते हैं लेकिन अलग mechanisms हैं: overloading same नाम रखता है लेकिन same class या scope के अंदर parameter signature बदलता है, जबकि overriding एक base/derived pair में होता है और एक identical signature चाहता है।

उदाहरण: Overriding vs Overloading

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual void speak() { std::cout << "Animal speaks" << std::endl; }
	void speak(int times) { for (int i = 0; i < times; i++) std::cout << "!"; std::cout << std::endl; }
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() override { std::cout << "Woof" << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog d;
	d.speak();
	d.speak(3);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointers के साथ Overriding

अगर आप एक base class pointer को एक derived object की ओर point कराएं और एक method call करें जो overridden था लेकिन base class में कभी virtual declared नहीं था, base class का version default से चलता है — यह 'static binding' है।

Base method को virtual mark करना dynamic binding पर switch करता है, ताकि इसकी बजाय derived version चले।

उदाहरण: Overriding with Pointers

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	void speak() { std::cout << "Animal" << std::endl; }
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() { std::cout << "Dog" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Animal *a = new Dog();
	a->speak();
	// Free the heap memory with delete
	delete a;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. parameter list थोड़ा बदलना, जो एक नया function बनाता है जो base को override करने की बजाय hide कर देता है।
  2. base class में virtual भूल जाना, ताकि एक base pointer के जरिए calls base version इस्तेमाल करें।
  3. override इस्तेमाल न करना, ताकि signature में एक typo चुपचाप accept हो जाए।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.