Inheritance in Cpp
Inheritance in Cpp
What is Inheritance?
--------------------
Inheritance means one class (child) can use the properties and functions of another class (parent).
It helps in code reuse and shows relationship between classes like "is-a".
Real-Life Example:
------------------
---------------------
class Animal {
public:
void eat() {
};
public:
void bark() {
};
----------------------------
2. Multilevel - Derived becomes base again (Puppy from Dog from Animal)
4. Hierarchical - Multiple classes from one base (Dog, Cat from Animal)
Examples:
---------
1. Single Inheritance:
class Animal {
public:
void eat() {
};
public:
void bark() {
};
2. Multilevel Inheritance:
class Animal {
public:
void eat() {
};
public:
void bark() {
};
public:
void weep() {
};
3. Multiple Inheritance:
class Father {
public:
void height() {
cout << "Tall height" << endl;
};
class Mother {
public:
void eyes() {
};
public:
void skinTone() {
};
Benefits of Inheritance:
------------------------
- Code Reuse
- Reduces Redundancy