SDSU CS 596 OODP
Comments about Points Assignment

[To Lecture Notes Index]
San Diego State University -- This page last updated Sept. 21, 1995
----------

Contents of Points Assignment Lecture

  1. What the Issue? What is the Effect?
  2. Point.hh File
  3. Point.cc File
  4. Some Issues
    1. Operator Overload
    2. Copy and Memory Leaks

What the Issue? What is the Effect?


Version A
class Point {

public:
	float 	distance(Point A, Point B);
}

float Point::distance(Point a, Point b) {
	float P1, P2
	P1=(a.x-b.x)*(a.x-b.x);
	P2=(a.y-b.y)*(a.y-b.y);
	return sqrt(P1+P2);
}


Version B
float Point :: distance( Point begin, Point end ) {
	
	float deltaX = ( end.x - begin.x );
	float deltaY = ( end.y - begin.y );

	return  sqrt ( deltaX * deltaX  + deltaY * deltaY ) ;
}

main( ) 
{
	Point StartLocation;
	Point EndLocation;
	Point NotUsed;

	float distanceTraveled;

	distanceTraveled = NotUsed.distance(StartLocation, EndLocation);
};
Version C

float Point :: distance( Point a ) {
	
	float deltaX = ( x - a.x  );
	float deltaY = ( y - a.y  );

	return ( sqrt ( deltaX * deltaX  + deltaY * deltaY ) );
}

main( ) {
	Point StartLocation;
	Point EndLocation;

	float distanceTraveled;
	distanceTraveled = StartLocation.distance( EndLocation );
}

Version D
float distance( Point begin, Point end ) {
	
	float deltaX = ( end.getX() - begin.getX() );
	float deltaY = ( end.getY() - begin.getY() );

	return ( sqrt ( deltaX * deltaX  + deltaY * deltaY ) );
}

main( ) {
	Point StartLocation;
	Point EndLocation;

	float distanceTraveled;
	distanceTraveled = distance( StartLocation, EndLocation );
}

Version A
class Point {

public:
	void 	getXY( int&, int& );
}

void Point :: getXY( int& X, int& Y ) {
	X = xCoordinate;
	Y = yCoordinate;
}


Version B
class Point {

public:
	void 	getXY( int& X , int& Y );
}


Version C

class Point {

public:

	int 	getX() const;
	int	getY() const;
}

Version A
class Rectangle {
	private:

		Point P1;
		Point P2;
		Point P3;
		Point P4;
}

void Rectangle :: print() 
{
	P1.print();		// top left corner
	P2.print();		// top right corner
	P3.print();		// lower left corner
	P4.print();		// lower right corner
}
Version B

class Rectangle {
	private:

		Point TopLeftCorner;
		Point TopRightCorner;
		Point BottomLeftCorner;
		Point BottomRightCorner;
}

void Rectangle :: print() 
{
	TopLeftCorner.print();
	TopRightCorner.print();
	BottomLeftCorner.print();
	BottomRightCorner.print();
}

Version A

class Point {

public:
	void print() const;
}

void Point :: print() const {
	cout  	<< '(' << xCoordinate << ',' << yCoordinate << ")\n";
}

main( ) {
	Point displayLocation( 4, 5 );
	displayLocation.print();
}

Version B

class Point {
public:
	void print( ostream& output ) const;
}

void Point :: print( ostream& output ) const {
	output  << '(' << xCoordinate << ',' << yCoordinate << ")\n";
}

main( ) {
	Point displayLocation( 4, 5 );
	ofstream logFile( "LogPoints" );

	displayLocation.print( cout );
	displayLocation.print( logFile );
	displayLocation.print( cerr );
}

Version A
class Point {

public:
	void createpoint( point& aPoint );
}

void Point :: createpoint( point& aPoint )
{
	cout << "Enter X coordinate: ";
	cin >> aPoint.x;
	cout << "Enter Y coordinate: ";
	cin >> aPoint.y;
}


main( ) {
	Point DisplayLocation;
	
	DisplayLocation.createpoint( DisplayLocation );
}

Version B

int readInt( char* Request ) {
	cout << Request;
	int response;
	cin >> response;
	return response;
}

