Question: Base Class Implement a base class Shape . This class a virtual function draw() and by extending Shape , we can draw various figure shapes
- Base Class
Implement a base class Shape. This class a virtual function draw() and by extending Shape, we can draw various figure shapes such as triangles, rectangles and squares.
Your Shape class may have the member functions and data fields similar to the following:
The draw function does not need to do anything:// shape.h #ifndef SHAPE_H #define SHAPE_H #include "ccc_win.h" using namespace std; class Shape { protected: Point position; // lower left corner of a shape float width; float height; public: Shape (); // set position to (0, 0) Shape ( Point p ); // set position to p void setPosition ( Point p ); void setSize ( float w, float h ); // set width and height to w and h virtual void draw(); }; #endifvoid Shape::draw() { } - Derived Classes
Implement derived classes Rectangle, Triangle and Square; derive Square from Rectangle. For example, your Triangle class may be similar to:
// triangle.h #ifndef TRIANGLE_H #define TRIANGLE_H #include "shape.h" using namespace std; class Triangle : public Shape { protected: float base; float height; public: Triangle (); // set position to (0, 0), width = 2, height = 2 Triangle ( Point p, float b, float h ); // set position to p, ... void draw(); // draw the triangle }; #endifHere are some sample functions:
Your Square class could be like:// triangle.cpp #include "triangle.h" using namespace std; Triangle::Triangle () : Shape() { width = 2.0; height = 1.0; } Triangle::Triangle( Point p, float b, float h ) : Shape ( p ) { width = b; height = h; } void Triangle::draw() { Point p0 = position; Point p1 ( p0.get_x()+width, p0.get_y() ); Point p2 ( p0.get_x() + width / 2.0, p0.get_y()+height ); cwin
You have to implement the Rectangle and Square functions, which are not given.// square.h #ifndef SQUARE_H #define SQUARE_H #include "rectangle.h" using namespace std; class Square : public Rectangle { public: Square (); // set position to (0, 0), width = 2, height = 2 Square ( Point p, float w ); // set position to p, ... }; #endif - A Drawing Program
Use the above classes to develop a drawing program. Users can place various shapes onto the screen by first clicking on a shape icon and then clicking on the desired screen locations:
You may declare a Shape pointer, which points to the object ( Triangle, Square, Rectangle) you have chosen (clicked); it will be bound to the correct draw function to draw the shape. Your code may be similar to the following:
Write a Makefile to compile your programs and run the executable. Test if it works properly.// lab7main.cpp ...... const float x0 = -9, y0 = -6, w = 3, h=3; int checkRegion( Point p ) { float x, y; x = p.get_x(); y = p.get_y(); if ( x0 = 0 ) { p1 = cwin.get_mouse(""); if ( p1.get_x() setPosition ( p1 ); ........ } } return 0; } - Polymorphism
Remove the virtual keyword in the Shape class. Recompile your programs, run it, and see what happens. Explained what you have observed.
- UML Diagram
Use dia to draw a class diagram to show all above classes and their relationships.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
