Triangle.cpp

My instructor has made a strong emphasis on not allowing others to make major changes to our program while using classes. Such as make changes unnecessarily to sides of a triangle. I am not completely following, but can somebody take a look at my triangle.h file and see if I'm on the right track?

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
  
class Triangle
{
public:
	// Constructor method prototypes
	Triangle();
	Triangle(const double SideA, const double SideB, const double SideC);

	// Access method prototypes

	double getSideA() const;
	double getSideB() const;
	double getSideC() const;

	//void setSideA(const double x);
	//void setSideB(const double y);
	//void setSideC(const double z);

	//void move(const int x, const int y);

	
	
	bool isRightTriangle() const;
	bool isEquilateralTriangle() const;
	bool isIsoscelesTriangle() const;
	float TriangleArea() const;
	bool isTriangle() const;


private:
	double m_x, m_y, m_z;

};
float TriangleArea() const;
You use doubles throughout your code, but then decide to use the less precise float here. Why is that?
I think what your instructor was referring to were good encapsulation practices. When you're writing a class, generally the end result is a library or header file for others to use. You shouldn't provide any executable code for others to tamper with - you're simply writing an interface with which they may use your class.

Your code looks good. I'd generally avoid mixing floats and doubles. Pick one and stay consistent. I'm thinking that maybe 'isValid' would be a better name for your 'isTriangle' member function - the impulse is to say that we know it's a triangle by virtue of the fact that it belongs to a Triangle object, when of course what you really meant was whether the triangle is valid or not.
Thanks for the feedback.

I am making another post. It includes the triangle.h, triangle.cpp, and main.cpp.

I am not doing well in this class and need to score at least 90% on all the homework assignments moving forward. This assignment is worth 20 points, so I'll need at least 18 points of 20.

Let me know if you feel it's worth 90%
...I think that depends on what your class does....
because...my class acts as a containter for many function that changes program...(maybe)
Let me know if you feel it's worth 90%


Well, I'm no teacher and don't plan to be, but the biggest flaw in your code's interface is the presence of isTriangle().
A Triangle object should always be a triangle. There should never be instances of "Triangle" that aren't valid triangles.

When you construct a new Triangle, simply fail if the input is invalid.

If you fixed that, I'd give you a better score.
You could round it out by defaulting your default constructor, and marking the appropriate functions noexcept.

Since Triangle satisfies LiteralType, mark the constructor and accessors constexpr.
http://en.cppreference.com/w/cpp/concept/LiteralType

You can also get rid of the trivial accessors as per the C++ Core Guidelines:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c131-avoid-trivial-getters-and-setters

I'll admit, I'm not entirely sold on the last point. This should be a judgement call for you: Is Triangle's implementation likely to change in the future?
Topic archived. No new replies allowed.