"Write a class definition named rectangle to represent a rectangle. The class contains
1. Two private double fields named width and height that specify the width and height of rectangle.
2. A string data field named color that specifies the color of the rectangle. The default color is white.
3. A no-argument constructor that creates a default rectangle
4. A constructor that creates a rectangle with the specified width and height
5. A member function named get_area( ) that returns the area of this rectangle
6. A member function named get_perimeter( ) that returns the perimeter of this rectangle
7. A member function named compare( rectangle another) that compares area of this rectangle with passed in rectangle and returns true if this rectangle is greater than passed in rectangle otherwise false
Pass object as argument"
I get multiple errors when running the program.
error C2535: 'rectangle::rectangle(double,double)' : member function already defined or declared
error C3867: 'rectangle::get_area': function call missing argument list; use '&rectangle::get_area' to create a pointer to member
error C3867: 'rectangle::get_perimeter': function call missing argument list; use '&rectangle::get_perimeter' to create a pointer to member
error C3867: 'rectangle::get_area': function call missing argument list; use '&rectangle::get_area' to create a pointer to member
error C3867: 'rectangle::get_perimeter': function call missing argument list; use '&rectangle::get_perimeter' to create a pointer to member
I don't quite understand the errors.
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
|
#include <iostream>
#include <string>
using namespace std;
class rectangle
{
private:
double width;
double height;
string color;
public:
rectangle(double w, double h)
{
width = 0;
height = 0;
}
rectangle( double w, double h)
{
height = h;
width= w;
color = "white";
}
double set_width(double w)
{
width = w;
}
double set_height(double h)
{
height = h;
}
double get_area()
{
return width * height;
}
double get_perimeter()
{
return 2 * (width + height);
}
void compare(rectangle r1, rectangle r2)
{
if (r2.get_area() > r1.get_area())
{
cout << "Rectangle 2 area is greater than Rectangle 1.";
}
else if ((r2.get_area() < r1.get_area()))
{
cout << "Rectangle 1 area ia greater than Rectangle 2.";
}
}
};
int main()
{
rectangle r1();
rectangle r2(78.5, 23.4);
r1().set_width(25.7);
r1().set_height(37.2);
r2.compare (r1(), r2);
cout << "The area of rectangle 1 is: " <<r1().get_area << endl;
cout << "The perimeter of rectangle 1 is: " <<r1().get_perimeter << endl;
cout << "The area of rectangle 2 is: " << r2.get_area << endl;
cout << "The perimteter of rectangle 2 is: " << r2.get_perimeter << endl;
return 0;
}
|