Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include ??? // For sqrt() function #include ??? // For stringstream class #incl

ID: 3689735 • Letter: #

Question

#include ??? // For sqrt() function
#include ??? // For stringstream class
#include ??? // For Point class declaration

//--------------------------------------------------------------------------------------------------------------
// CTOR: Point()
//
// DESCRIPTION
// Default constructor. Initializes the point to be at the origin (0, 0) and the color to black = (0, 0, 0).
//
// PSEUDOCODE
// Call init() and pass 0, 0, 0, 0, and 0 as the parameters
//--------------------------------------------------------------------------------------------------------------
Point::Point()
{
init(0, 0, 0, 0, 0);
}

//--------------------------------------------------------------------------------------------------------------
// CTOR: Point(int, int)
//
// DESCRIPTION
// Second constructor. Initializes the mX and mY data members to the input parameters pInitX and pInitY and the
// color to black = (0, 0, 0).
//
// PSEUDOCODE
// Call init() and pass pInitX, pInitY, 0, 0, and 0 as the parameters
//--------------------------------------------------------------------------------------------------------------
Point::Point(int pInitX, int pInitY)
{
init(pInitX, pInitY, 0, 0, 0);
}

//--------------------------------------------------------------------------------------------------------------
// CTOR: Point(int, int, int, int, int)
//
// DESCRIPTION
// Third constructor. Initializes the mX and mY data members to the input parameters pInitX and pInitY. Initi-
// alizes the mColor data member to pRed, pGreen, and pBlue.
//
// PSEUDOCODE
// Call init() and pass pInitX, pInitY, pInitRed, pInitGreen, and pInitBlue as the parameters
//--------------------------------------------------------------------------------------------------------------
Point::Point(int pInitX, int pInitY, int pInitRed, int pInitGreen, int pInitBlue)
{
???
}

