Today - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Today

Description:

... due 9pm Friday before Spring Break. Project 7 Friday after Spring Break ... Make background blue to black rather than chromatic. Spring 2003 CS 325 Class Notes ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 22
Provided by: janett
Category:
Tags: black | break | spring | today

less

Transcript and Presenter's Notes

Title: Today


1
Lecture 12
  • Today
  • Questions
  • Increment and Decrement operators
  • DLLs
  • Announcements
  • Project 5 (Checkers) due 9pm Tuesday
  • Project 6 (Checkers II) due 9pm Friday before
    Spring Break
  • Project 7 Friday after Spring Break
  • Exam 2 Week Following Spring BReak

2
MapObjects
  • See the zip File
  • Dialog Bug -- Chase Option
  • Is not remembering previous setting
  • Fix this
  • Add another Option to dialog box
  • Change the colors of the chasing Objects
  • Make text background transparent
  • Make default settings all off
  • Make background blue to black rather than
    chromatic

3
Change train of thought
  • Trace this code
  • int a 5
  • cout ltlt "a is " ltlt a
  • cout ltlt " a afterward is " ltlt a ltlt endl
  • a5
  • cout ltlt "a is " ltlt a
  • cout ltlt " a afterward is " ltlt a ltlt endl
  • a5
  • cout ltlt "a-- is " ltlt a--
  • cout ltlt " a afterward is " ltlt a ltlt endl
  • a5
  • cout ltlt "--a is " ltlt --a
  • cout ltlt " a afterward is " ltlt a ltlt endl

4
What do you think about this?
  • int notfoo(int x)
  • return x
  • int main()
  • int a0
  • anotfoo(a)
  • cout ltlt a ltlt endl
  • return 0

5
Overloading --
  • What are some properties of and - - we will
    need to maintain when we overload these?
  • Question 1 What does it mean to increment or
    decrement concerning the type we want to or
  • Question 2 Where are the and - - ops used?
  • Question 3 Is it prefix or postfix
  • A How do we tell the difference between the two
  • B What should it return and when

6
Lets do the Fraction class
  • Q1 Incrementing a Fraction means adding the
    value of the denominator to the numerator
  • Q2 Can be stand-alone, or inside another
    expression that means it needs to return a value
  • Q3A C tells us how to write these methods so
    the compiler can tell the difference
  • Prefix takes no parameters
  • Postfix takes a dummy integer parameter
  • Q3B
  • Prefix should return the changed value
  • Postfix should return value before it is changed

7
Prefix and Postfix
  • Fraction operator()
  • //prefix
  • this-gtnumthis-gtden
  • return this
  • Fraction operator(int)
  • //postfix
  • Fraction temp this
  • this-gtnumthis-gtden
  • return temp

8
Class Exercises
  • Modify Zip13
  • Add the code for the operators that we just
    did.
  • Add the code for the - - operators as well.
  • Take off the int parameter on the postfix
    operations and see what kind of error you get
    (compile, link, name)
  • Make the or -- friend functions rather than
    methods.

9
Building using components
  • Components can mean different things
  • Subprogram
  • Class
  • DLL (Dynamic Link Library)
  • COM (Common Object Model)
  • CORBA (Common Object Request Broker Architecture)
  • First two imply source code as components
  • Last three utilize binary code as components

10
Source Binary Components
  • Class (source-based) approach
  • Build source code in individual components
  • Link these components together
  • Must rebuild the entire system every time a
    component is changed/modified
  • Binary components are treated as separate units
    at the executable (system) level
  • Example DLL files
  • The application links to appropriate DLLs as
    needed during execution (at run-time)

11
Benefits to Binary Components
  • Applications can share components
  • Less disk space required for applications
  • Components can be reused in applications
  • Upgrades to a component do not require
    recompilation of the applications
  • Can upgrade applications by simply upgrading
    individual components, but be careful!
  • Upgrading an implementation of a DLL is fine
  • Upgrading the interface can be dangerous
  • Why?

12
Consider an example with DLLs
  • Simple windows painting program
  • Left click draws randomly colored/sized rectangle
  • Right click draws randomly colored/sized ellipse
  • OnPaint redraws as needed
  • Define a new data type to store information about
    each shape
  • Need location, size, color, ellipse or rectangle
  • Use a vector of this data type in our application

13
Build this application with DLLs
  • 1st Build the DLL (the new data type)
  • Built in its own MFC project
  • Need to create a .lib file
  • Need to create a .dll file
  • 2nd Build the application
  • A different (second) MFC project
  • Must link in references to DLL object
  • Include header file for compilation
  • Include .lib file for linking state

