-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultilevelInheritance.cpp
More file actions
45 lines (37 loc) · 1014 Bytes
/
MultilevelInheritance.cpp
File metadata and controls
45 lines (37 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Topic: Multilevel Inheritance
// Standard: C++20
// Build: g++ -std=c++20 -o multilevel MultilevelInheritance.cpp
#include <iostream>
class Animal {
public:
void eat() const {
std::cout << "Animal is eating\n";
}
};
class Mammal : public Animal {
public:
void walk() const {
std::cout << "Mammal is walking\n";
}
};
class Dog : public Mammal {
public:
void bark() const {
std::cout << "Dog is barking\n";
}
};
int main() {
Dog dog;
dog.eat(); // from Animal
dog.walk(); // from Mammal
dog.bark(); // from Dog
// Interview one-liner:
// Multilevel inheritance forms a chain: Dog -> Mammal -> Animal.
// The most-derived class reuses behavior from all parent levels.
//
// Additional interview points:
// 1) Keep inheritance depth shallow (usually <= 2-3 levels).
// 2) Deep hierarchies increase coupling and maintenance cost.
// 3) Prefer composition when variation is mostly behavioral.
return 0;
}