daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # 1) Include header files, not cpp files!
  2.  
  3. **Remove:**
  4.  
  5.         #include "Triangle.cpp"
  6.  
  7. **reason:**
  8.  
  9. cpp files are what gets compiled. They depend on header files, not the other way around.
  10. headers can depend on other headers, that happens a lot actually. but they shouldn't ever depend on a cpp file.
  11.  
  12. # 2) Wrap headers in ifndef/define blocks.
  13.  
  14. **Move:**
  15.  
  16. `#ifndef TRIANGLE_H` and `#define TRIANGLE_H` to the top of the file above everything else.
  17. Stylistically you may want to comment what the file does above the ifndef/define block instead of under
  18.  
  19. **Example:**
  20.  
  21.         // This is the triangle header file! Here is where I describe it.
  22.         #ifndef TRIANGLE_H
  23.         #define TRIANGLE_H
  24.         // TODO: Put the includes here
  25.         // TODO: Put your class here
  26.         #endif
  27.  
  28. **Reason:**
  29.  
  30. In C++ you can think of the line: `#include <iostream>` as copy-pasting the contents of the iostream file overtop of that line.
  31. So when you #include "triangle.h" in both main.cpp and triangle.cpp, you're describing what the triangle class will look like
  32. to each of those cpp files. main.cpp needs to know so it can create and use a triangle, and triangle.cpp needs to know so it can
  33. implement the functions in Triangle correctly.
  34.  
  35. If you include a file twice, that file is effectively copy-pasted twice. Normally that would be re-defining its contents, which
  36. would be a compile error. What the ifndef/define block does is say "hey, if TRIANGLE_H isn't already defined, this is the first
  37. time it's been included. So go ahead and define TRIANGLE_H, along with all the file contents. The next time triangle.h is included
  38. we'll see that TRIANGLE_H is already defined and we skip right by the contents of that header, instead of re-defining what a
  39. Trinagle is.
  40.  
  41. The reason I suggest you move it to the top, is you're including iostream, triangle.cpp (we talked about that part already) and
  42. using namespace std every time triangle.h is included. There's no need for that.
  43.  
  44. # 3) Fix the triangle class declaration
  45.  
  46. **Change:**
  47.  
  48.         class Triangle(){
  49.  
  50. to
  51.  
  52.         class Triangle {
  53.    
  54. **Reason:**
  55.  
  56. You're close! but you're confusing the syntax of declaring a constructor with the syntax of declaring a class.
  57.  
  58. # 4) Make functions you intend to use outside of Triangle public.
  59.  
  60. **Move:**
  61.  
  62. the "public:" access specifier above `double Perim();`
  63.    
  64. **Reason:**
  65.  
  66. If you want to call the methods: Perim, Area and IsRect from main.cpp (or from anywehre but the inside of the triangle class!)
  67. you'll need those methods to be public.
  68.  
  69. You might be tempted to make everything public, but it's bad form - and your instructor specifically said "encapsulate a, b, c".
  70. You can google the encapsulation design pattern to understand why. I won't get into it here.
  71.  
  72. # 5) Fill out your triangle constructor
  73.  
  74. **See the method:**
  75.  
  76.         Triangle::Triangle(int aa, int bb, int cc)
  77.  
  78. **Changes:**
  79.  
  80. - In the header file you don't give the three parameters names. This isn't an error, but it's weird.
  81. You should probably give those parameters names.
  82.  
  83. - In your other constructor (the one without parameters) you show that you know how to assign a, b, and c a value!
  84. Go ahead and assign them values in this method.
  85.  
  86. - Consider if you actually want to use `int` as the types of the parameters.
  87. You're storing the side lengths as doubles but you're providing them to the constructor as ints. This isn't an error, but it's weird.
  88.    
  89. # 6) Fix the body of the Set method.
  90.  
  91. **See the method: **
  92.        
  93.         Triangle::Set(double aa, double bb, double cc)
  94.  
  95. **Note:**
  96.    
  97. Remember a method is an exchange. You provide it with it's parameters, it does some work and it returns to you some value (unless it 'returns' void).
  98. Your method is asking for 3 doubles, and returning a bool. If you're returning a bool, you're agreeing to give back a true or false value.
  99. Here you're tring to return whatever cout << "..."; returns. That doesn't sound like a true/false value to me.
  100.  
  101.  **Changes:**
  102.  
  103. Don't cout anything from this function. Just return true if a triangle is possible, and false if it's not possible.
  104. You can cout whatever you want from where Set is being called from.
  105.  
  106.  
  107. # 7) Encapsulate a, b, c.
  108.  
  109. You've made a b and c private. That's half of encapsulation.
  110. Your instructor **probably** wants you to write a getter and setter for these variables in Triangle.
  111.  
  112. **Example:**
  113.  
  114.         double Triangle::GetA() {
  115.             return a;
  116.         }
  117.  
  118.         void Triangle::SetA(double aa) {
  119.             a = aa;
  120.         }
  121.  
  122. I'll leave it as an exersize to do the rest.
  123.  
  124. # 8) Using your Triangle in main.cpp
  125.  
  126. If you take your local variable `t`, you can call functions on it like so:
  127.  
  128.         a.SetA(1.1);
  129.         cout << a.GetA() << endl; // will print 1.1
  130.  
  131.         double theValueOfA; // local variable
  132.         theValueOfA = a.GetA(); // store the result of GetA in our new variable
  133.  
  134.         cout << theValueOfA << endl; // will also print 1.1
  135.  
  136. With that information, after you're done your calls to cin to get the user inputs, do the following:
  137.  
  138. 1) create a boolean variable to store the result of set, we'll use this later.
  139. 2) call the Set function on t, providing the 3 lengths the user inputted. Store the return of Set in our boolean.
  140. 3) Write an if statement. Check if the value of that boolean is true. If it is, print "A triangle with these parameters is possible.", otherwise print "A triangle with these parameters is NOT possible."
  141. 4) If your assignment requires you to do anything else or anything different, such as printing the result of Perim, Area and isRect, do that!
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top