When a class inherits another class, if a function, defined in the base class, is again defined in derived class. If an subclass object is created, the function defined in the subclass over writes the function defined in the base class.
#include <iostream>
using namespace std;
class Base {
public:
void print() {
cout << "I am in base class" << endl;
}
};
class Derived : public Base {
public:
void print() {
cout << "I am in derived class" << endl;
}
};
int main() {
Base b;
b.print();
Derived d;
d.print();
return 0;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.