Skip to content

Commit a3c4b4d

Browse files
author
XuhuaHuang
committed
Add house boat multiple inheritance example
1 parent 4267605 commit a3c4b4d

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

MultipleInheritance/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1010
add_executable(date_time "date_time.cpp")
1111
add_executable(coloured_circle "coloured_circle.cpp")
1212
add_executable(modern_coloured_circle "modern_coloured_circle.cpp")
13+
add_executable(house_boat "house_boat.cpp")

MultipleInheritance/house_boat.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
3+
class house_t {
4+
protected:
5+
int residents{0};
6+
7+
public:
8+
house_t() = default;
9+
10+
explicit house_t(int num)
11+
: residents(num) {}
12+
13+
void set_residents(int num) noexcept { residents = num; }
14+
15+
[[nodiscard]]
16+
int get_residents() const noexcept {
17+
return residents;
18+
}
19+
};
20+
21+
class boat_t {
22+
protected:
23+
float max_speed{0.0f};
24+
25+
public:
26+
boat_t() = default;
27+
28+
explicit boat_t(float speed)
29+
: max_speed(speed) {}
30+
31+
void set_max_speed(float speed) noexcept { max_speed = speed; }
32+
33+
[[nodiscard]]
34+
float get_max_speed() const noexcept {
35+
return max_speed;
36+
}
37+
};
38+
39+
class house_boat_t : public house_t, public boat_t {
40+
public:
41+
house_boat_t() = default;
42+
43+
house_boat_t(int residents, float speed)
44+
: house_t(residents)
45+
, boat_t(speed) {}
46+
};
47+
48+
int main() {
49+
house_boat_t hb;
50+
hb.set_residents(4);
51+
hb.set_max_speed(10.0f);
52+
53+
std::cout << "This dwelling has " << hb.get_residents() << " residents and a max speed of " << hb.get_max_speed()
54+
<< std::endl;
55+
56+
return 0;
57+
}

0 commit comments

Comments
 (0)