Inheritance - heshawacooray/OOP-Heshawa GitHub Wiki
Definition: Inheritance is an OOP concept where one class (derived class) inherits properties and behaviors (methods) from another class (base class). This allows for code reuse and a hierarchical class structure.
Example code:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating food" << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking" << endl;
}
};
int main() {
Dog d;
d.eat(); // Inherited from Animal class
d.bark(); // Defined in Dog class
return 0;
}