| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Rectangle Issues
Im doing a project that requires me to use a "test file" that reads from my .h and .cpp. The output of the test file should return a success message. THis is what I've got, and what it is returning. (SEE BELOW)
Important!!! I am forbidden from changing the test file in any way. I am also not supposed to set the values for w and h. I've done this just to test in this example. Also. The "string trash" at the bottom does not work and issues a message that it is not part of the class. Using MS Visual C++ 6.0 TEST FILE #include "Rectangle.h" #include "Rectangle.h" // make sure double inclusion is protected #include <iostream> #include <math.h> #include <conio.h> using namespace std; //------------------------------------------------------ // Compares two double values for approximate equality. This is necessary // because two double values may not be exactly equal because of rounding errors. // // Input: Two double values // Returns: true if the two values are within the tolerance value, false if not. //------------------------------------------------------ bool compare (float x1, float x2) { float tolerance = 0.0005; bool result = (fabs(x1 - x2) <= tolerance); return result; } //------------------------------------------------------ // main function //------------------------------------------------------ int main() { bool error = false; // have we found an error yet? // check default rectangle Rectangle r1; float w = r1.getWidth(); float h = r1.getHeight(); cout << "Your default values are:" << "\n\twidth = " << w << "\n\theight = " << h << endl; if (compare(w, h)) cout << "WARNING: Your default width and height values are equal.\n" << "This is not wrong, but it may cause you trouble in a later assignment.\n"; // try changing the values r1.setWidth(9.6); r1.setHeight(3.7); Rectangle r7 = r1; r7.setWidth(-4); r7.setHeight(-4); if (!compare(r1.getWidth(), 9.6) || !compare(r7.getWidth(), 9.6)) { cout << "ERROR: Your set and/or get function for Width does not work.\n"; error = true; } if (!compare(r1.getHeight(), 3.7) || !compare(r7.getHeight(), 3.7)) { cout << "ERROR: Your set and/or get function for Height does not work.\n"; error = true; } if (!compare(r1.getArea(), 35.52)) { cout << "ERROR: Your getArea function does not work.\n"; error = true; } if (!compare(r1.getPerimeter(), 26.6)) { cout << "ERROR: Your getPerimeter function does not work.\n"; error = true; } // check explicit rectangle Rectangle r2(12.5, 4.7); Rectangle r8(-4.5, 3.7); Rectangle r9(4.5, -3.7); if (!compare (r2.getHeight(), 4.7) || !compare (r8.getHeight(), 3.7) || !compare (r9.getHeight(), 0.0)) { cout << "ERROR: Your constructor does not properly set the height.\n"; error = true; } if (!compare (r2.getWidth(), 12.5) || !compare (r8.getWidth(), 0.0) || !compare (r9.getWidth(), 4.5)) { cout << "ERROR: Your constructor does not properly set the width.\n"; error = true; } Rectangle r3, r4, r5, r6; if (Rectangle::getCount() != 8) { cout << "ERROR: Your instantiation counter does not work.\n"; error = true; } if (!error) { cout << "Your Rectangle class appears to have no errors. Congratulations!\n"; } //string trash; // cout << "\n\nEnter any character to exit... -> "; // cin >> trash; } HEADER FILE #include <iostream> #ifndef Rectangle_H #define Rectangle_H class Rectangle { public: Rectangle(float width, float height); //uses default width and height Rectangle(); float getWidth(); //width accessor float getHeight(); //height accessor void setWidth(float width); //width mutator. Do not allow negative values void setHeight(float height); //height mutator. Do not allow negative values float getArea(); //area = width * height float getPerimeter(); //perimiter = 2 * (width + height) static int getCount(); //gets the number of rectangles that have been instantiated float x1; float x2; private: float wid; float ht; float area; static int count; float perimeter; }; //#include Rectangle.cpp #endif IMPLEMENTATION #include <iostream> #include "Rectangle.h" Rectangle::Rectangle(float width, float height) //uses default width and height { count++; } Rectangle::Rectangle() { wid = 3; ht = 5; count++; } // getWidth returns the value in the private member width. float Rectangle::getWidth() { return wid; count++; } // getLength returns the value in the private member length. float Rectangle::getHeight() { return ht; } // setData copies the argument w to private member width and // l to private member length. void Rectangle::setWidth(float width) { wid = width; } void Rectangle::setHeight(float height) { ht = height; } // getArea multiplies the private members width and length. // The result is stored in the private member area. float Rectangle::getArea() { return area = (wid * ht); } // getArea returns the value in the private member area. float Rectangle::getPerimeter() { return perimeter = 2 * (wid + ht); } int Rectangle::getCount() { return count; } int Rectangle::count; ERRORS Your default values are: width = 3 height = 5 ERROR: Your set and/or get function for Width does not work. ERROR: Your set and/or get function for Height does not work. ERROR: Your constructor does not properly set the height. ERROR: Your constructor does not properly set the width. Press any key to continue |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Rectangle Issues |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|