//--------------------------------------------------------------------------------------------------------------
// CTOR: Point(int, int, Color)
//
// DESCRIPTION
// Fourth constructor. Initializes the mX and mY data members to the input parameters pInitX and pInitY, and the
// mColor data member to pInitColor.
//
// PSEUDOCODE
// Call init() and pass pInitX, pInitY, and pInitColor as the parameters
//--------------------------------------------------------------------------------------------------------------
Point::Point(int pInitX, int pInitY, Color& pInitColor)
{
???
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: distance(Point&)
//
// DESCRIPTION
// Calculates distance from this Point to pAnotherPoint.
//
// PSEUDOCODE
// deltaX <- getX() - pAnotherPoint.getX()
// deltaY <- getY() - pAnotherPoint.getY()
// Return the square root of deltaX * deltaX + deltaY * deltaY
//--------------------------------------------------------------------------------------------------------------
double Point::distance(Point& pAnotherPoint)
{
double deltaX = getX() - pAnotherPoint.getX();
double deltaY = getY() - pAnotherPoint.getY();
return sqrt(deltaX * deltaX + deltaY * deltaY);
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: getColor()
//
// DESCRIPTION
// Accessor function for the mColor data member.
//
// PSEUDOCODE
// Return mColor.
//--------------------------------------------------------------------------------------------------------------
Color Point::getColor()
{
???
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: getX()
//
// DESCRIPTION
// Accessor function for the mX data member.
//
// PSEUDOCODE
// Return mX.
//--------------------------------------------------------------------------------------------------------------
int Point::getX()
{
return mX;
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: getY()
//
// DESCRIPTION
// Accessor function for the mY data member.
//
// PSEUDOCODE
// Return mY
//--------------------------------------------------------------------------------------------------------------
int Point::getY()
{
return mY;
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: init(int, int)
//
// DESCRIPTION
// Initializes mX and mY to pInitX and pInitY.
//
// PSEUDOCODE
// Call setX() and pass pInitX as the parameter.
// Call setY() and pass pInitY as the parameter.
//--------------------------------------------------------------------------------------------------------------
void Point::init(int pInitX, int pInitY)
{
setX(pInitX);
setY(pInitY);
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: init(int, int, int, int, int)
//
// DESCRIPTION
// Initializes mX and mY to pInitX and pInitY. Initializes mColor to pInitRed, pInitGreen, and pInitBlue.
//
// PSEUDOCODE
// Create a Color object named initColor and pass pInitRed, pInitGreen, and pInitBlue as the parameters
// Call init() and pass pInitX, pInitY, and initColor as the parameters.
//--------------------------------------------------------------------------------------------------------------
void Point::init(int pInitX, int pInitY, int pInitRed, int pInitGreen, int pInitBlue)
{
???
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: init(int, int, Color)
//
// DESCRIPTION
// Initializes mX and mY to pInitX and pInitY. Initializes mColor to pInitColor.
//
// PSEUDOCODE
// Call setX() and pass pInitX as the parameter
// Call setY() and pass pInitY as the parameter
// Call setColor() and pass pInitColor as the parameter
//--------------------------------------------------------------------------------------------------------------
void Point::init(int pInitX, int pInitY, Color& pInitColor)
{
???
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: move(int, int)
//
// DESCRIPTION
// Moves the point to the new (x, y) coordinates specified by the input params.
//
// PSEUDOCODE
// Call init() passing pNewX and pNewY as the params
//--------------------------------------------------------------------------------------------------------------
void Point::move(int pNewX, int pNewY)
{
init(pNewX, pNewY);
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: setColor(Color)
//
// DESCRIPTION
// Mutator function for the mColor data member.
//
// PSEUDOCODE
// mColor <- pNewColor
//--------------------------------------------------------------------------------------------------------------
void Point::setColor(Color& pNewColor)
{
???
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: setX(int)
//
// DESCRIPTION
// Mutator function for the mX data member.
//
// PSEUDOCODE
// mX <- pNewX
//--------------------------------------------------------------------------------------------------------------
void Point::setX(int pNewX)
{
mX = pNewX;
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: setY(int)
//
// DESCRIPTION
// Mutator function for the mY data member.
//
// PSEUDOCODE
// mY <- pNewY
//--------------------------------------------------------------------------------------------------------------
void Point::setY(int pNewY)
{
mY = pNewY;
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: toString()
//
// DESCRIPTION
// Returns a string representation of the Point, e.g., if mX is 10, mY is -130, and mColor is black, then this
// function will return the string "(x, y) = (10, -130) and color = (0, 0, 0)".
//
// REMARKS
// The C++ Standard Library stringstream class is a stream class which is similar in functionality to the
// ostream class (the class of the cout object that we use to send output to the output window). The difference
// between a stringstream object and cout is that for the stringstream object, the "output" is sent to a string
// object which is encapsulated within the stringstream object. For example,
//
// stringstream sout; -- sout is a stringstream object which encapsulates a string.
// sout << fixed << setprecision(3); -- ss is configured so real numbers will be formatted in fixed notation
// with 3 digits after the decimal pt.
// sout << "Fred"; -- The string in ss now contains "Fred".
// int x = 123;
// sout << ' ' << x; -- The string in ss now contains "Fred 123".
// double y = 3.14159265828;
// sout << endl << y; -- The string in ss now contains "Fred 123 3.142".
// cout << sout.str(); -- The str() function returns the string, i.e., "Fred 123 3.142".
//
// Ref: http://cplusplus.com/reference/sstream/stringstream
//
// PSEUDOCODE
// Define a stringstream object named sout calling the default ctor
// Send to sout "(x, y) = (" followed by getX() followed by ", " followed by getY() followed by ") "
// Send to sout "and color = " followed by getColor().toString()
// Return sout.str()
//--------------------------------------------------------------------------------------------------------------
string Point::toString()
{
???
}

Explanation / Answer

Solution: See the code below:

1. Point class declaration: point.h

------------------------------------------------------------

#ifndef POINT_H
#define POINT_H

using namespace std;

class Point
{
private:
    int mX;
    int mY;
    Color mColor; //No specification of Color class given
public:
    Point(); //Default constructor
    Point::Point(int, int); //First parameterised constructor
    Point::Point(int, int, int, int, int); //Second parameterised constructor
    Point::Point(int, int, Color&); //Third parameterised constructor
    double distance(Point&); //distance between two points
    Color& getColor(); //returns color
    void setColor(Color&); //sets color
    int getX(); //returns X
    void setX(int); //sets X
    int getY(); //returns Y
    void setY(int); //sets Y
    void init(int, int); //first version of init() function
    void init(int, int, int, int, int);//second version of init() function
    void init(int, int, Color&); //third version of init() function
    void move(int, int); //moves the point to new coordinates
    string toString(); //returns string representation
};

#endif /* POINT_H */

----------------------------------------------------

2. Implementation of functions of Point class: point.cpp

------------------------------------------------------------

#include <cmath>          // For sqrt() function
#include <sstream>          // For stringstream class
#include "point.h"          // For Point class declaration

//--------------------------------------------------------------------------------------------------------------
// CTOR: Point()
//
// DESCRIPTION
// Default constructor. Initializes the point to be at the origin (0, 0) and the color to black = (0, 0, 0).
//
// PSEUDOCODE
// Call init() and pass 0, 0, 0, 0, and 0 as the parameters
//--------------------------------------------------------------------------------------------------------------
Point::Point()
{
    init(0, 0, 0, 0, 0);
}

//--------------------------------------------------------------------------------------------------------------
// CTOR: Point(int, int)
//
// DESCRIPTION
// Second constructor. Initializes the mX and mY data members to the input parameters pInitX and pInitY and the
// color to black = (0, 0, 0).
//
// PSEUDOCODE
// Call init() and pass pInitX, pInitY, 0, 0, and 0 as the parameters
//--------------------------------------------------------------------------------------------------------------
Point::Point(int pInitX, int pInitY)
{
    init(pInitX, pInitY, 0, 0, 0);
}

//--------------------------------------------------------------------------------------------------------------
// CTOR: Point(int, int, int, int, int)
//
// DESCRIPTION
// Third constructor. Initializes the mX and mY data members to the input parameters pInitX and pInitY. Initi-
// alizes the mColor data member to pRed, pGreen, and pBlue.
//
// PSEUDOCODE
// Call init() and pass pInitX, pInitY, pInitRed, pInitGreen, and pInitBlue as the parameters
//--------------------------------------------------------------------------------------------------------------
Point::Point(int pInitX, int pInitY, int pInitRed, int pInitGreen, int pInitBlue)
{
    init(pInitX, pInitY, pInitRed, pInitGreen, pInitBlue);
}

//--------------------------------------------------------------------------------------------------------------
// CTOR: Point(int, int, Color)
//
// DESCRIPTION
// Fourth constructor. Initializes the mX and mY data members to the input parameters pInitX and pInitY, and the
// mColor data member to pInitColor.
//
// PSEUDOCODE
// Call init() and pass pInitX, pInitY, and pInitColor as the parameters
//--------------------------------------------------------------------------------------------------------------
Point::Point(int pInitX, int pInitY, Color& pInitColor)
{
    init(pInitX, pInitY, pInitColor);
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: distance(Point&)
//
// DESCRIPTION
// Calculates distance from this Point to pAnotherPoint.
//
// PSEUDOCODE
// deltaX <- getX() - pAnotherPoint.getX()
// deltaY <- getY() - pAnotherPoint.getY()
// Return the square root of deltaX * deltaX + deltaY * deltaY
//--------------------------------------------------------------------------------------------------------------
double Point::distance(Point& pAnotherPoint)
{
    double deltaX = getX() - pAnotherPoint.getX();
    double deltaY = getY() - pAnotherPoint.getY();
    return sqrt(deltaX * deltaX + deltaY * deltaY);
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: getColor()
//
// DESCRIPTION
// Accessor function for the mColor data member.
//
// PSEUDOCODE
// Return mColor.
//--------------------------------------------------------------------------------------------------------------
Color Point::getColor()
{
    return mColor;
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: getX()
//
// DESCRIPTION
// Accessor function for the mX data member.
//
// PSEUDOCODE
// Return mX.
//--------------------------------------------------------------------------------------------------------------
int Point::getX()
{
    return mX;
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: getY()
//
// DESCRIPTION
// Accessor function for the mY data member.
//
// PSEUDOCODE
// Return mY
//--------------------------------------------------------------------------------------------------------------
int Point::getY()
{
    return mY;
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: init(int, int)
//
// DESCRIPTION
// Initializes mX and mY to pInitX and pInitY.
//
// PSEUDOCODE
// Call setX() and pass pInitX as the parameter.
// Call setY() and pass pInitY as the parameter.
//--------------------------------------------------------------------------------------------------------------
void Point::init(int pInitX, int pInitY)
{
    setX(pInitX);
    setY(pInitY);
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: init(int, int, int, int, int)
//
// DESCRIPTION
// Initializes mX and mY to pInitX and pInitY. Initializes mColor to pInitRed, pInitGreen, and pInitBlue.
//
// PSEUDOCODE
// Create a Color object named initColor and pass pInitRed, pInitGreen, and pInitBlue as the parameters
// Call init() and pass pInitX, pInitY, and initColor as the parameters.
//--------------------------------------------------------------------------------------------------------------
void Point::init(int pInitX, int pInitY, int pInitRed, int pInitGreen, int pInitBlue)
{
    Color initColor(pInitRed,pInitGreen,pInitBlue);
    init(pInitX,pInitY,initColor);
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: init(int, int, Color)
//
// DESCRIPTION
// Initializes mX and mY to pInitX and pInitY. Initializes mColor to pInitColor.
//
// PSEUDOCODE
// Call setX() and pass pInitX as the parameter
// Call setY() and pass pInitY as the parameter
// Call setColor() and pass pInitColor as the parameter
//--------------------------------------------------------------------------------------------------------------
void Point::init(int pInitX, int pInitY, Color& pInitColor)
{
    setX(pInitX);
    setY(pInitY);
    setColor(pInitColor);
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: move(int, int)
//
// DESCRIPTION
// Moves the point to the new (x, y) coordinates specified by the input params.
//
// PSEUDOCODE
// Call init() passing pNewX and pNewY as the params
//--------------------------------------------------------------------------------------------------------------
void Point::move(int pNewX, int pNewY)
{
    init(pNewX, pNewY);
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: setColor(Color)
//
// DESCRIPTION
// Mutator function for the mColor data member.
//
// PSEUDOCODE
// mColor <- pNewColor
//--------------------------------------------------------------------------------------------------------------
void Point::setColor(Color& pNewColor)
{
    mColor=pNewColor;
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: setX(int)
//
// DESCRIPTION
// Mutator function for the mX data member.
//
// PSEUDOCODE
// mX <- pNewX
//--------------------------------------------------------------------------------------------------------------
void Point::setX(int pNewX)
{
    mX = pNewX;
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: setY(int)
//
// DESCRIPTION
// Mutator function for the mY data member.
//
// PSEUDOCODE
// mY <- pNewY
//--------------------------------------------------------------------------------------------------------------
void Point::setY(int pNewY)
{
    mY = pNewY;
}

//--------------------------------------------------------------------------------------------------------------
// FUNCTION: toString()
//
// DESCRIPTION
// Returns a string representation of the Point, e.g., if mX is 10, mY is -130, and mColor is black, then this
// function will return the string "(x, y) = (10, -130) and color = (0, 0, 0)".
//
// REMARKS
// The C++ Standard Library stringstream class is a stream class which is similar in functionality to the
// ostream class (the class of the cout object that we use to send output to the output window). The difference
// between a stringstream object and cout is that for the stringstream object, the "output" is sent to a string
// object which is encapsulated within the stringstream object. For example,
//
//     stringstream sout;                 -- sout is a stringstream object which encapsulates a string.
//     sout << fixed << setprecision(3); -- ss is configured so real numbers will be formatted in fixed notation
//                                           with 3 digits after the decimal pt.
//     sout << "Fred";                    -- The string in ss now contains "Fred".
//     int x = 123;
//     sout << ' ' << x;                  -- The string in ss now contains "Fred 123".
//     double y = 3.14159265828;
//     sout << endl << y;                 -- The string in ss now contains "Fred 123 3.142".
//     cout << sout.str();                -- The str() function returns the string, i.e., "Fred 123 3.142".
//
// Ref: http://cplusplus.com/reference/sstream/stringstream
//
// PSEUDOCODE
// Define a stringstream object named sout calling the default ctor
// Send to sout "(x, y) = (" followed by getX() followed by ", " followed by getY() followed by ") "
// Send to sout "and color = " followed by getColor().toString()
// Return sout.str()
//--------------------------------------------------------------------------------------------------------------
string Point::toString()
{
    stringstream sout;
    sout << "(x, y) = (" << getX() << ", " << getY() << ") ";
    sout << "and color = " << getColor().toString();
    return sout.str();  
}

-----------------------------------------------------

Note: No specification of Color class is given.