help with a program that will represent an axis-aligned right triangle in the x-y plane as a Class.

I am having trouble of exactly how "class" works.
I dont know what the difference between set and get is.
I have this code:

#include <iostream>
#include <cmath>

using namespace std;

class Point
{
private:
double px;
double py;

public:
void setX(const double x);
void setY(const double y);
double getX() const;
double getY() const;
};

class Triangle
{
private:
Point blPoint;
double length, height;

public:
// member functions
void setBottomLeftX(const double x);
void setBottomLeftY(const double y);
void setLength(const double inLength);
void setHeight(const double inHeight);

Point getBottomLeft() const;
Point getBottomRight() const;
Point getTopLeft() const;
double getLength() const;
double getHeight() const;

double perimeter() const;
double hypotenuse() const;
void scaleLength(const double sx);
void scaleHeight(const double sy);
void display() const;
};

// FUNCTION PROTOTYPES GO HERE:
double read_triangle(Triangle & tri);

int main()
{
// Define local variables
Triangle tri;
double sx, sy;

//Prompt the user for triangle information and fill Class Triangle object, tri,
//with this information
read_triangle(tri);

// Display triangle information
tri.display();

// Prompt and read scale factors to change length and height
cout << "Enter scale factor in x direction: ";
cin >> sx;

cout << "Enter scale factor in y direction: ";
cin >> sy;

// Apply scale factors
tri.scaleLength(sx);
tri.scaleHeight(sy);

// Display triangle information
tri.display();

return 0;
}

// FUNCTION DEFINITIONS GO HERE:

// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:

void Point::setX(const double x)
{
px = x;
}

void Point::setY(const double y)
{
py = y;
}

double Point::getX() const
{
return (px);
}

double Point::getY() const
{
return (py);
}

void Triangle::setBottomLeftX(const double x)
{
/* INSERT YOUR CODE */
blPoint.px = x; <-- this part i do not understand!!
}


void Triangle::setBottomLeftY(const double y)
{
/* INSERT YOUR CODE */
blPoint.py = y;
}

void Triangle::setLength(const double inLength)
{
/* INSERT YOUR CODE */

}

void Triangle::setHeight(const double inHeight)
{
/* INSERT YOUR CODE */

}

Point Triangle::getBottomLeft() const
{
/* INSERT YOUR CODE */
return (blPoint.px);
return (blPoint.py);
}

Point Triangle::getBottomRight() const
{
/* INSERT YOUR CODE */

}

Point Triangle::getTopLeft() const
{
/* INSERT YOUR CODE */
}

double Triangle::getLength() const
{
/* INSERT YOUR CODE */
}

double Triangle::getHeight() const
{
/* INSERT YOUR CODE */
}

double Triangle::hypotenuse() const
{
/* INSERT YOUR CODE */
}

double Triangle::perimeter() const
{
/* INSERT YOUR CODE */
}

void Triangle::scaleLength(const double scalefact)
{
/* INSERT YOUR CODE */
}

void Triangle::scaleHeight(const double scalefact)
{
/* INSERT YOUR CODE */
}

void Triangle::display() const
{
/* INSERT YOUR CODE */
}

double read_triangle(Triangle & tri)
{
/* INSERT YOUR CODE */
}



I know that i have alot of code yet to go I just do not understand exactly how to get void Triangle::setBottomLeftX(const double x) to work.

Implement the get and set member functions for the class Triangle. Use the appropriate class
attributes of the class Triangle.
a. The location of the bottom left vertex is stored in the member attribute blPoint.
b. The top left vertex can be computed from blPoint and the height.
c. The bottom right vertex can be computed from blPoint and the length.
Last edited on
A "set" function is one which takes an argument, and copies the value of that argument into the class's appropriate private data member.

A "get" function is one which finds the value of a data member of the class, and returns it to the calling code.
void Triangle::setBottomLeftX(const double x)
{
/* INSERT YOUR CODE */
blPoint.px = x;
}

so here when it says The location of the bottom left vertex is stored in the member attribute blPoint. what exactly is it saying?
Your Triangle class has an attribute (also known as a data member) called blPoint.

