Skip to content

Instantly share code, notes, and snippets.

@brokenpylons
Created April 2, 2020 16:58
Show Gist options
  • Select an option

  • Save brokenpylons/4bf017e76b3f1b607fef64530f2376ba to your computer and use it in GitHub Desktop.

Select an option

Save brokenpylons/4bf017e76b3f1b607fef64530f2376ba to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
// Printable = Number | String
class Printable {
public:
virtual std::string toString() const = 0;
virtual ~Printable() = default;
};
class Number : public Printable {
public:
Number(int value) : value(value) {}
std::string toString() const override {
return std::to_string(value);
}
private:
int value;
};
class String : public Printable {
public:
String(const std::string &value) : value(value) {}
std::string toString() const override {
return value;
}
private:
std::string value;
};
class Oseba : public Printable {
public:
Oseba(const std::string &ime, const std::string &priimek) : ime(ime), priimek(priimek) {}
private:
std::string toString() const override {
return "Jaz sem: " + ime + " " + priimek;
}
private:
std::string ime;
std::string priimek;
};
void print(const Printable &p) {
std::cout << p.toString() << std::endl;
}
class View {
public:
View(bool visible) : visible(visible) {}
std::string draw() const {
if (visible) {
return doDraw();
}
return "";
}
virtual std::string toString() const = 0;
protected:
std::string printVisiable() const {
if (visible) {
return "visible";
} else {
return "non visible";
}
}
private:
virtual std::string doDraw() const = 0;
bool visible;
};
class TextView : public View {
public:
TextView(bool visible, const std::string &text) : View(visible), text(text) {}
std::string doDraw() const override {
return "text";
}
std::string toString() const override {
return "TextView: " + text + "(" + printVisiable() + ")";
}
private:
std::string text;
};
class TestView : public View {
public:
TestView(bool visible) : View(visible) {}
std::string doDraw() const {
return "text";
}
};
int main() {
TextView t(false, "test");
std::cout << t.draw() << std::endl;
std::cout << t.toString() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment