Project 6 building a class
This presentation is the property of its rightful owner.
Sponsored Links
1 / 15

Project 6 – Building a Class PowerPoint PPT Presentation


  • 77 Views
  • Uploaded on
  • Presentation posted in: General

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() )

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


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

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


A point class

A Point Class

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


Interface for the point class

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;

};


Test driver for the point class

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";

}


Point class implementation

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();

}


What s new

What’s New

  • Initialization Lists

  • Use of ostringstream.


Initialization lists

Initialization Lists

Point::Point(double x, double y) : _x(x), _y(y) { }

Point::Point(double x, double y)

{

_x = x;

_y = y;

}


Stream classes for strings

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


Ostringstream

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();

}


Euclidean algorithm

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.


Euclidean example

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


Euclidean implementation

Euclidean Implementation

long gcd(long m, long n)

{long r;

while( n > 0 )

{

r = m%n;

m = n;

n = r;

}

return m;

}


Gcd client

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;

}


Reducing to lowest terms norm

Reducing to lowest terms: norm()

  • Think about this one. It probably will require use of the gcd(), as well as some logical checks


  • Login