Question: Fix my code to correctly implement a pass by reference. Should be a co
ID: 640546 • Letter: Q
Question
Question: Fix my code to correctly implement a pass by reference. Should be a couple fixes.
Three functions are below. Don't worry about linking everything together, header/cpp files and all other small details.
________________________________________________
Rectangle::Rectangle(int x,int y,int width,int height){
_x =x;
_y =y;
_width = width;
_height = height;
}
________________________________________________
bool Rectangle::contains(Rectangle const &r2){
int x0,x1,y0,y1;
x0 = r2._x;
y0 = r2._y;
x1 = r2._x + r2._length;
y1 = r2._y + r2._width;
if(x0(this._x+this._width) || x1>(this._x+this._width) ){
return false;
}
if(y0(this._y+this._length) || y1>(this._y+this._length) ){
return false;
}
return true;
}
___________________________________________________
void Test_ContainsRectangle_AllInside(void){
// This test tests functionality of bool Rectangle::contains(Rectangle)
// The rectangle passed as a parameter is inside the rectangle with no shared border
Rectangle r1(0,0,20,20);
Rectangle r2(5,5,10,10);
bool i = r1.contains(r2);
TS_ASSERT_EQUALS(i, true);
// Tests
}
__________________________________________________
Right now my error message is below:
rectangle.cpp: In member function
Explanation / Answer
The program runs correctly and gives output as '1'.
#include <iostream>
using namespace std;
class Rectangle
{
public:
int _x,_y,_width,_height;
Rectangle(int x,int y,int width,int height);
bool contains(Rectangle const &r2);
};
Rectangle::Rectangle(int x,int y,int width,int height){
_x =x;
_y =y;
_width = width;
_height = height;
}
bool Rectangle::contains(Rectangle const &r2){
int x0,x1,y0,y1;
x0 = r2._x;
y0 = r2._y;
x1 = r2._x + r2._height;
y1 = r2._y + r2._width;
if(x0<(_x) || x1>(_x+_width) ){
return false;
}
if(y0<(_y) || y1>(_y+_height) ){
return false;
}
return true;
}
void Test_ContainsRectangle_AllInside(void){
// This test tests functionality of bool Rectangle::contains(Rectangle)
// The rectangle passed as a parameter is inside the rectangle with no shared border
Rectangle r1(0,0,20,20);
Rectangle r2(5,5,10,10);
bool i = r1.contains(r2);
// TS_ASSERT_EQUALS(i, true);
cout<<i<<endl;
// Tests
}
int main()
{
Test_ContainsRectangle_AllInside();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.