Line 8-9: What is the purpose of x and y? And why are they public?
Line 15: You can't return two values like that. GetValues() can only return a single int.
Line 21: Class name should not be specified here. You have no implementation of this function.
Line 24-25: These are function declarations and do not belong here.
Line 35: Why are you declaring 4 Rectangles within this function?
Line 36-37: You're modifying the arguments, not the class members.
Line 79: rect1, rect2, rect3 and rectdefault are all local variables that go out of scope when the function exists.
Lines 45-52,56-63,68-75: These braces are unnecessary. They makes lines 44,55,67 look like function definitions, which they are not.
Lines 47,49,58,60,70,72: You're modifying the instance's x any with, not length and width.
Lines 44,55,67: You're making recursive calls to SetValues().
Line 51: You're going to return unconditionally and never get to the following code.
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
|
#include <iostream>
using namespace std;
class Rectangle
{ int width;
int length;
public:
// Got rid of x and x
// Default constructor
Rectangle ()
{ width = 1;
length = 1;
}
// Parameterized constructor
Rectangle (int w, int l)
{ width = w;
length = l;
}
void PromptValues ()
{ cout << "Enter the width;";
cin >> width;
cout << "Enter the length :";
cin >> length;
}
int GetWidth () const
{ return width;
}
int GetLength() const
{ return length;
}
int GetArea()
{ return width*length;
}
int GetPerimeter()
{ return width * 2 + length * 2;
}
};
int main()
{ Rectangle rect1; // Default constructor (1,1)
Rectangle rect2 (1,1); // Explicit constructor
Rectangle rect3;
rect3.PromptValues();
cout << rect1.GetWidth() << "," << rect1.GetLength() << endl;
cout << rect2.GetWidth() << "," << rect2.GetLength() << endl;
cout << rect3.GetWidth() << "," << rect3.GetLength() << endl;
cout << "rect1 area: " << rect1.GetArea() << endl; // Retrieves area from the member function
cout << "rect2 area: " << rect2.GetArea() << endl;
cout << "rect3 area: " << rect3.GetArea() << endl;
cout << "rect1 perimeter: " << rect1.GetPerimeter() << endl; // Retrieves perimeter from member function from class
cout << "rect2 perimeter: " << rect2.GetPerimeter() << endl;
cout << "rect3 perimeter: " << rect3.GetPerimeter() << endl;
system ("pause");
return 0;
}
|