300 likes | 450 Views
CSE 20232 Lecture 20 – Arrays & Classes. How array locations are addressed C++ classes Reading assignment What is a class? What is an object? Structure of a class declaration Access (public vs. private) Using an object Examples. Arrays addresses. grid in memory. Index values Address.
E N D
CSE 20232Lecture 20 – Arrays & Classes • How array locations are addressed • C++ classes • Reading assignment • What is a class? • What is an object? • Structure of a class declaration • Access (public vs. private) • Using an object • Examples
Arrays addresses grid in memory IndexvaluesAddress // declaration of 2-d array #define CMAX 4 #define RMAX 3 int grid[RMAX][CMAX]; grid logical arrangement of grid sizeof(int) is 4 bytes in row is CMAX*sizeof(int) or 16 &(grid[0][0]) is 1024 &(grid[1][0]) is 1024 + 16 or 1040 In general ... &(grid[r][c]) = &(grid[0][0]) + (r*CMAX+c)*sizeof(int)
Reading • Classes • Textbook sections • 3.1 – 3.10 • 9.1 – 9.10
What are CLASS & OBJECT? • A class is … • A collection of data members • That represent the state of each object of the class • A set of member functions for … • accessing and manipulating the data members • An object is … • A particular instance of a class • Each object has its own data members and share access to the class member functions
Public vs. Private • Public parts of the class • Establish the API, the Application Programmer Interface, for that class • Most member functions are public • Private parts of the class • Allow hiding of implementation and representation details that are not needed by application programmer • Data members are almost always private • Information Hiding • Provide the user of a class, function or data type ONLY the imformation essential to its proper use – HIDE the rest
Types of member functions • Constructors • Called when an object of the class is declared or created • C++ provides a default constructor • Destructors • Called when an object ceases to exist • C++ provides a default destructor • Accessors • Provide access to data member values • Mutators • Allow changes to data member values
Types of nonmember functions • Facilitators, helpers, utilities • Perform actions related to data members • These are functions like … • show(), draw(), input(), output() • These may be overloaded operators like … • >>, <<, ==, +, … • These are often related to I/O, display, etc. • These are often implemented as friend functions • Friends have access to the private class members
What you get for free. • C++ provides these by default for any created class • Constructor (called when objects declared) • Sets all data members to the default values for the data member’s type • Copy Constructor (called when objects are copied) • When object is passed as a value parameter • When object is returned as value of function • When object is declared as a copy of another object • Destructor • Assignment operator (A = B;) • Assigns data members of A to same values as data members of B
Existing classes • C++ is full of predefined classes and objects • Basic data types • int, bool, double, … • Other types • string, • Streams • istream, ostream, ifstream, ofstream • Note: cin and cout are instances of istream and ostream – they are objects
Simple C++ class declaration class <name> { public: <public member function prototypes> private: <data members> <private member function prototypes> };
C++ class declaration // example class – point in 2-D Cartesian plane class point { public: // constructor, allowing us to set values point(double x = 0.0, double y = 0.0); // accessor function void get(double &x, double &y); // mutator function void set(double x, double y); private: double xPosit, yPosit; // hidden data members };
C++ class functions // example class – point in 2-D Cartesian plane // the class name and scope resolution operator (point::) // are required so the compiler knows that this function // is part of the point class // constructor, allowing us to set values point::point(double x, double y) : xPosit(x), yPosit(y) { // the initialization list “: xPosit(x), yPosit(y)” // replaces these assignments in a more efficient way // xPosit = x; // yPosit = y; }
C++ class functions // example class – point in 2-D Cartesian plane // accessor function void point::get(double &x, double &y) { x = xPosit; y = yPosit; } // mutator function void point::set(double x, double y) { xPosit = x; yPosit = y; }
Where? • The entire class declaration can be placed in a singe header file // point.h – JHS 2006 class point //point in 2-D Cartesian plane { public: point(double x = 0.0, double y = 0.0); void get(double &x, double &y); void set(double x, double y); private: double xPosit, yPosit; // hidden data members }; point::point(double x, double y) : xPosit(x), yPosit(y) { } void point::get(double &x, double &y) { x = xPosit; y = yPosit; } void point::set(double x, double y) { xPosit = x; yPosit = y; }
Where? • Or BETTER, we place the class declaration in the header file and the function implementations in a similarly named .cpp file // point.h – JHS 2006 // the point class declaration (its API) class point //point in 2-D Cartesian plane { public: point(double x = 0.0, double y = 0.0); void get(double &x, double &y); void set(double x, double y); private: double xPosit, yPosit; // hidden data members };
Where? • Or BETTER, we place the class declaration in the header file and the function implementations in a similarly named .cpp file // point.cpp – JHS 2006 // the point class function implementations #include “point.h” point::point(double x, double y) : xPosit(x), yPosit(y) { } void point::get(double &x, double &y) { x = xPosit; y = yPosit; } void point::set(double x, double y) { xPosit = x; yPosit = y; }
Using the point class // usePoint.cpp – JHS 2006 #include <iostream> #include “point.h” program output using namespace std; point (3.5,-4.5) int main() point (37.0,12.0) { double over, up; point p(3.5, -4.5); p.get(over,up); cout << “point (“ << over << ‘,’ << up << “)\n”; p.set(37.0, 12.0); p.get(over,up); cout << “point (“ << over << ‘,’ << up << “)\n”; return 0; }
Class relationships • Class A is a refined version of class B • This involves inheritance and we sill discuss that near semester end • Class A has an instance of class B inside it • Example: a line class might have two point class objects marking the ends of the line segment
Line class using points // line.h – JHS 2006 // example class – line segment in 2-D Cartesian plane #include “point.h” class line { public: // default line is point(0,0) to point(1,0) line(point p1 = point(), point p2 = point(1.0,0.0)); point getBegin(); point getEnd(); void setBegin(point p); void setEnd(point p); void get(double &x1, double &y1, double &x2, double &y2); void set(double x1, double y1, double x2, double y2); double length(); private: point beginPt, endPt; // hidden data members };
Line class functions // line.cpp – JHS 2006 // example class – line segment in 2-D Cartesian plane #include “line.h” // default line is point(0,0) to point(1,0) line::line(point p1 = point(), point p2 = point(1.0,0.0)) : beginPt(p1), endPt(p2) { } // return a copy of the beginning point of the line point line::getBegin() { return beginPt; } // return a copy of the end point of the line point getEnd() { return endPt; }
Line class functions // line.cpp (continued) // set the position of the beginning point to (x1,y1) and // set the position of the end point to (x2,y2) void line::get(double &x1, double &y1, double &x2, double &y2) { beginPt.get(x1,y1); endPt.get(x2,y2); } // get the position of the beginning point (x1,y1) and // get the position of end point (x2,y2) void line::set(double x1, double y1, double x2, double y2) { beginPt.set(x1,y1); endPt.set(x2,y2); }
Line class functions // line.cpp (continued) // set the position of the beginning point to p void line::setBegin(point p) { beginPt = p; // uses default = operator } // get the position of the beginning point through p void line::setEnd(point p) { endPt = p; // uses default = operator }
Line class functions // line.cpp (continued) // compute the length of the line segment double line::length() { double x1, y1, x2, y2, dx, dy; // get components of beginning and end point beginPt.get(x1,y1); endPt.get(x2,y2); // compute change in x and change in y dx = x2 – x1; dy = y2 – y1; // find length and return it return sqrt(dx*dx + dy*dy); }
Using Lines // determine if two lines intersect // find point of intersection of two lines (L1 & L2) // set point (ip) as that point. Return true if // intersection exists, false otherwise. bool findIntersection(line L1, line L2, point ip) { double x1, y1, x2, y2, dx, dy, // line 1 values x3, y3, x4, y4, dx2, dy2; // line 2 values // get components of beginning and end point L1.get(x1,y1,x2,y2); L2.get(x3,y3,x4,y4); // compute change in x and y for both lines dx1 = x2 – x1; dy1 = y2 – y1; dx2 = x4 – x3; dy2 = y4 – y3;
Using Lines // in parametric form, the two line equations are ... // L1: x = s*dx1 + x1 and y = s*dy1 + y1 // L2: x = t*dx2 + x3 and y = t*dy2 + y3 // after some algebra ...... // t = (dy1*(x1-x3)+dx1(y1-y3))/(dx2*dy1+dx1*dy2) // and s = t*(dx2/dx1) = t*(dy2/dy1) double numer = dy1*(x1-x3) + dx1(y1-y3), denom = dx2*dy1 + dx1*dy2; // if (dx2*dy1+dx1*dy2 == 0) then the lines are // parallel or coincident and there is NO intersection if (denom == 0.0) return false;
Using Lines // if (dx2*dy1+dx1*dy2 != 0) then the lines do intersect // at t = (dy1*(x1-x3)+dx1(y1-y3))/(dx2*dy1+dx1*dy2) // and s = t*(dx2/dx1) = t*(dy2/dy1) double t = numer / denom, s = t*(dx2/dx1); // (0 <= s <= 1 and 0 <= t <= 1) must be true for the // point of intersection to lie ON both line segments if ((t < 0.0) || (1.0 < t) || (s < 0.0) && (1.0 < s)) return false; // intersection is outside segment bounds ip.set(x1+s*dx1, y1+s*dy1); // set intersection point x & y return true; // indicate intersection found }
De Morgan’s Law • When discussing logical expressions we should have discussed De Morgan’s Law • Given two boolean expressions (p, q) … • De Morgan’s Law states … • not (p and q) == (not p) or (not q) • not (p or q) == (not p) and (not q) • In C++ this gives us … • !(p && q) == (!p) || (!q) • !(p || q) == (!p) && (!q)
De Morgan’s Law (example) • Checking whether value is outside two bounds low and hi … • if ( ! ((low <= value) && (value <= hi)) ) • Is the same as … • if ( (!(low <= value)) || (!(value <= hi)) ) • Is the same as … • if ( (low > value) || (value > hi) )
Next time • More on classes