Pointers

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.

Thanks!





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "Line.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>        // sqrt()

using namespace 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 <<() 
Last edited on
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.
    */
    delete this->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.
Last edited on
This looks similar to some of the stuff I've seen. Thank you for the help.
Topic archived. No new replies allowed.