14
Building the DLL
  • In VC, select
  • New
  • Project
  • Win32 Dynamic Link Library
  • Create header file
  • images.h
  • Note the extra stanza that must be added to the
    header file
  • Two underscores at the start of declspec
  • include ltafxwin.hgt
  • class __declspec(dllexport) Images
  • publicImages()COLORREF GetColor( )int
    GetShape( )int GetSize( )CPoint GetPoint(
    )void SetColor(COLORREF)void
    SetShape(int)void SetSize(int)void
    SetPoint(CPoint)
  • privateCOLORREF mColorint mShape,
    mSizeCPoint mPoint

15
Building the DLL (continued)
  • include "images.h"
  • ImagesImages()
  • COLORREF ImagesGetColor() return mColor
  • CPoint ImagesGetPoint() return mPoint
  • int ImagesGetShape() return mShape
  • int ImagesGetSize() return mSize
  • void ImagesSetColor(COLORREF c) mColor
    c
  • void ImagesSetPoint(CPoint p) mPoint p
  • void ImagesSetShape(int s) mShape s
  • void ImagesSetSize(int s) mSize s
  • The body of the class simply contains the code
    needed to implement all the various methods in
    the class

16
Building the DLL (last steps)
  • Under Build menu, select build xxx.dll
  • This should generate two files of interest
  • xxx.dll, the binary component we are interested
    in, located in the Debug directory
  • xxx.lib, library information that tells where the
    component code exists (a link to the DLL), also
    located in the Debug directory
  • Can now use this class in other applications

17
Building our Application
  • In VC
  • New
  • Project
  • Win32 application
  • Header
  • Include the header for DLL class
  • Keep vector of images
  • include "..\ImagesDLL\images.h"
  • include ltafxwin.hgt
  • include ltvectorgt
  • using namespace std
  • class CPaintWin public CFrameWnd
  • publicCPaintWin()afx_msg void
    OnLButtonDown(UINT, CPoint)afx_msg void
    OnRButtonDown(UINT, CPoint)afx_msg void
    OnPaint()
  • privatevectorltImagesgt dataDECLARE_MESSAGE_MAP(
    )

18
Building our application (cont)
  • OnLButtonDown and OnRButtonDown are similar in
    nature
  • Get a random size
  • Get a random color
  • Declare a new Images object and then sets
  • Color
  • Size
  • Shape (1rect, 2circle)
  • Location
  • Push this object onto our vector of Images
  • //this is main.cpp
  • afx_msg void CPaintWinOnLButtonDown (UINT
    flags, CPoint point)
  • int size, r, g, bsize rand()50 10r
    rand()255g rand()255b
    rand()255COLORREF c RGB(r,g,b)Images
    pp.SetColor(c)p.SetSize(size)p.SetShape(1)/
    /2 w/right buttonp.SetPoint(point)data.push_bac
    k(p)InvalidateRect(NULL)

19
Building our application (cont)
  • OnPaint routine runs thru the vector of Images
    and draws each of them
  • What is bad about this?
  • afx_msg void CPaintWinOnPaint( ) CPaintDC
    dc(this)if ( data.empty( ) ) return // handles
    initial windowvectorltImagesgtiterator afor
    (adata.begin() altdata.end() a) // print
    all Images CPoint p a-gtGetPoint( ) int s
    a-gtGetSize( ) CBrush pBrush new CBrush(
    ) pBrush-gtCreateSolidBrush(a-gtGetColor( ))
    if (a-gtGetShape( )1) // rectangle
    dc.FillRect(CRect(p.x-s/2,p.y-s/2,p.xs/2,p.ys/2)
    ,pBrush) else // ellipse CPen pPen
    new CPen() pPen-gtCreatePen(PS_SOLID, 1,
    a-gtGetColor()) dc.SelectObject(pPen)
    dc.SelectObject(pBrush) dc.Ellipse(CRect(p.x-s/
    2, p.y-s/2, p.xs/2, p.ys/2))

20
Compiling our application
  • Project / Settings
  • General Using MFC in a Shared DLL
  • Link Object/Library Modules
  • Must add location of DLL library file
  • Example ..\ImagesDLL\Debug\ImagesDLL.lib
  • If you name your dll project something else, the
    names wont be the same as this example
  • This should allow application to compile
  • To run executable, need access to the DLL file
  • Must be in a known location
  • Would normally want it in the system path
  • Copy it to the application directory (in the
    DEBUG folder)

21
Class Exercises
  • The files needed to generate an MFC application
    using DLLs are out on the web
  • DLL part
  • Snippita contains Images.h
  • Snippitb contains Images.cpp
  • Front End
  • Snippitc contains PaintWin.h
  • Snippitd contains PaintWin.cpp
  • Build this application (using DLLs)
Write a Comment
User Comments (0)
About PowerShow.com