main( ) {
	Point DisplayLocation( 	readInt( "Enter X coordinate: " ),
				readInt( "Enter Y coordinate: " );
}

Version A
class Rectangle {

	Rectangle( const int, const int, const int, const int );
}


// This constructor creates a Rectangle from four integers. 
// The order in which the x pairs and y pairs are listed does not affect 
// the proper creation of the Rectangle

Rectangle :: Rectangle( const int a, const int b, const int c, const int d )
{
	// code not shown
}


Version B

class Rectangle {

	Rectangle( const int x1, const int y1, 
		   const int x2, const int y2 )
}

// ( x1, y1 ) = upper left corner, ( x2, y2 ) = lower right corner or 
// ( x1, y1 ) = upper right corner, ( x2, y2 ) = lower left corner

Rectangle :: Rectangle( const int x1, const int y1, 
			const int x2, const int y2 )
{
	// code not shown
}

Version A
Rectangle :: Rectangle ( Point r, Point s ) 
{
	int smallX;
	int largeX;
	int smallY;
	int largeY;

	if ( r.getX() <= s.getX() )  {
		smallX = r.getX();
		largeX = s.getX();
	} else {
		smallX = s.getX();
		largeX = r.getX();
	};

	if ( r.getY() <= s.getY() )  {
		smallY = r.getY();
		largeY = s.getY();
	} else {
		smallY = s.getY();
		largeY = r.getY();
	};

	UpperLeftCorner.setX( smallX );
	UpperLeftCorner.setY( smallY );
	LowerRightCorner.setX( largeX );
	LowerRightCorner.setY( largeY );
}

Rectangle :: Rectangle ( int x1,  int y1,  int x2,  int y2 ) 
{
	int smallX;
	int largeX;
	int smallY;
	int largeY;

	if ( x1 <= x2 )  {
		smallX = x1;
		largeX = x2;
	} else {
		smallX = x2;
		largeX = x1;
	};

	if ( y1 <= y2 )  {
		smallY = y1;
		largeY = y2;
	} else {
		smallY = y2;
		largeY = y1;
	};

	UpperLeftCorner.setX( smallX );
	UpperLeftCorner.setY( smallY );
	LowerRightCorner.setX( largeX );
	LowerRightCorner.setY( largeY );
}

Version B
Rectangle :: Rectangle ( Point r, Point s ) 
{
	int smallX = min( r.getX() , s.getX() );
	int largeX  = max( r.getX() , s.getX() );
	int smallY = min( r.getY() , s.getY() );
	int largeY = max( r.getY() , s.getY() );

	UpperLeftCorner.setX( smallX );
	UpperLeftCorner.setY( smallY );
	LowerRightCorner.setX( largeX );
	LowerRightCorner.setY( largeY );
}
Rectangle :: Rectangle (  int x1,  int y1,  int x2,  int y2 ) 
{
	int smallX = min( x1 , x2 );
	int largeX  = max( x1 , x2 );
	int smallY = min( y1 , y2 );
	int largeY = max( y1 , y2 );

	UpperLeftCorner.setX( smallX );
	UpperLeftCorner.setY( smallY );
	LowerRightCorner.setX( largeX );
	LowerRightCorner.setY( largeY );
}

Version C

Rectangle :: Rectangle ( Point r, Point s ) 
{
	setRectangleCorners( r.getX(),  s.getX(),  r.getY(),  s.getY() )
}

Rectangle :: Rectangle (  int x1,  int y1,  int x2,  int y2 ) 
{
	setRectangleCorners( x1,  y1,  x2,  y2 )
}

void Rectangle :: setRectangleCorners( int x1, int y1, int x2, int y2 )
{
	int smallX = min( x1 , x2 );
	int largeX  = max( x1 , x2 );
	int smallY = min( y1 , y2 );
	int largeY = max( y1 , y2 );

	UpperLeftCorner.setX ( smallX );
	UpperLeftCorner.setY ( smallY );
	LowerRightCorner.setX ( largeX);
	LowerRightCorner.setY ( largeY );
}

Version A
Rectangle :: Rectangle ( Point UpperLeftCorner, 
			 Point LowerRightCorner ) 
{
	// Using screen coordinates 
	// If input is in wrong order, correct the problem

	if ( ! ( UpperLeftCorner < LowerRightCorner ) )  {
		swap ( UpperLeftCorner , LowerRightCorner );
	};

	// Code removed
}

Version B
Rectangle :: Rectangle ( Point UpperLeftCorner, 
			 Point LowerRightCorner ) 
{
	// Using screen coordinates 
	// Handle invalid input

	if ( ! ( UpperLeftCorner < LowerRightCorner ) )  {
		cerr << "Corners not correct for creating a Rectangle" 
			<< endl;
		exit( 0 );
	};

	// Code removed
}

Version C

#include <assert.h>

Rectangle :: Rectangle ( Point UpperLeftCorner, 
			 Point LowerRightCorner ) 

	assert( UpperLeftCorner < LowerRightCorner ) ;

	// Code removed
}

