Skip to content

Commit fb2af6a

Browse files
author
sajith
committed
Improved
1 parent 76f08c6 commit fb2af6a

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ add_subdirectory(plant-care-better)
2626
#add_executable(isp_door_better2 ISP/isp_door_example_better2.cpp)
2727
#add_executable(isp_device ISP/isp_device_example.cpp)
2828
#add_executable(isp_device_better ISP/isp_device_example_better.cpp)
29-
add_executable(isp_multiple_inheritance ISP/isp_multiple_inheritance.cpp)
29+
#add_executable(isp_multiple_inheritance ISP/isp_multiple_inheritance.cpp)
30+
add_executable(dip_lamp DIP/dip_lamp_example.cpp)

DIP/dip_lamp_example.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//
2+
// Created by sajith on 4/23/21.
3+
//
4+
5+
#include <iostream>
6+
7+
class Lamp
8+
{
9+
public:
10+
void switchOn()
11+
{
12+
std::cout << "Lamp is on\n";
13+
}
14+
15+
void switchOff()
16+
{
17+
std::cout << "Lamp is off\n";
18+
}
19+
};
20+
21+
class Button
22+
{
23+
Lamp &lamp;
24+
bool on = false;
25+
26+
public:
27+
Button(Lamp &lamp) : lamp{lamp} {}
28+
29+
void toggle()
30+
{
31+
if (on)
32+
{
33+
lamp.switchOff();
34+
} else
35+
{
36+
lamp.switchOn();
37+
}
38+
39+
on = !on;
40+
}
41+
};
42+
43+
int main()
44+
{
45+
Lamp lamp;
46+
Button button(lamp);
47+
button.toggle();
48+
button.toggle();
49+
50+
return 0;
51+
}

0 commit comments

Comments
 (0)