Project 6 – Building a Class

1 / 15

# Project 6 – Building a Class - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Project 6 – Building a Class' - tania

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
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() )
• Finding the greatest common divisor (gcd)
• Euclidean Algorithm
• Copy Constructor
• Assignment Operator
A Point Class
• A point in the cartesian plane can be represented by a 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

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

#include // 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
• Initialization Lists
• Use of ostringstream.
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
• 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
• 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
• 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.
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

long gcd(long m, long n)

{long r;

while( n > 0 )

{

r = m%n;

m = n;

n = r;

}

return m;

}

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()
• Think about this one. It probably will require use of the gcd(), as well as some logical checks