I'm looking for some help with a few of these ToDo comments in this program. I can't figure out how to copy release and create the line in this program. Any help would be greatly appreciated.
#include "Line.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath> // sqrt()
usingnamespace std;
void Line::copyLine(const Line& copy) {
/* TODO (1):
* Duplicate the copy line.
*/
}// end Line()
void Line::releaseLine() {
/* TODO (2):
* Release both points and reset pointers.
*/
}// end releaseLine()
/*--------------------------------------------------------------------*/
Line::Line(const Point& p1, const Point& p2) {
/* TODO (3):
* Create a new line given the parameter points.
*/
}// end Line()
Line::Line(const Line& copy) {
copyLine(copy);
}// end Line()
Line::~Line() {
releaseLine();
}// end ~Line()
const Line& Line::operator =(const Line& rhs) {
return *this;
}// end =()
/*--------------------------------------------------------------------*/
/* Return a copy of the point */
Point Line::getP1() const {
/* TODO (7):
* Return a copy of p1.
*/
return *p1;
}// end getP1()
Point Line::getP2() const {
/* TODO (8):
* Return a copy of p2.
*/
return *p2;
}// end getP2()
/* Duplicate the parameter point */
void Line::setP1(const Point& p1) {
/* TODO (9):
* Update p1 by creating a new point based on parameter point.
* Do not leak memory.
*/
}// end setP1()
void Line::setP2(const Point& p2) {
/* TODO (10):
* Update p2 by creating a new point based on parameter point.
* Do not leak memory.
*/
}// end setP2()
/*--------------------------------------------------------------------*/
double Line::length() const {
/* TODO (11):
* Return the length of the line.
* d = sqrt(dx * dx + dy * dy)
*/
}// end length()
double Line::slope() const {
/* TODO (12):
* Return the slope of the line.
* m = dy / dx
*/
double m = 0.0;
// m = ((y2 - y1) / (x2 - x1));
return m;
}// end slope()
/*--------------------------------------------------------------------*/
string Line::toString() const {
/* TODO (13):
* Return a string formatted as follows:
* [x1, y1][x2, y2][d = n.n][m = n.n]
*/
ostringstream oss;
oss << "[" << "][" << "][" << length() << "][" << slope() << "]" << endl;
return oss.str();
}// end toString()
/*--------------------------------------------------------------------*/
ostream& operator <<(ostream& os, const Line& o) {
/* TODO (14):
* Display the object by calling its toString() method.
*/
os << o.toString();
return os;
}// end <<()
What have you tried?
Do you understand how to allocate memory to a pointer? (using new)
Do you understand how to free memory from a pointer? (using delete)
This is a guess, but it looks like your professor wants you to do something like this..?
1 2 3 4 5 6 7 8 9 10 11
/* Duplicate the parameter point */
void Line::setP1(const Point& p1) {
/* TODO (9):
* Update p1 by creating a new point based on parameter point.
* Do not leak memory.
*/
deletethis->p1;
this->p1 = new Point(p1);
// or?
//this->p1 = new Point(p1.x, p1.y); // ???
}
PS: I wholeheartedly disagree with the design here -- there's no need to use pointers here -- but that's your professor's fault, not yours :)
PLEASE USE CODE TAGS TO FORMAT YOUR CODE
This means putting [code] and [/code] around your code.