| Цитата(zubikova @ 13.3.2010, 21:58) | | ммм...а не подскажете как тут организовать ввод-вывод данных? |
Ну вот тебе вывод. Ввод делается аналогично.
| Код | #include <iostream> #include <vector> #include <memory>
struct Point { float X, Y; Point(float x, float y) : X(x), Y(y) { } };
class Shape { private: Point _coords; virtual void PrintInfo() = 0; public: Shape(Point coords) : _coords(coords) { } Point GetCoords() const { return _coords; } friend std::ostream& operator<<(std::ostream& s, Shape* shape); };
std::ostream& operator<<(std::ostream& s, Shape* shape) { shape->PrintInfo(); return s; }
class Square : public Shape { private: float _angle; float _size; void PrintInfo(); public: Square(Point coords, float angle, float size) : Shape(coords), _angle(angle), _size(size) { } void Rotate(float angle) { _angle += angle; } };
void Square::PrintInfo() { std::cout << "Square (angle = " << _angle << ", size = " << _size << ")" << std::endl; }
class Circle : public Shape { private: float _radius; void PrintInfo(); public: Circle(Point coords, float radius) : Shape(coords), _radius(radius) { } void Scale(float d) { _radius *= d; } };
void Circle::PrintInfo() { std::cout << "Circle (radius = " << _radius << ")" << std::endl; }
typedef std::tr1::shared_ptr<Shape> ShapePtr; typedef std::vector<ShapePtr> ShapeVec; typedef std::vector<ShapePtr>::iterator ShapeVecIt;
int main() { ShapeVec vec;
vec.push_back(ShapePtr(new Square(Point(5, 5), 45, 10))); vec.push_back(ShapePtr(new Circle(Point(30, 40), 15)));
for (ShapeVecIt it = vec.begin(); it != vec.end(); it++) std::cout << *it; return 0; } |
|