Point.hh File

#ifndef	_Point_HH
#define	_Point_HH

#include <iostream.h>

class Point {

	public:
		Point( float x, float y );

		void print ( ostream& output ) const;

		int operator >= ( const Point&  a ) const;
		int operator <= ( const Point&  a ) const;

		float distance ( const Point& a ) const;
		Point operator + ( const Point& a ) const;
		Point operator - ( const Point& a ) const;

		float x() const;
		float y() const;
		float radiusVector() const;

	private:
		float xCoordinate;
		float yCoordinate;
};


#endif	// _Point_HH




Point.cc File


#include <Point.hh>
#include <math.h>

Point :: Point ( float x, float y ) {
	xCoordinate = x;
	yCoordinate = y;
}

void Point :: print ( ostream& output ) const {
	output << '(' << xCoordinate << ',' << yCoordinate << ")";
}


int Point :: operator >= ( const Point& a ) const {

	if (	( xCoordinate >= a.xCoordinate ) && 
		( yCoordinate >= a.yCoordinate ) 
	    )
		return TRUE;
	else return FALSE;
}


int Point :: operator <= ( const Point& a ) const {
	if (	( xCoordinate <= a.xCoordinate ) && 
		( yCoordinate <= a.yCoordinate ) 
	    )
		return TRUE;
	else return FALSE;
}
	 

float Point :: distance ( const Point& a ) const { 

	Point delta( 0, 0 );
	delta = *this - a;

	return sqrt( delta.x() * delta.x() +  delta.y() * delta.y() );
}


Point Point :: operator + ( const Point& a ) const {

	Point Sum( xCoordinate + a.xCoordinate , 
		   yCoordinate + a.yCoordinate );

	return Sum;
}

Point Point :: operator - ( const Point& a ) const {

	int deltaX = xCoordinate - a.xCoordinate;
	int deltaY = yCoordinate - a.yCoordinate;

	Point Difference( deltaX, deltaY );

	return Difference;
}

float Point :: x()  const{

	return xCoordinate;
}


float Point :: y()  const{

	return yCoordinate;
}


float Point :: radiusVector()  const{

	Point origin( 0.0 , 0.0 );

	return this->distance( origin );
}

Some Issues


Operator Overload

Version A Inside Class
class Point {

	public:
		Point operator + ( const Point& a )  const;
};


Point Point :: operator + ( const Point& a ) const {

	Point Sum( xCoordinate + a.xCoordinate , 
		   yCoordinate + a.yCoordinate );

	return Sum;
}

main() {
	Point A( 2.0, 3.0 );
	Point B( 1.0, 3.3 );
	Point Sum( 0, 0 );

	Sum = A + B;
}


Version B Outside Class

class Point {

	public:
};

Point  operator + ( const Point& a, const Point& b ) {

	Point Sum( a.x() + b.x() , 
		   a.y() + b.y() );

	return Sum;
}

main() {
	Point A( 2.0, 3.0 );
	Point B( 1.0, 3.3 );
	Point Sum( 0, 0 );

	Sum = A + B;
}

Copy and Memory Leaks

Version A
class Point {

	public:
		Point operator + ( const Point a ) const;
};


Point Point :: operator + ( const Point a ) const {// Object copied

	Point Sum( xCoordinate + a.xCoordinate , 
		   yCoordinate + a.yCoordinate );

	return Sum;		// Object is copied here
}

main() {
	Point A( 2.0, 3.0 );
	Point B( 1.0, 3.3 );
	Point Sum( 0, 0 );

	Sum = A + B;			// Copied again here
}
Version B
class Point {

	public:
		Point operator + ( const Point& a ) const;
};


Point Point :: operator + ( const Point& a ) const {

	Point Sum( xCoordinate + a.xCoordinate , 
		   yCoordinate + a.yCoordinate );

	return Sum;		// Object is copied here
}

main() {
	Point A( 2.0, 3.0 );
	Point B( 1.0, 3.3 );
	Point Sum( 0, 0 );

	Sum = A + B;			// Copied again here
}
Version C
class Point {

	public:
		Point& operator + ( const Point& a) const;
};


Point& Point :: operator + ( const Point& a ) const {

	Point* Sum = new Point( xCoordinate + a.xCoordinate , 
				yCoordinate + a.yCoordinate );

	return *Sum;
}

main( ) {
	Point A( 2.0, 3.0 );
	Point B( 1.0, 3.3 );
	Point Sum( 0, 0 );

	Sum = A + B;			// Copied again here
					// Memory Leak
}