Reusing Code example: Class Quadrilateral - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Reusing Code example: Class Quadrilateral

Description:

That people are by nature lazy (which for us means that we'd ... { cout 'The roach was somewhere between '; write(A[k-1].position,cout); cout ' and ' ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 13
Provided by: need9
Category:

less

Transcript and Presenter's Notes

Title: Reusing Code example: Class Quadrilateral


1
Reusing Code example Class Quadrilateral
  • Consider a class to represent quadrilaterals,
    i.e. polygons with four sides, and
  • That people are by nature lazy (which for us
    means that wed like to reuse code).
  • A quadrilateral is defined by its four vertices,
    which are of course Points (we include a char
    that is used as a label
  • class Quad
  • public
  • char label
  • Point vert
  • int main()
  • // Create quadrilateral S
  • Quad S
  • S.vert new point4
  • // Read label and vertices
  • cout ltlt "Enter label and 4 vertices "
  • cin gtgt S.label
  • for(int i 0 i lt 4 i)
  • cin gtgt S.verti
  • // Write label and vertices
  • cout ltlt "Your quadrilateral is " ltlt S.label ltlt
    " "
  • for(int j 0 j lt 4 j)
  • cout ltlt S.vertj ltlt ' '
  • cout ltlt endl
  • return 0

2
Header files and Definition (.cpp) files
  • // in file Point.h
  • include ltiostreamgt
  • include ltfstreamgt
  • using namespace std
  • class point
  • public
  • double x, y
  • void read(point p, istream IN)
  • void write(point p, ostream OUT)
  • point operator(point a, point b)
  • point operator/(point P, double z)
  • ostream operatorltlt(ostream OUT, point p)
  • istream operatorgtgt(istream IN, point p)
  • // in file Point.cpp
  • include Point.h
  • void read(point p, istream IN)
  • char c
  • IN gtgt c gtgt p.x gtgt c gtgt p.y gtgt c
  • point operator(point a, point b)
  • point S
  • S.x a.x b.x
  • S.y a.y b.y
  • return S
  • istream operatorgtgt(istream IN, point p)
  • read(p,IN)
  • return IN

3
main() that includes point.h
  • A .h file (or header file) tells how to use the
    module, and
  • a .cpp file that contains the source code that
    implements the module (i.e. that tells the
    compiler how all these functions really work).
  • include "point.h"
  • class Quad
  • public
  • char label
  • point vert
  • int main()
  • // Create quadrilateral S
  • Quad S
  • S.vert new point4
  • // Read label and vertices
  • cout ltlt "Enter label and 4 vertices "
  • cin gtgt S.label
  • for(int i 0 i lt 4 i)
  • cin gtgt S.verti
  • // Write label and vertices
  • cout ltlt "Your quadrilateral is " ltlt S.label ltlt
    " "
  • for(int j 0 j lt 4 j)
  • cout ltlt S.vertj ltlt ' '
  • cout ltlt endl
  • return 0

4
Static Arrays
  • We've seen dynamic arrays, where the size of the
    array can change from run to run of the program.
  • Ship shipsAtSeaArray new ShipN // where
    user enters value for N
  • Useful if the size of the array may need to
    change from run to run of a program.
  • A static array s size is determined at compile
    time, so from run to run, the array's size is
    always the same
  • int daysOfTheWeek7 // always 7 days in a
    week!
  • Can initialize static arrays with values in s
  • int prime10 2,3,5,7,11,13,17,19,23,29

5
Reconsider Class Quad
  • class Quad
  • public
  • char label
  • Point vert4 // static
  • class Quad
  • public
  • char label
  • Point vert // dynamic

6
Comparing static and dynamic arrays
  • include "point.h"
  • class Quad
  • // identical other than vert
  • public
  • char label
  • point vert4
  • int main()
  • // Create quadrilateral S
  • Quad S
  • // Read label and vertices
  • cout ltlt "Enter lbl and 4 vertices"
  • cin gtgt S.label
  • for(int i 0 i lt 4 i)
  • cin gtgt S.verti
  • include "point.h"
  • class Quad
  • public
  • char label
  • point vert
  • int main()
  • // Create quadrilateral S
  • Quad S
  • S.vert new point4
  • // Read label and vertices
  • cout ltlt "Enter lbl and 4 vertices"
  • cin gtgt S.label
  • for(int i 0 i lt 4 i)

7
Tracking Cockroaches revisited
  • Break the following program up into separate
    modules
  • The Point" module
  • Point.h and Point.cpp.
  • The Hhmmss" module
  • Hhmmss.h and Hhmmss.cpp.
  • And the "main program" main.cpp which includes
    class Datum.

8
Classes Point, Hhmmss, and Datum
  • class Point
  • public
  • double x, y
  • void read(Point p, istream IN)
  • void write(Point p, ostream OUT)
  • //--- TIME IN HHMMSS
  • class Hhmmss
  • public
  • int h,m,s
  • void read(Hhmmss T, istream IN)
  • //--- A DATA READING FROM THE EXPERIMENT ----//
  • class Datum
  • public
  • Point position
  • Hhmmss time
  • void read(DatumD, istream IN)
  • Note an instance of Datum is composed of a Point
    and a Hhmmss instance

9
The main() program
  • int main()
  • int N string s
  • ifstream IN("trial.txt")
  • IN gtgt N gtgt s gtgt s
  • // Read and store data readings
  • Datum A new DatumN
  • for(int i 0 i lt N i)
  • read(Ai,IN)
  • // Get query time from the user
  • Hhmmss T
  • cout ltlt "Enter a time "
  • read(T,cin)
  • / Find 1st sighting at or after given time /
  • int k 0
  • while (k lt N Ak.time lt T)
  • k
  • if (k 0)
  • cout ltlt "This was before the first sighting
    at "
  • write(A0.position,cout)
  • else if (k N)
  • cout ltlt "This was after the last sighting at
    "
  • write(AN-1.position,cout)
  • else
  • cout ltlt "The roach was somewhere between "
  • write(Ak-1.position,cout)
  • cout ltlt " and "
  • write(Ak.position,cout)
  • cout ltlt endl
  • return 0

10
Function Definitions
  • void read(Point p, istream IN)
  • char c
  • IN gtgt c gtgt p.x gtgt c gtgt p.y gtgt c
  • void write(Point p, ostream OUT)
  • OUT ltlt '(' ltlt p.x ltlt ',' ltlt p.y ltlt ')'
  • void read(Hhmmss T, istream IN)
  • char c
  • IN gtgt c gtgt T.h gtgt c gtgt T.m gtgt c gtgt T.s gtgt c
  • void read(Datum D, istream IN)
  • char c
  • IN gtgt c
  • read(D.time,IN)
  • IN gtgt c
  • read(D.position,IN)
  • IN gtgt c

11
ICE Converting Dates
  • Write a program that reads a date in mm/dd/yyyy
    format and prints it out in "dd monthName yyyy"
    format. Make use of a static array for
    monthNames
  • For example, entering
  • 8/9/1961
  • results in
  • 9 August 1961
  • Recall that a static array can be initialized
    with a list of values in 's.
  • For example, an array of the first 10 prime
    numbers can be constructed like this
  • int prime10 2,3,5,7,11,13,17,19,23,29

12
ICE Solution Converting Dates
  • include ltiostreamgt
  • include ltstringgt
  • int main()
  • // Create an array containing the month names
  • string Month12 "January", "February",
    "March", "April",
  • "May", "June", "July", "August",
    "September",
  • "October", "November", "December"
  • // Read date
  • int m, d, y
  • char c
  • cin gtgt m gtgt c gtgt d gtgt c gtgt y
  • // Write date
  • cout ltlt d ltlt ' ' ltlt Monthm - 1 ltlt ' ' ltlt y ltlt
    endl
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com