Hi guys,


Im pretty dam stuck at the moment on an assignment Im meant to do. Its my last question so everything is pretty laid out, but the question (to me is not useful) so if you could just help me out it'd be much appreciated

Ok so what i had to do was "create" a rectangle where 4 co-ordinates were stored mapping out where, on an x, y grid this rectangle is. Then i had to make a way for the user to see where on the grid they are by allowing them to see the current x, y co-ordinate.

Finally they had to be able to move the co-ordinate "absolute" and "relative" in order to be able to figure out where this rectangle lies.

Down below is the code for this and works 100%. It should be fairly easy to understand.

// Point2D_OO.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Point2D.h"

//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
	Point2D aPoint;
	int menu = 0;

	while ( menu != 4 )
	{
		system("cls");

		if (aPoint.contains(8, 6, 4, 2) == 1)
		{
			cout << endl << "You are currently inside the rectangle" << endl << endl;
		}
		else if ( aPoint.contains(8, 6, 4, 2) == 2)
		{
			cout << endl << "X is currently inside the rectangle" << endl << endl;
		}
		else if ( aPoint.contains(8, 6, 4, 2) == 3)
		{
			cout << endl << "Y is currently inside the rectangle" << endl << endl;
		}
		else
		{
			cout << endl << "You are not currently inside the rectangle" << endl << endl;
		}

		cout << "======Menu======" << endl;
		cout << "1. Display X and Y co ordinates" << endl;
		cout << "2. Move to a co-ordinate" << endl;
		cout << "3. Increment or decrement the X or Y co-ordinate" << endl;
		cout << "4. Exit" << endl;

		cin >> menu;

		if ( menu == 1 )
		{
			cout << endl << "-----" << endl;
			cout <<"x = " << aPoint.printX() << endl;
			cout << "y = " << aPoint.printY() << endl;
			getch();
		}
		else if ( menu == 2 )
		{
			aPoint.moveAbsolute();
		}
		else if ( menu == 3 )
		{
			aPoint.moveRelative();
		}
	}
    return 0;
}

//---------------------------------------------------------------------------

now that works how ever what i have to do now is create 2 more classes and move the original one along with these 2 into a header file which is fairly easy and i have done
and the rest of the instructions are as follows:

The previous question is a messy mixture of OO and non-OO code. Tidy up the cody by:
1) implement class Offset and use an offset object as an argument to the member function MoveRelative(). An offset object has two values that are added to the x and y vlaue of a point.
2) Implement class aRectangle and use an aRectangle object as an arguement to the contains method

So I've figured that "2" means that where

if (aPoint.contains(8, 6, 4, 2) == 1)
		{
			cout << endl << "You are currently inside the rectangle" << endl << endl;
		}
		else if ( aPoint.contains(8, 6, 4, 2) == 2)
		{
			cout << endl << "X is currently inside the rectangle" << endl << endl;
		}
		else if ( aPoint.contains(8, 6, 4, 2) == 3)
		{
			cout << endl << "Y is currently inside the rectangle" << endl << endl;
		}
		else
		{
			cout << endl << "You are not currently inside the rectangle" << endl << endl;
		}

is placed something of similar value should be placed in the rectangle class however I am unsure of how this should be done and it might behelpful if some one can point out a path to me.

as for "1" I got NO effing clue on what is going on there.

for simplisity sakes we can use the .h file which i have created for the class to minimize code if you need to change anything in the cpp file just identify the change maybe in bold

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

class Point2D{
	public:
	Point2D() {x = 0; y = 0;}
	int printX() {   return x;   }
	int printY() {   return y;   }
	
	int moveRelative()
	{
		char answer;
		int newXY;

		cout << endl;
		cout << "Do you wish to modify x? (y or n): ";
		cin >> answer;
		if (answer == 'y')
		{
			cout << "Please increment or decrement the x co-ordinate: ";
			cin >> newXY;
			x = x + newXY;
		}
		else if (answer == 'n')
		{
			cout << endl;
			cout << "Do you wish to modify y? (y or n): ";
			cin >> answer;
			if (answer == 'y')
			{
				cout << "Please increment  or decrement the y co-ordinate: ";
				cin >> newXY;
				y = y + newXY;
			}
		}
		else
		{
			cout << "Unknown answer" << endl;
			cout << "Do you wish to modify x? (y or n): ";
			cin >> answer;
		}
		return 0;
	}

