Data Structures with C Using STL - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Data Structures with C Using STL

Description:

white blue teal green turquoise dark grey brown purple light blue light grey ... { white, blue, teal, green, turquoise, darkgray, brown, purple, lightblue, ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 46
Provided by: ruthu
Category:

less

Transcript and Presenter's Notes

Title: Data Structures with C Using STL


1
Data Structures with CUsing STL
  • Chapter Thirteen
  • Inheritance and Abstract
  • Classes Example

2
EZDraw Functions
  • // initialize drawing surface
  • void openWindow()
  • // wait until a key is pressed
  • void viewWindow()
  • // erase the drawing window
  • void eraseWindow()
  • // close the drawing surface
  • void closeWindow()

3
EZDraw Functions
  • // wait secs seconds before executing next
  • // instruction
  • void delayWindow(double secs)
  • // return true if a key pressed else, return
    false
  • bool keyPress()

4
Building a graphics system
  • Graphics window

x
(0,0)
10 units
length
8 u n i t s
radius
y
width
Base point
Base point (x,y)
5
Shapes in graphics package
  • Circle
  • Rectangle
  • Square
  • Polygon

6
Common characteristics of shapes
  • Base point - although the base point may be in
    different locations depending on the shape all
    shapes have one.
  • Fill color - valid values are
  • white blue teal green turquoise dark
    grey brown purple light blue light grey
    gold red orange pink yellow black

7
shapeColor Class
  • include "d_draw.h"
  • // colors of the shapes
  • enum drawing_color
  • white, blue, teal, green, turquoise, darkgray,
    brown, purple, lightblue, lightgray, gold, red,
    orange, pink, yellow, black

8
shapeColor Class - cont.
  • // wrapper class for the colors
  • class shapeColor
  • private
  • drawing_color color
  • // table defining conversion from simple
    colors
  • // to necessary Windows definitions
  • static const ezdColor g_aColors

9
shapeColor Class - cont.
  • public
  • // constructor
  • shapeColor(drawing_color c blue)
  • // increment and decrement operators
  • shapeColor operator (int)
  • shapeColor operator ()
  • shapeColor operator-- (int)
  • shapeColor operator-- ()

10
shapeColor Class - cont.
  • // relational operators
  • friend bool operator (const shapeColor lhs,
  • const
    shapeColor rhs)
  • friend bool operator! (const shapeColor lhs,
  • const
    shapeColor rhs) friend bool operatorlt (const
    shapeColor lhs,
  • const shapeColor
    rhs)
  • friend bool operatorlt (const shapeColor lhs,
  • const
    shapeColor rhs)

11
shapeColor Class - cont.
  • friend bool operatorgt (const shapeColor lhs,
  • const
    shapeColor rhs)
  • friend bool operatorgt (const shapeColor lhs,
  • const
    shapeColor rhs)
  • // convert to Windows definition for colors
  • ezdColor convertToEzdColor()

12
Implementation of shapeColor Class
  • // color conversion lookup table
  • const ezdColor shapeColorg_aColors
  • ezdWhite, ezdBlue, ezdTeal, ezdGreen,
    ezdTurquoise,
  • ezdDarkGray, ezdBrown, ezdPurple,
    ezdLightBlue,
  • ezdLightGray, ezdGold, ezdRed, ezdOrange,
    ezdPink,
  • ezdYellow, ezdBlack

13
Implementation of shapeColor Class
  • // constructor. initialize color
  • shapeColorshapeColor(drawing_color c) color(c)
  • // postfix increment
  • shapeColor shapeColoroperator (int)
  • drawing_color origColor color
  • color drawing_color((color1) 16)
  • return shapeColor(origColor)

14
Implementation of shapeColor Class
  • // prefix increment
  • shapeColor shapeColoroperator ()
  • color drawing_color((color1) 16)
  • return this
  • // postfix decrement
  • shapeColor shapeColoroperator-- (int)
  • drawing_color origColor color
  • if (color black) color white
  • else color drawing_color(color-1) return
    shapeColor(origColor)

15
Implementation of shapeColor Class
  • // prefix decrement
  • shapeColor shapeColoroperator-- ()
  • if (color black) color white
  • else color drawing_color(color-1)
  • return this
  • // use lookup table to convert to Windows color
    definitions
  • ezdColor shapeColorconvertToEzdColor()
  • return g_aColorscolor

16
Implementation of shapeColor Class
  • // relational operators
  • bool operator (const shapeColor lhs,
  • const shapeColor rhs)
  • return lhs.color rhs.color
  • bool operator! (const shapeColor lhs,
  • const shapeColor rhs)
  • return lhs.color ! rhs.color
  • bool operatorlt (const shapeColor lhs,
  • const shapeColor rhs)
  • return lhs.color lt rhs.color

17
Implementation of shapeColor Class
  • bool operatorlt (const shapeColor lhs,
  • const shapeColor rhs)
  • return lhs.color lt rhs.color
  • bool operatorgt (const shapeColor lhs,
  • const shapeColor rhs)
  • return lhs.color gt rhs.color
  • bool operatorgt (const shapeColor lhs,
  • const shapeColor rhs)
  • return lhs.color gt rhs.color

18
Hierarchy of shapes
19
Shape Class - base class
  • // The graphics base class. Maintains the base
    point and fill
  • // color. Has functions to access and change
    these
  • // attributes. The graphics classes that draw
    specific
  • // figures inherit this class.
  • class shape
  • protected
  • double baseX, baseY // location of
    the base point
  • shapeColor color // color of
    the shape
  • EZDHANDLE shape_handle // handle to the
    shape

20
Shape Class - base class
  • public
  • // the arguments initialize the base point (x,y)
    and the
  • // fill color c
  • shape(double x, double y, shapeColor c)
  • // virtual destructor may be necessary for
    derived class
  • virtual shape()
  • // returns the x coordinate of the base point
  • double getX() const
  • // returns the y coordinate of the base point
  • double getY() const

21
Shape Class - base class
  • // repositions the base point to the new
    coordinates (x,y)
  • void move(double x, double y)
  • // returns the current fill color for the figure
  • shapeColor getColor() const
  • // set the fill color of the figure to the value
    c from the
  • // color palette
  • void setColor(shapeColor c)
  • // draw the shape. must be implemented in a
    derived class
  • virtual void draw() 0

22
Shape Class - base class
  • // removes the figure from the drawing surface
  • void erase()
  • // update the shape
  • virtual void update()

23
Implementation of shape class
  • // initialize basepoint coordinates and color.
    specify that no
  • // shape has been drawn by setting shape_handle
    to 0
  • shapeshape(double x, double y, shapeColor c)
  • baseX(x), baseY(y), color(c),
    shape_handle(0)
  • // shape class destructor
  • shapeshape()
  • // access base point
  • double shapegetX() const
  • return baseX
  • double shapegetY() const
  • return baseY

24
Implementation of shape class
  • // retrieve shape color
  • shapeColor shapegetColor() const
  • return color
  • // modify shape color
  • void shapesetColor(shapeColor c)
  • color c
  • // change the position of the base point
  • void shapemove(double x, double y)
  • baseX x baseY y

25
Implementation of shape class
  • void shapeerase()
  • // shape_handle identifies the shape in the
    window.
  • // the shape is defined if shape_handle is not 0
  • if (shape_handle ! 0)
  • ezdDeleteShape(shape_handle) // erase
    the shape
  • shape_handle 0 // no shape
    defined
  • void shapeupdate()
  • erase()
  • draw()

26
circleShape Class - Derived class
  • include "d_shape.h"
  • // declaration of circleShape class with base
    class shape
  • class circleShape public shape
  • private
  • double radius // radius of the circle
  • public
  • // arguments for the base point, radius and
    color
  • circleShape(double x 0.0, double y 0.0,
  • double r 0.0, shapeColor c
    darkgray)

27
circleShape Class - Derived class
  • // retrieve or set the radius
  • double getRadius() const
  • void setRadius(double r)
  • // draw the circle
  • virtual void draw()

28
Implementaton of circleShape
  • // constructor
  • circleShapecircleShape(double x, double y,
    double r,
  • shapeColor c) shape(x,y,c),
    radius(r)
  • // return value of private radius data
  • double circleShapegetRadius() const
  • return radius
  • // change the radius value of the current object
  • void circleShapesetRadius(double r)
  • radius r // assign r as the new
    radius

29
Implementaton of circleShape
  • // draw the circleShape with center at (x,y),
    given radius
  • // and color
  • void circleShapedraw()
  • EZDCOLORVAL old_color
  • old_color ezdSetColor(color.convertToEzdColor
    ())
  • shape_handle ezdDrawCircle(baseX, baseY,
    radius)
  • ezdSetColor(old_color)

30
rectShape Class - derived class
  • include "d_shape.h"
  • // declaration of rectShape class with base class
    shape
  • class rectShape public shape
  • private
  • double length, width // rectangle
    dimensions
  • public
  • // constructor. has parameters for the base
    point,
  • // length, width and color
  • rectShape(double x 0.0, double y 0.0,
  • double len 0.0, double
    wid 0.0,
  • shapeColor c darkgray)

31
rectShape Class - derived class
  • // retrieve or set rectangle dimensions
  • double getLength() const
  • double getWidth() const
  • void setSides(double len, double wid)
  • // draw the rectangle
  • virtual void draw()

32
Implementation of rectShape
  • // constructor
  • rectShaperectShape(double x, double y, double
    len, double wid, shapeColor c) shape(x,y,c),
    length(len), width(wid)
  • // retrieve length and width
  • double rectShapegetLength() const
  • return length
  • double rectShapegetWidth() const
  • return width

33
Implementation of rectShape
  • // change size of rectangle
  • void rectShapesetSides(double len, double wid)
  • length len width wid
  • void rectShapedraw()
  • EZDCOLORVAL old_color
  • old_color ezdSetColor(color.convertToEzdColor
    ())
  • // execute primitive function and draw the
    rectangle
  • shape_handle ezdDrawRectangle(baseX, baseY,

  • baseXlength,baseYwidth)
  • ezdSetColor(old_color)

34
squareShape Class - derived class
  • include "d_rectShape.h"
  • // declaration of squaretShape class with base
    class rectshape
  • class squareShapepublic rectShape
  • public
  • squareShape(double x 0.0, double y 0.0,
  • double s 0.0,
    shapeColor c darkgray)
  • // retrieve and store sides
  • double getSides() const
  • void setSides(double s)
  • // draw the square
  • virtual void draw()

35
Implement squareShape Class
  • // constructor
  • squareShapesquareShape(double x, double y,
    double s, shapeColor c) rectshape(x,y,s,s,c)
  • // retrieve and store sides
  • double squareShapegetSides() const
  • return getLength()
  • void squareShapesetSides(double s)
  • setSides(s,s)
  • // draw the square
  • void squareShapedraw()
  • rectShapedraw()

36
polyShape Class - derived class
  • include ltvectorgt
  • include ltcmathgt
  • include "d_shape.h"
  • // declaration of polygonShape class with base
    class shape
  • class polyShape public shape
  • private
  • int numSides // number of sides
  • double length // length of each side
  • // x and y-coordinates of the vertices
  • vectorltdoublegt point_X
  • vectorltdoublegt point_Y
  • void buildPoints() // construct the
    vertices

37
polyShape Class - derived class
  • public
  • // constructor. has arguments for the base
    point, number // of sides, the length of each
    side and the color
  • polyShape(double x 0.0, double y 0.0, int n
    4, double len 0.0, shapeColor c
    darkgray)
  • // retrieve or set length of a side
  • double getLength() const
  • void setLength(double len)

38
polyShape Class - derived class
  • // retrieve or set number of sides
  • int getN()
  • void setN(int numsides)
  • // draw the polygon
  • virtual void draw()

39
  • void polyShapebuildPoints()
  • int side
  • double theta, d
  • const double PI 3.141592653589793,
  • DELTA_THETA
    (2.0PI)/numSides
  • // allocate space for the numSides coordinates
  • point_X.resize(numSides)
  • point_Y.resize(numSides)
  • d length/(2.0sin(PI/numSides))
  • theta 0.0
  • for(side 0 side lt numSides side)
  • point_Xside baseX dcos(theta)
  • point_Yside baseY - dsin(theta)
  • theta DELTA_THETA

40
Implementation of polyShape
  • // initialize base class, the number of sides and
    the length
  • // of each side
  • polyShapepolyShape(double x, double y, int n,
    double len, shapeColor c) shape(x,y,c),
    numSides(n),
  • length(len), point_X(0), point_Y(0)
  • // retrieve the length of the sides
  • double polyShapegetLength() const
  • return length

41
Implementation of polyShape
  • // change the length of a side
  • void polyShapesetLength(double len)
  • length len
  • int polyShapegetN()
  • return numSides
  • void polyShapesetN(int n)
  • numSides n

42
Implementation of polyShape
  • void polyShapedraw()
  • EZDCOLORVAL old_color
  • // build the polygon's points and draw it
  • buildPoints()
  • old_color ezdSetColor(color.convertToEzdColor
    ())
  • shape_handle ezdDrawPolygon(numSides,
    point_X0, point_Y0)
  • ezdSetColor(old_color)

43
EZDraw functions
  • include "ezdraw.h"
  • void openWindow() // initialize drawing
    surface
  • void viewWindow() // wait until a key is
    pressed
  • void eraseWindow() // erase the drawing
    window
  • void closeWindow() // close the drawing
    surface
  • // wait secs seconds before executing next
    instruction
  • void delayWindow(double secs)
  • // return true if a key pressed otherwise,
    return false
  • bool keyPress()

44
Program using graphics package
  • include "squareshape.h" // squareShape class
  • include "d_draw.h" // drawing functions
  • int main()
  • // begin with the large square at (2.5, 1.5),
    sides 5,
  • // and color lightblue
  • squareShape sq(2.5,1.5,5,lightblue)
  • // use c to cycle through the colors
  • shapeColor c lightblue
  • // location and side of a square. set to the
    values
  • // of the large square
  • double x 2.5, y 1.5, side 5.0
  • int i

45
  • // create the drawing window
  • openWindow()
  • for (i0i lt 5i)
  • sq.draw() // draw the current square shape
  • c // move to the next color in the palette
  • side - 1.0 // decrease the side by 1.0
  • x 0.5 // increase x and y by 0.5
  • y 0.5
  • sq.setColor(c) // assign the new attributes
  • sq.setSide(side)
  • sq.move(x,y)
  • viewWindow() // view the nested squares
  • closeWindow() // close the window
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com