1 / 15

Project 6 – Building a Class

B Smith: Took 50 minutes. Score 3. Elaborated extensively on parts. B Smith: Cover during Recitation. Project 6 – Building a Class. Overview. Example using Point Class Implementation Initialization Lists Use of ostringstream (using toString() )

tania
Download Presentation

Project 6 – Building a Class

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. B Smith: Took 50 minutes. Score 3. Elaborated extensively on parts. B Smith: Cover during Recitation Project 6 – Building a Class

  2. Overview • Example using Point Class • Implementation • Initialization Lists • Use of ostringstream (using toString() ) • Finding the greatest common divisor (gcd) • Euclidean Algorithm • Copy Constructor • Assignment Operator

  3. A Point Class • A point in the cartesian plane can be represented by a Class

  4. B Smith: discuss use of const! Students use this example as the template for the project! Careful with the assignment operator and the copy constructor. Interface for the Point Class class Point { public: Point(double=0.0,double=0.0); // default constructor Point(const Point&); // copy constructor ~Point(); // destructor Point& operator=(const Point&); // assignment operator double getX() const; double getY() const; string toString() const; private: double _x, _y; };

  5. Test Driver for the Point Class int main() { Point p0; // invokes default constructor cout << "p0 = " << p0.toString() << "\n"; Point p1(5,-2); // invokes default constructor cout << "p1 = " << p1.toString() << "\n"; Point p2=p1; // invokes copy constructor cout << "p2 = " << p2.toString() << "\n"; p0 = p1; // invokes assignment operator cout << "p0 = " << p0.toString() << "\n"; cout << "p0.getX() = " << p0.getX() << "\n"; cout << "p0.getY() = " << p0.getY() << "\n"; }

  6. Point Class Implementation #include <sstream> // defines ostringstream class #include "Point.h" // defines Point class Point::Point(double x, double y) : _x(x), _y(y) { } Point::Point(const Point& p) : _x(p._x), _y(p._y) { } Point::~Point() { } Point& Point::operator=(const Point& p) { _x = p._x; _y = p._y; return *this; } double Point::getX() const { return _x; } double Point::getY() const { return _y; } string Point::toString() const { ostringstream output; output << "(" << _x << "," << _y << ")"; return output.str(); }

  7. What’s New • Initialization Lists • Use of ostringstream.

  8. Initialization Lists Point::Point(double x, double y) : _x(x), _y(y) { } Point::Point(double x, double y) { _x = x; _y = y; }

  9. Stream Classes for Strings • A mechanism that can be used to read from strings or write to strings • String streams provide a buffer • This buffer/string can be manipulated with special functions

  10. ostringstream • The toString() function uses a string stream to accumulate its output • ostringstream objects can use a member function called str(), which returns the buffer as a string string Point::toString() const { ostringstream output; output << "(" << _x << "," << _y << ")"; return output.str(); }

  11. Euclidean Algorithm • Given nonnegative integers m and n, this algorithm finds their greatest common divisor • for gcd(m,n) • n = 0? Then stop with m as the answer. Otherwise: • r (m mod n) • mn • nr • go to step 1.

  12. B Smith: This is not a good explanation! Euclidean Example gcd(40902, 24140) = gcd(24140, 16762) = gcd(16762, 7378) = gcd(7378, 2006)= gcd(2006, 1360) = gcd(1360,646)= gcd(646, 68) = gcd(68, 34)= gcd(34,0)= 34

  13. Euclidean Implementation long gcd(long m, long n) {long r; while( n > 0 ) { r = m%n; m = n; n = r; } return m; }

  14. gcd() client // client/driver function for gcd() int main() { int a = 24140, b = 16762; int c; c = gcd(a,b); cout << "The gcd of a and b is: " << c ; return 0; }

  15. Reducing to lowest terms: norm() • Think about this one. It probably will require use of the gcd(), as well as some logical checks

More Related