0
What is overriding in c++...?
answer
2 Answers
+ 9
In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.
+ 1
consider
class Base {
public:
void foo(){
std::cout << "Hello!";
}
};
and two subclasses
class Derived1 : Base { };
class Derived2 : Base {
public:
void foo(){
std::cout << "haha, I'm overriding Base::foo";
}
};
Now, if you had
Derived1 first;
Derived2 second;
first.foo() will print "Hello", because it inherited the method of the base class. second.foo() will print "haha, I'm overriding Base::foo", because we added a method with the same name, foo, to it, that overrides the method it got from the base class.