Question: Base Class Implement a base class Shape . This class a virtual function draw() and by extending Shape , we can draw various figure shapes

  1. 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:

    // 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(); }; #endif 
    The draw function does not need to do anything:
     void Shape::draw() { } 
  2. 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 }; #endif 

    Here are some sample functions:

    // 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  
    Your Square class could be like:
    // 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 
    You have to implement the Rectangle and Square functions, which are not given.

  3. 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:

    // 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; } 

    Write a Makefile to compile your programs and run the executable. Test if it works properly.

  4. Polymorphism

    Remove the virtual keyword in the Shape class. Recompile your programs, run it, and see what happens. Explained what you have observed.

  5. UML Diagram

    Use dia to draw a class diagram to show all above classes and their relationships.

O /lab7main Quit 1 - O /lab7main Quit 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!