That should be used to store the coordinates of the bottom-left vertex of the triangle.

Since data members are private (unless there's a VERY good reason for them not to be), other code can't simply set the value of that attribute. So your class has to supply a method that other code can call to set the value. That method is setBottomLeftX.

When you post code here, please use the code tags, to make it more readable.
So when doing this I blPoint.px = x; and it says that px is private.

My main question would be what exactly do I put in this line? i don't fully understand how to call it..

Thanks for all your explanation so far you have been a huge help.

Would it be blPoint.setX(x); ?
After doing a bunch of reading this seems to be the way to do it, but I could be wrong?
Last edited on
You're getting the idea. Each class keeps its own data (i.e. attributes) private, and provides a public interface (public methods) for other classes to call.

So Point keeps its px and py members private, so that Triangle can't set them directly (and nor can any other class). Instead, Point should have get* and set* methods to allow other classes to set and retrieve the values. Methods in Triangle will then call these methods, rather than trying to directly access the attributes.

Similarly, Triangle's attributes are private, and other code can't directly access them. Triangle will have get* and set* methods of its own.

This is my new code:

#include <iostream>
#include <cmath>

using namespace std;

class Point
{
private:
double px;
double py;

public:
void setX(const double x);
void setY(const double y);
double getX() const;
double getY() const;
};

class Triangle
{
private:
Point blPoint;
double length, height;

public:
// member functions
void setBottomLeftX(const double x);
void setBottomLeftY(const double y);
void setLength(const double inLength);
void setHeight(const double inHeight);

Point getBottomLeft() const;
Point getBottomRight() const;
Point getTopLeft() const;
double getLength() const;
double getHeight() const;

double perimeter() const;
double hypotenuse() const;
void scaleLength(const double sx);
void scaleHeight(const double sy);
void display() const;
};

// FUNCTION PROTOTYPES GO HERE:
double read_triangle(Triangle & tri);

int main()
{
// Define local variables
Triangle tri;
double sx, sy;

//Prompt the user for triangle information and fill Class Triangle object, tri,
//with this information
read_triangle(tri);

// Display triangle information
tri.display();

// Prompt and read scale factors to change length and height
cout << "Enter scale factor in x direction: ";
cin >> sx;

cout << "Enter scale factor in y direction: ";
cin >> sy;

// Apply scale factors
tri.scaleLength(sx);
tri.scaleHeight(sy);

// Display triangle information
tri.display();

return 0;
}

// FUNCTION DEFINITIONS GO HERE:

// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:

void Point::setX(const double x)
{
px = x;
}

void Point::setY(const double y)
{
py = y;
}

double Point::getX() const
{
return (px);
}

double Point::getY() const
{
return (py);
}

void Triangle::setBottomLeftX(const double x)
{
/* INSERT YOUR CODE */
blPoint.setX(x);
}

void Triangle::setBottomLeftY(const double y)
{
/* INSERT YOUR CODE */
blPoint.setY(y);
}

void Triangle::setLength(const double inLength)
{
/* INSERT YOUR CODE */
length=inLength;
}

void Triangle::setHeight(const double inHeight)
{
/* INSERT YOUR CODE */
height=inHeight;
}

Point Triangle::getBottomLeft() const
{
/* INSERT YOUR CODE */
return blPoint;
}

Point Triangle::getBottomRight() const
{
/* INSERT YOUR CODE */
return (blPoint + length); <-- error here
}

Point Triangle::getTopLeft() const
{
/* INSERT YOUR CODE */
return (blPoint + height); <-- error here
}

double Triangle::getLength() const
{
/* INSERT YOUR CODE */
return (length);
}

double Triangle::getHeight() const
{
/* INSERT YOUR CODE */
return (height);
}

double Triangle::hypotenuse() const
{
/* INSERT YOUR CODE */
return (sqrt((height * height)+(length * length)));
}

double Triangle::perimeter() const
{
/* INSERT YOUR CODE */
return ((sqrt((height * height)+(length * length)))+ height + length);
}

void Triangle::scaleLength(const double scalefact)
{
/* INSERT YOUR CODE */

length = scalefact * length;
}

void Triangle::scaleHeight(const double scalefact)
{
/* INSERT YOUR CODE */
height = scalefact * height;
}

void Triangle::display() const
{
/* INSERT YOUR CODE */

}

double read_triangle(Triangle & tri)
{
/* INSERT YOUR CODE */
double x, y, inLength, inHeight;
cout << "Enter bottom left x coordinate: ";
cin >> x;
tri.setBottomLeftX(x);

cout << "Enter bottom left y coordinate: ";
cin >> y ;
tri.setBottomLeftY(y);

cout << "Enter length: ";
cin >> inLength;
tri.setLength(inLength);

cout << "Enter Height: ";
cin >> inHeight;
tri.setHeight(inHeight);
}


For return (blPoint + height); , and return (blPoint + height); it is giving me an error saying that no match for 'operator+' in ... what am i doing wrong here?
For return (blPoint + height); , and return (blPoint + height); it is giving me an error saying that no match for 'operator+' in ... what am i doing wrong here?

You're trying to add the values of an integer and a Point class. Since Point is your class, the compiler doesn't know how to do this addition. If you want to perform an addition of these two types, you'll need to define your own "+" operator that does it.

Why are you trying to do this addition? It doesn't seem to have anything to do with getting the value of a data attribute, which is what your get* methods should be doing.

Edit: And please could you put your code in code tags when posting it?
Last edited on
I am trying to find the bottom right and top left points of the triangle, which would be just the bottom left point plus the length or height.
So now for these sections I have:
return (blPoint.setX(x)+ length);
And:
return (blPoint.setY(y)+ length);
But I am getting an error that says x and y are not defined in the scope and not quite sure how to fix it.

Are these the code tags you were talking about?
Point::setX(double) is a function that changes the value of Point.px and does not return any value. You cannot compute (void + double).

1
2
3
4
Point Triangle::getBottomRight() const
{
  return // What?
}

Return type is Point, not a double. Obviously, you must create a Point.

Qualifier is const. This function cannot change this triangle. It cannot change the member variables of the class. blPoint is a member variable. Therefore, you can call only member functions blPoint that are const.
Now i just dont know whats wrong when I'm outputting it...
If you could please take a look at it and point me in the right direction that'd be great!!!

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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <cmath>

using namespace std;

class Point
{
private:
double px;
double py;

public:
void setX(const double x);
void setY(const double y);
double getX() const;
double getY() const;
};

class Triangle
{
private:
Point blPoint;
double length, height;

public:
// member functions
void setBottomLeftX(const double x);
void setBottomLeftY(const double y);
void setLength(const double inLength);
void setHeight(const double inHeight);

Point getBottomLeft() const;
Point getBottomRight() const;
Point getTopLeft() const;
double getLength() const;
double getHeight() const;

double perimeter() const;
double hypotenuse() const;
void scaleLength(const double sx);
void scaleHeight(const double sy);
void display() const;
};

// FUNCTION PROTOTYPES GO HERE:
double read_triangle(Triangle & tri);

int main()
{
// Define local variables
Triangle tri;
double sx, sy;

//Prompt the user for triangle information and fill Class Triangle object, tri,
//with this information
read_triangle(tri);

// Display triangle information
tri.display();

// Prompt and read scale factors to change length and height
cout << "Enter scale factor in x direction: ";
cin >> sx;

cout << "Enter scale factor in y direction: ";
cin >> sy;

// Apply scale factors
tri.scaleLength(sx);
tri.scaleHeight(sy);

// Display triangle information
tri.display();

return 0;
}

// FUNCTION DEFINITIONS GO HERE:

// CLASS MEMBER FUNCTION DEFINITINOS GO HERE:

void Point::setX(const double x)
{
px = x;
}

void Point::setY(const double y)
{
py = y;
}

double Point::getX() const
{
return (px);
}

double Point::getY() const
{
return (py);
}

void Triangle::setBottomLeftX(const double x)
{
/* INSERT YOUR CODE */
 blPoint.setX(x);
}

void Triangle::setBottomLeftY(const double y)
{
/* INSERT YOUR CODE */
blPoint.setY(y);
}

void Triangle::setLength(const double inLength)
{
/* INSERT YOUR CODE */
length=inLength;
}

void Triangle::setHeight(const double inHeight)
{
/* INSERT YOUR CODE */
height=inHeight;
}

Point Triangle::getBottomLeft() const
{
/* INSERT YOUR CODE */
return (blPoint);
}

Point Triangle::getBottomRight() const
{
/* INSERT YOUR CODE */
//Point getBottomRight;
double mx = (blPoint.getX()+ length);
getBottomRight.setX(mx);
return(getBottomRight);
}

Point Triangle::getTopLeft() const
{
/* INSERT YOUR CODE */
//Point getTopLeft;
double my = (blPoint.getY()+ height);
getTopLeft.setY(my);
return (getTopLeft);
}

double Triangle::getLength() const
{
/* INSERT YOUR CODE */
return (length);
}

double Triangle::getHeight() const
{
/* INSERT YOUR CODE */
return (height);
}

double Triangle::hypotenuse() const
{
/* INSERT YOUR CODE */
//hypotenuse = (sqrt((height * height)+(length * length)));
return (sqrt((height * height)+(length * length)));
}

double Triangle::perimeter() const
{
/* INSERT YOUR CODE */
 //perimeter = ((sqrt((height * height)+(length * length)))+ height + length);
 return ((sqrt((height * height)+(length * length)))+ height + length);
}

void Triangle::scaleLength(const double scalefact)
{
/* INSERT YOUR CODE */

 length = scalefact * length;
}

void Triangle::scaleHeight(const double scalefact)
{
/* INSERT YOUR CODE */
height = scalefact * height;
}

void Triangle::display() const
{
/* INSERT YOUR CODE */
cout <<"---------------------------------------" << endl;
cout << "Lower Left Vertex (" << blPoint.getX() << ", " << blPoint.getY() << ')' <<endl;
cout << "Top Left Vertex (" << blPoint.getX() << ", " << getTopLeft.getY() << ')' << endl;
cout << "Bottom Right Vertex (" << getBottomRight.getX() << ", " << blPoint.getY() << ')' << endl;
cout << "Dimensions (" << getBottomRight.getX()- blPoint.getX() << ", " << getTopleft.getY() - blPoint.getY() << ')' << endl;
cout << "Hypotenuse = " << hypotenuse() << endl;
cout << "Perimeter = " << perimeter() << endl;
cout <<"---------------------------------------" << endl;
}

double read_triangle(Triangle & tri)
{
/* INSERT YOUR CODE */
double x, y, inLength, inHeight;
cout << "Enter bottom left x coordinate: ";
cin >> x;
tri.setBottomLeftX(x);

cout << "Enter bottom left y coordinate: ";
cin >> y ;
tri.setBottomLeftY(y);

cout << "Enter length: ";
cin >> inLength;
tri.setLength(inLength);

cout << "Enter Height: ";
cin >> inHeight;
tri.setHeight(inHeight);
}



Im getting an error that says it does not have a class type in lines 194, 195, and 196
Your Triangle does not have a member variable named "getTopLeft". It does have a member function named "getTopLeft()".

Do not name variables the same as functions.
so
1
2
3
4
5
6
7
8
Point Triangle::getTopLeft() const
{
/* INSERT YOUR CODE */
//Point getTopLeft;
double my = (blPoint.getY()+ height);
getTopLeft.setY(my);
return (getTopLeft);
}


In which line should i not have getTopLeft?
he
Last edited on
and you need to change this line:


Last edited on
with that change it is saying that in lines 138 and 147 there is no match for call to '(Point) () const':
and in lines 195,196 and 197 it says it does not have a class type
getBottomRight and getTopLeft are methods. You're using them as if they were variables. You're doing the same thing in lines 195 - 197 as keskiverto and C266447 corrected in other parts of your code.
Cool thanks
Last edited on
I also am having trouble with this code in the same respects as CRZ12 and arazan. what lines were changed in the end?
The problem was that he was using method names as if they were variable names when trying to call them. The changes were simply to use the correct syntax for calling a function, which can be found in any rudimentary C or C++ tutorial, such as:

http://www.cplusplus.com/doc/tutorial/functions/

Topic archived. No new replies allowed.