// test_shape3.cpp all in one file, should really be split up #include "vt100_plot.h" // shape3.h struct Point{float X; float Y;}; class Shape // modified for shape made up of lists of shapes { public: Shape(); // constructor ~Shape(); // destructor, mainly deletes linked list void SetCenter(Point ACenter); Point Center(); void Move(float dx, float dy); // may also want MoveTo(Point XY) void AddComponent(Shape *Ashape); void AddSibling(Shape *Ashape); virtual void Draw(); protected: Point TheCenter; private: Shape *list_of_component_shapes; Shape *list_of_sibling_shapes; }; // shape3.cpp //#include "shape3.h" Shape::Shape() // body for constructor, default initialization { TheCenter.X = 0; // make it legal the first time, keep it legal TheCenter.Y = 0; list_of_component_shapes = 0; list_of_sibling_shapes = 0; } Shape::~Shape() // body for destructor { // could use 'delete' in here on list_of_component_shapes // and list_of_sibling_shapes } void Shape::SetCenter(Point ACenter) // body for function SetCemter { TheCenter = ACenter; } Point Shape::Center() { return TheCenter; } void Shape::Move(float dx, float dy) { TheCenter.X += dx; TheCenter.Y += dy; } void Shape::AddComponent(Shape *Ashape) { Ashape->list_of_component_shapes=list_of_component_shapes; list_of_component_shapes=Ashape; } void Shape::AddSibling(Shape *Ashape) { Ashape->list_of_sibling_shapes=list_of_component_shapes; list_of_sibling_shapes=Ashape; } void Shape::Draw() // draws linked list of sub-shapes { Shape *local_list = list_of_component_shapes; while(local_list){ local_list->Draw(); local_list=local_list->list_of_component_shapes; } } // circle3.h //#include "shape3.h" class Circle : public Shape { public: Circle(float X, float Y, float R); void SetRadius(float Aradius); void Draw(); float Radius(); private: float TheRadius; }; // circle3.cpp //#include "circle3.h" Circle::Circle(float X, float Y, float R) { Point p={X,Y}; SetCenter(p); TheRadius = R; } void Circle::SetRadius(float Aradius) { TheRadius = Aradius; } void Circle::Draw() { DRAW_CIRCLE(TheCenter.X, TheCenter.Y, TheRadius); } float Circle::Radius() { return TheRadius; } // rectangle3.h //#include "shape3.h" class Rectangle : public Shape { public: Rectangle(float X, float Y, float W, float H); void SetSize(float Awidth, float Aheight); void Draw(); float Width(); float Height(); private: float TheWidth; float TheHeight; }; // rectangle3.cpp //#include "rectangle3.h" Rectangle::Rectangle(float X, float Y, float W, float H) { TheCenter.X = X; TheCenter.Y = Y; TheWidth = W; TheHeight = H; } void Rectangle::SetSize(float Awidth, float Aheight) { TheWidth = Awidth; TheHeight = Aheight; } void Rectangle::Draw() { DRAW_RECTANGLE(TheCenter.X, TheCenter.Y, TheWidth, TheHeight); } float Rectangle::Height() { return TheHeight; } float Rectangle::Width() { return TheWidth; } // test_shape3.cpp //#include "circle3.h" //#include "rectangle3.h" int main() // just a few tests { Circle a_circle(1.0F, 2.0F, 3.0F); Circle *circle_ptr = new Circle(7.0F, 0.0F, 3.0F); Point a_point = {3,4}; Rectangle a_rect(-8.0, 2.0, 4.0, 6.0); Shape a_shape; INITIALIZE(); a_circle.SetCenter(a_point); a_circle.SetRadius(2); a_circle.Draw(); a_circle.Move(12.5, 5.0); a_circle.Draw(); circle_ptr->Draw(); a_rect.Draw(); a_shape.SetCenter(a_point); a_shape.AddComponent(new Circle(-12,5,4)); a_shape.AddComponent(new Rectangle(-18,-6,4,4)); a_shape.Draw(); PRINT(); return 0; }