	int moveAbsolute()
	{
		char answer;
	
		cout << "Do you wish to move to a new starting point? (y or n): ";
		cin >> answer;

		if (answer == 'y')
		{
			cout << "Position of x: ";
			cin >> x;
			cout << "Position of y: ";
			cin >> y;
		}
		return 0;
	}

	int contains(int topX, int topY, int bottomX, int bottomY)
	{
		if (x <= topX && x >= bottomX)
		{
			if (y <= topY && y >= bottomY)
			{
				return 1;
			}
			else
			{
				return 2;
			}
		}
		else if (y <= topY && y >= bottomY)
		{
			if (x <= topX && x >= bottomX)
			{
				return 1;
			}
			else
			{
				return 3;
			}
		}
		else
		{
			return 0;
		}
	}

private:
 int x, y;
};

class Offset{


};

class aRectangle {
public:
		
private:
};

just an update i believe aRectangle should include inheritance from/for point2D's contians method

class aRectangle : public Point2D {
public:

private:
};

so i want it to gain the values of

int topX 8, topY 6, bottomX 4, bottomY 2

in order for it to be transferred into the contains method

I don't think you have to use any inheritance for the aRectangle class like your last post says.

And the way I set this up I'm not too sure why he got you to use classes for Offset and aRectangle, unless he wants you use get() and set() functions to assign values, when you could just use structures since they start off being public (opposed to classes which start out private).

Point2D.h changes/additions

class Offset
{
	public:
	Offset(int x = 0, int y = 0)
	{
		_x = x;
		_y = y;
	}
	int _x, _y;
};

class aRectangle
{
	public:
	aRectangle(int x1 = 0, int y1 = 0, int x2 = 0, int y2 = 0)
	{
		_x1 = x1;
		_y1 = y1;
		_x2 = x2;
		_y2 = y2;
	}
	int _x1, _y1, _x2, _y2;
};

//changes within the Point2D class
void moveRelative(Offset oset)
{
	x += oset._x;
	y += oset._y;
}

int contains(aRectangle rect)
{
	if (x <= rect._x1 && x >= rect._x2)
	{
		if (y <= rect._y1 && y >= rect._y2)
			return 1;
		else
			return 2;
	}
	else if (y <= rect._y1 && y >= rect._y2)
	{
		if (x <= rect._x1 && x >= rect._x2)
			return 1;
		else
			return 3;
	}
	return 0;
}

main.cpp changes/additions

int main(int argc, char* argv[]) //might look different because I use code::blocks and it doesn't like your format (just change it back)
{
	Point2D aPoint; //unchanged
	aRectangle rect(8,6,4,2); //declare a rectangle
	Offset oset; //declare an offset
	int menu = 0; //unchanged

	//....other codes....

	else if ( menu == 3 )
	{
		cout << "x offset: ";
		cin >> oset._x;
		cout << "y offset: ";
		cin >> oset._y;
		aPoint.moveRelative(oset); //pass the offset through
	}
}
commented: works :) +1

Hi,

Thanks for your post. I believe he is tryign to teach us the relations between classes, inheritance and friendships and how to make code simple and effective to debug and/or manage.

I took your code and just renamed it to sort of make it follow the names I use so i can wrap my head around. It looked fairly simple but its not registering the fact that the class as an identifier...

.h file
Point2D

void moveRelative(Offset oset)
	{
		x += oset.newX;
		y += oset.newY;
	}
	

	int contains(aRectangle rect)
	{
		if (x <= rect.xTop && x >= rect.xBottom)
		{
			if (y <= rect.yTop && y >= rect.yBottom)
			{
				return 1;
			}
			else
			{
				return 2;
			}
		}
		else if (y <= rect.yTop && y >= rect.yBottom)
		{
			if (x <= rect.xTop && x >= rect.xBottom)
			{
				return 1;
			}
			else
			{
				return 3;
			}
		}
		return 0;
	}

-- as per recommentdations
other 2 classes

class Offset
{
	public:
	Offset(int x = 0, int y = 0)
	{
		newX = x;
		newY = y;
	}
	int newX, newY;
};

class aRectangle
{
	public:
	aRectangle(int topX = 0, int topY = 0, int botX = 0, int botY = 0)
	{
		xTop = topX;
		yTop = topY;
		xBottom = botX;
		yBottom = botY;
	}
	int xTop, yTop, xBottom, yBottom;
};

and .cpp file

else if ( menu == 3 )
		{
			cout << "x offset: ";
			cin >> oset.newX;
			cout << "y offset: ";
			cin >> oset.newY;
			aPoint.moveRelative(oset);
		}

errors are:

syntax error : identifier 'Offset'
syntax error : identifier 'aRectangle'
'oset' : undeclared identifier
left of '.newX' must have class/struct/union

'Point2D::contains' : function does not take 1 arguments

all errors are based on those

For the first two errors make sure that you either have both of the classes above Point2D class or just put the class prototypes above the Point2D class

class aRectangle;
class Offset;

As for the 3rd and 4th errors (4th is caused by the 3rd) you probably do not have a Offset variable declared within main().

The previous question is a messy mixture of OO and non-OO code. Tidy up the cody by:
1) implement class Offset and use an offset object as an argument to the member function MoveRelative(). An offset object has two values that are added to the x and y vlaue of a point.
2) Implement class aRectangle and use an aRectangle object as an arguement to the contains method

To me this does not sound like he wants you to use friendship/inheritance.

hey thanks that was it... it seems error 3 and 4 are caused by 1 and 2 due to the fact that they arent registered unless declared so i initiated them up top :)

you da man!

final code is:

Header File

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

class Offset
{
	public:
	Offset(int x = 0, int y = 0)
	{
		newX = x;
		newY = y;
	}
	int newX, newY;
};

class aRectangle
{
	public:
	aRectangle(int topX = 0, int topY = 0, int botX = 0, int botY = 0)
	{
		xTop = topX;
		yTop = topY;
		xBottom = botX;
		yBottom = botY;
	}
	int xTop, yTop, xBottom, yBottom;
};

class Point2D{
	public:
	Point2D() {x = 0; y = 0;}
	int printX() {   return x;   }
	int printY() {   return y;   }
	
	int moveAbsolute()
	{
		char answer;
	
		cout << "Do you wish to move to a new starting point? (y or n): ";
		cin >> answer;

		if (answer == 'y')
		{
			cout << "Position of x: ";
			cin >> x;
			cout << "Position of y: ";
			cin >> y;
		}
		return 0;
	}

	void moveRelative(Offset oset)
	{
		x += oset.newX;
		y += oset.newY;
	}
	
	int contains(aRectangle rect)
	{
		if (x <= rect.xTop && x >= rect.xBottom)
		{
			if (y <= rect.yTop && y >= rect.yBottom)
			{
				return 1;
			}
			else
			{
				return 2;
			}
		}
		else if (y <= rect.yTop && y >= rect.yBottom)
		{
			if (x <= rect.xTop && x >= rect.xBottom)
			{
				return 1;
			}
			else
			{
				return 3;
			}
		}
		return 0;
	}

private:
 int x, y;
};

CPP File

// Point2D_OO.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Point2D.h"
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
	Point2D aPoint;
	aRectangle rect(8,6,4,2);
	Offset oset;
	int menu = 0;

	while ( menu != 4 )
	{
		system("cls");

		if (aPoint.contains(rect) == 1)
		{
			cout << endl << "You are currently inside the rectangle" << endl << endl;
		}
		else if ( aPoint.contains(rect) == 2)
		{
			cout << endl << "X is currently inside the rectangle" << endl << endl;
		}
		else if ( aPoint.contains(rect) == 3)
		{
			cout << endl << "Y is currently inside the rectangle" << endl << endl;
		}
		else
		{
			cout << endl << "You are not currently inside the rectangle" << endl << endl;
		}

		cout << "======Menu======" << endl;
		cout << "1. Display X and Y co ordinates" << endl;
		cout << "2. Move to a co-ordinate" << endl;
		cout << "3. Increment or decrement the X or Y co-ordinate" << endl;
		cout << "4. Exit" << endl;

		cin >> menu;

		if ( menu == 1 )
		{
			cout << endl << "-----" << endl;
			cout <<"x = " << aPoint.printX() << endl;
			cout << "y = " << aPoint.printY() << endl;
			getch();
		}
		else if ( menu == 2 )
		{
			aPoint.moveAbsolute();
		}
		else if ( menu == 3 )
		{
			cout << "Offset X by: ";
			cin >> oset.newX;
			cout << "Offset Y by: ";
			cin >> oset.newY;
			aPoint.moveRelative(oset);
		}
	}
    return 0;
}

//---------------------------------------------------------------------------
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.