Skip to content

Commit 1d2937c

Browse files
committed
solved virtual inheritance exercise
1 parent 81b89c5 commit 1d2937c

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

exercises/virtual_inheritance/TextBox.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ void Drawable::draw() const {
1010
Rectangle::Rectangle(int id, float width, float height) :
1111
Drawable(id), m_width(width), m_height(height) {}
1212

13-
Text::Text(int id, const std::string &content) :
13+
Text::Text(int id, std::string content) :
1414
Drawable(id), m_content(content) {}
1515

16-
TextBox::TextBox(const std::string &content,
16+
TextBox::TextBox(std::string content,
1717
float width, float height) :
18-
Rectangle(1, width, height), Text(2, content) {}
18+
Drawable(3), Rectangle(1, width, height), Text(2, content) {}

exercises/virtual_inheritance/TextBox.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ class Drawable {
1010
int m_id;
1111
};
1212

13-
class Rectangle : public Drawable {
13+
class Rectangle : public virtual Drawable {
1414
public:
1515
Rectangle(int id, float width, float height);
1616
protected:
1717
float m_width;
1818
float m_height;
1919
};
2020

21-
class Text : public Drawable {
21+
class Text : public virtual Drawable {
2222
public:
23-
Text(int id, const std::string &content);
23+
Text(int id, std::string content);
2424
protected:
2525
std::string m_content;
2626
};
2727

2828
class TextBox : public Rectangle, public Text {
2929
public:
30-
TextBox(const std::string &content,
30+
TextBox(std::string content,
3131
float width, float height);
3232
};

exercises/virtual_inheritance/trymultiherit.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
#include <iostream>
33

44
int main() {
5-
// create a TextBox and call draw
6-
7-
8-
// Fix the code to call both draws by using types
9-
10-
11-
// try with virtual inheritance
12-
5+
TextBox box = TextBox {"test", 10, 10};
6+
box.draw(); // errors initially
137

8+
Rectangle &r = box;
9+
r.draw();
10+
Text &t = box;
11+
t.draw();
1412
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy