I'm new to C++ and have a problem here. My code is below, I need to finish my two 'isRectangle' and 'isSquare' statements, to determine whether or not the quadrilateral is a rectangle or square. Here is what I have, but it keeps giving me errors and doesn't work properly. How do I make this work correctly? Thank you!
HEADER FILE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#pragma once
class Quadralateral
{
public:
Quadralateral(int=0,int=0,int=1,int=0,int=1,int=1,int=0,int=1);
bool isRectangle();
bool isSquare();
~Quadralateral(void);
private:
int X1,Y1,X2,Y2,X3,Y3,X4,Y4;
};
#include "Quadralateral.h"
Quadralateral::Quadralateral(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
X1 = x1; // memberwise assignment
Y1 = y1;
X2 = x2;
Y2 = y2;
X3 = x3;
Y3 = y3;
X4 = x4;
Y4 = y4;
}
bool Quadralateral::isRectangle() // HELP ON THIS FUNCTION
{
int s1; // top
int s2; // left
int s3; // right
int s4; // bottom
if (X1 != X3)
returnfalse;
if (Y1 != Y2)
returnfalse;
s1 = X2 - X1;
s4 = X4 - X3;
if (s1 != s4)
returnfalse;
s2 = X1 - X3;
s3 = X2 - X4;
if (s2 != s3)
returnfalse;
if ((s2 = s3) && (s1 = s4))
returntrue;
// All tests for a rectangle passed
}
bool Quadralateral::isSquare() // HELP ON THIS FUNCTION
{
int s1; // top
int s2; // left
int s3; // right
int s4; // bottom
if (X1 != X3)
returnfalse;
if (Y1 != Y2)
returnfalse;
s1 = X2 - X1;
s4 = X4 - X3;
if (s1 != s4)
returnfalse;
s2 = X1 - X3;
s3 = X2 - X4;
if (s2 != s3)
returnfalse;
if (s2 = s3 = s1 = s4)
returntrue;
// Test for square passed
}
Quadralateral::~Quadralateral(void)
{
}
you would need to add #include <iostream> before int main().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int main()
{
// you need to instantiate the class
Quadralateral q(2,2,2,2,2,2);
//checks for the return value of the method.
if(q.isRectangle())
{
// this code outputs to the console
std::cout << "is a rectangle" << std::endl;
}
else
{
std::cout << "is not a rectangle" << std::endl;
}
}
oh! One more thing, It just keeps saying that is isn't a rectangle though, no matter what I put in. What numbers would i put in to check if it is working right for a rectangle?
Points that represent a valid rectangle? If you are incapable of coming up with a valid set of points, you don't have much chance of getting the code right.