Created
May 22, 2016 13:52
-
-
Save theidexisted/775487481094ab4479dcc6507ade957d to your computer and use it in GitHub Desktop.
watch out for return value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Shape { | |
public: | |
virtual ~Shape() { } // A virtual destructor | |
virtual void draw() = 0; // A pure virtual function | |
virtual void move() = 0; | |
// ... | |
virtual Shape* clone() const = 0; // Uses the copy constructor | |
virtual Shape* create() const = 0; // Uses the default constructor | |
}; | |
class Circle : public Shape { | |
public: | |
Circle() = default; | |
~Circle() = default; | |
void draw() {} | |
void move() {} | |
Circle* clone() const; // Covariant Return Types; see below | |
Circle* create() const; // Covariant Return Types; see below | |
// ... | |
}; | |
Circle* Circle::clone() const { return new Circle(*this); } | |
Circle* Circle::create() const { return new Circle(); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment