int main(int argc, char* argv[]) { const int canvas_width = 128; const int canvas_height = 128; PNG canvas; canvas.resize(canvas_width, canvas_height); const RGBAPixel triangle_color = color::ORANGE; Shape* triangle = new Triangle(triangle_color, Vector2(32, 32), Vector2(64, 64), Vector2(32, 64)); triangle->draw(&canvas); canvas.writeToFile("test_destructor.png"); /* TODO: Why is this leaking memory? Triangle does have a valid destructor!? * Can you stop it from leaking WITHOUT changing triangle's type from a * Shape pointer to a Triangle pointer type? */ delete triangle; triangle = NULL; return 0; }
int main(int argc, char* argv[]) { const int canvas_width = 128; const int canvas_height = 128; PNG canvas; canvas.resize(canvas_width, canvas_height); Vector2 rectangle_center(canvas_width / 2, canvas_height / 2); const int rectangle_width = canvas_width / 4; const int rectangle_height = canvas_height / 4; Rectangle* rectangle = new Rectangle(rectangle_center, color::BLUE, rectangle_width, rectangle_height); rectangle->draw(&canvas); const int rectangle_perimeter = rectangle->perimeter(); cout << "Rectangle's Perimeter = " << rectangle_perimeter << endl; const int rectangle_area = rectangle->area(); cout << "Rectangle's Area = " << rectangle_area << endl; /* But we can treat a Rectangle just like a Shape using a Shape pointer */ Shape* shape = rectangle; const int shape_perimeter = shape->perimeter(); cout << "Shape's Perimeter = " << shape_perimeter << endl; const int shape_area = shape->area(); cout << "Shape' Area = " << shape_area << endl; /* TODO: For some reason the shape's area and perimeter is different from * triangle's area and perimeter even though they are pointing to the same * object! Can you this this so they are the same WITHOUT changing the * shape's type from a Shape pointer to a Triangle pointer? */ if (rectangle_perimeter == shape_perimeter) { cout << "The Perimeters are the same!" << endl; } else { cout << "The Perimeters are NOT the same." << endl; } if (rectangle_area == shape_area) { cout << "The Areas are the same!" << endl; } else { cout << "The Areas are NOT the same." << endl; } canvas.writeToFile("test_virtual.png"); delete rectangle; return 0; }
int main() { const int rectangle_width = 16; const int rectangle_height = 16; Vector2 rectangle_center(32, 16); // I can create a Rectangle using a Rectangle Pointer! Rectangle* rectangle = new Rectangle(rectangle_center, color::RED, rectangle_width, rectangle_height); rectangle_center.set_x(rectangle_center.x() + rectangle_width); // OR I can create a Rectangle using a Shape Pointer!! Shape* rectangle_shape = new Rectangle(rectangle_center, color::ORANGE, rectangle_width, rectangle_height); rectangle_center.set_x(rectangle_center.x() + rectangle_width); // OR I can create a Rectangle using a Drawable Pointer!!! Drawable* rectangle_drawable = new Rectangle( rectangle_center, color::YELLOW, rectangle_width, rectangle_height); // Since all these pointers point to something that is a Drawable type // lets make an array of drawable pointers size_t num_drawables = 3; Drawable** drawables = new Drawable*[num_drawables]; drawables[0] = rectangle; drawables[1] = rectangle_shape; drawables[2] = rectangle_drawable; PNG canvas; canvas.resize(WIDTH, HEIGHT); // Since they are all drawable I can call draw on all of them for (size_t i = 0; i < num_drawables; i++) { drawables[i]->draw(&canvas); } canvas.writeToFile(OUTPUT_FILE); for (size_t i = 0; i < num_drawables; i++) { delete drawables[i]; drawables[i] = NULL; } delete[] drawables; drawables = NULL; return 0; }
PNG testColorPicker(colorPicker & picker) { PNG img; img.resize(FUNCTORTESTWIDTH, FUNCTORTESTHEIGHT); RGBAPixel px; for(int x = 1; x < FUNCTORTESTWIDTH; x = x + x) { for(int y = 1; y < FUNCTORTESTHEIGHT; y = y + y) { px = picker(x, y); cout << "\toperator()(" << x << ", " << y << ") = {" << (int)px.red << ", "; cout << (int)px.green << ", " << (int)px.blue << "}" << endl; } } for(int x = 0; x < FUNCTORTESTWIDTH; x++) for(int y = 0; y < FUNCTORTESTHEIGHT; y++) *img(x, y) = picker(x, y); return img; }