C++ में Function Overriding
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Overriding में Access Specifiers
Overriding सही से काम करने के लिए, base class का method derived class को visible होना चाहिए, जिसका आमतौर पर मतलब है यह public या protected declared है — एक private base method meaningfully override नहीं हो सकता क्योंकि derived class इसे देख भी नहीं सकती।
उदाहरण: Access Specifiers in Overriding
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Overriding बनाम Overloading
Overloading और overriding अक्सर confuse होते हैं लेकिन अलग mechanisms हैं: overloading same नाम रखता है लेकिन same class या scope के अंदर parameter signature बदलता है, जबकि overriding एक base/derived pair में होता है और एक identical signature चाहता है।
उदाहरण: Overriding vs Overloading
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- parameter list थोड़ा बदलना, जो एक नया function बनाता है जो base को override करने की बजाय hide कर देता है।
- base class में
virtualभूल जाना, ताकि एक base pointer के जरिए calls base version इस्तेमाल करें। overrideइस्तेमाल न करना, ताकि signature में एक typo चुपचाप accept हो जाए।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: