-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultipleInheritance.cpp
More file actions
45 lines (37 loc) · 1.1 KB
/
MultipleInheritance.cpp
File metadata and controls
45 lines (37 loc) · 1.1 KB
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: Multiple Inheritance
// Standard: C++20
// Build: g++ -std=c++20 -o multiple_inheritance MultipleInheritance.cpp
#include <iostream>
class Printer {
public:
void print() const {
std::cout << "Printing document\n";
}
};
class Scanner {
public:
void scan() const {
std::cout << "Scanning document\n";
}
};
class AllInOneMachine : public Printer, public Scanner {
public:
void fax() const {
std::cout << "Faxing document\n";
}
};
int main() {
AllInOneMachine device;
device.print(); // from Printer
device.scan(); // from Scanner
device.fax(); // from AllInOneMachine
// Interview one-liner:
// Multiple inheritance lets one class reuse behavior from more than one base class.
// Example: AllInOneMachine is both a Printer and a Scanner.
//
// Additional interview points:
// 1) Use it carefully because member-name ambiguity can appear.
// 2) The diamond problem is handled in C++ with virtual inheritance when needed.
// 3) Prefer interface-style bases when combining unrelated capabilities.
return 0;
}