Skip to content

Instantly share code, notes, and snippets.

@kumailxp
Created March 11, 2020 23:05
Show Gist options
  • Save kumailxp/8582ef747b1de9df76a89d90aa7b7791 to your computer and use it in GitHub Desktop.
Save kumailxp/8582ef747b1de9df76a89d90aa7b7791 to your computer and use it in GitHub Desktop.
#include <string>
#include <sstream>
#include <iostream>
struct Shape {
virtual std::string str() const = 0;
};
struct Circle : Shape {
float radius;
Circle(const float radius) : radius{radius} {}
Circle() : radius{0} {}
void resize(float factor) { radius *= factor; }
std::string str() const override {
std::ostringstream oss;
oss << "A circle of radius " << radius;
return oss.str();
}
};
template<typename T>
struct ColoredShape : T {
std::string color;
template<typename...Args>
ColoredShape(std::string color, Args ...args)
: T(std::forward<Args>(args)...), color{color} {}
std::string str() const override {
std::ostringstream oss;
oss << T::str() << " has the color " << color;
return oss.str();
}
};
int main() {
Circle C{0.5};
ColoredShape<Circle> circle{"red" , 10};
// circle.radius = 0.5;
std::cout << "r= " << circle.str() << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment