Skip to content

Commit 1a11820

Browse files
author
sajith
committed
Improved
1 parent ff7c68f commit 1a11820

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ set(CMAKE_CXX_STANDARD 20)
2323
#add_executable(isp_door ISP/isp_door_example.cpp)
2424
#add_executable(isp_door_better ISP/isp_door_example_better.cpp)
2525
#add_executable(isp_door_better2 ISP/isp_door_example_better2.cpp)
26-
add_executable(isp_device ISP/isp_device_example.cpp)
26+
#add_executable(isp_device ISP/isp_device_example.cpp)
27+
add_executable(isp_device_better ISP/isp_device_example_better.cpp)

ISP/isp_device_example_better.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//
2+
// Created by sajith on 4/19/21.
3+
//
4+
5+
#include <iostream>
6+
#include <stdexcept>
7+
8+
// Interfaces
9+
class Printer
10+
{
11+
12+
public:
13+
virtual ~Printer() = default;
14+
virtual void print() = 0;
15+
};
16+
17+
class Scanner
18+
{
19+
public:
20+
virtual ~Scanner() = default;
21+
virtual void scan() = 0;
22+
};
23+
24+
class Fax
25+
{
26+
public:
27+
virtual ~Fax() = default;
28+
virtual void fax() = 0;
29+
};
30+
31+
32+
class AdvancedPrinter : public Printer, public Scanner, public Fax
33+
{
34+
public:
35+
void print()
36+
{
37+
std::cout << "Printing...\n";
38+
}
39+
40+
void scan()
41+
{
42+
std::cout << "Scanning...\n";
43+
}
44+
45+
void fax()
46+
{
47+
std::cout << "Faxing...\n";
48+
}
49+
};
50+
51+
class SimplePrinter : public Printer
52+
{
53+
public:
54+
void print()
55+
{
56+
std::cout << "Printing...\n";
57+
}
58+
};
59+
60+
// clients
61+
void printUsers(Printer &printer)
62+
{
63+
printer.print();
64+
}
65+
66+
void scanUsers(Scanner &scanner)
67+
{
68+
scanner.scan();
69+
}
70+
71+
void faxUsers(Fax &fax)
72+
{
73+
fax.fax();
74+
}
75+
76+
int main()
77+
{
78+
AdvancedPrinter advancedPrinter;
79+
SimplePrinter simplePrinter;
80+
81+
printUsers(advancedPrinter);
82+
scanUsers(advancedPrinter);
83+
faxUsers(advancedPrinter);
84+
// scanUsers(simplePrinter); // won't compile
85+
86+
return 0;
87+
}

0 commit comments

Comments
 (0)