# Thread: Please Help!!!!!!!

1. ## Please Help!!!!!!!

/*To me this is like the hardest and most diffucult to understand problem that this book has ever used to explain something. The objective of this program is to show how you can combine two classes that after declared in a .hpp and .cpp file. */

/*If you could please explain to me what's going on here, you don't have to explain the whole thing just one point would be enought (I think)*/

// Begin Rect.hpp

#include <iostream>
class Point // holds x,y coordinates
{
// no constructor, use default
public:
void SetX(int x) { itsX = x; }
void SetY(int y) { itsY = y; }
int GetX()const { return itsX;}
int GetY()const { return itsY;}
private:
int itsX;
int itsY;
}; // end of Point class declaration

class Rectangle
{
public:
Rectangle (int top, int left, int bottom, int right);
~Rectangle () {}

int GetTop() const { return itsTop; }
int GetLeft() const { return itsLeft; }
int GetBottom() const { return itsBottom; }
int GetRight() const { return itsRight; }

Point GetUpperLeft() const { return itsUpperLeft; }
Point GetLowerLeft() const { return itsLowerLeft; }
Point GetUpperRight() const { return itsUpperRight; }
Point GetLowerRight() const { return itsLowerRight; }

void SetUpperLeft(Point Location) {itsUpperLeft = Location;}
void SetLowerLeft(Point Location) {itsLowerLeft = Location;}
void SetUpperRight(Point Location) {itsUpperRight = Location;}
void SetLowerRight(Point Location) {itsLowerRight = Location;}

void SetTop(int top) { itsTop = top; }
void SetLeft (int left) { itsLeft = left; }
void SetBottom (int bottom) { itsBottom = bottom; }
void SetRight (int right) { itsRight = right; }

int GetArea() const;

private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
int itsTop;
int itsLeft;
int itsBottom;
int itsRight;
};
// end Rect.hpp

// Begin rect.cpp

#include "rect.hpp"
Rectangle::Rectangle(int top, int left, int bottom, int right)
{
itsTop = top;
itsLeft = left;
itsBottom = bottom;
itsRight = right;

itsUpperLeft.SetX(left);
itsUpperLeft.SetY(top);

itsUpperRight.SetX(right);
itsUpperRight.SetY(top);

itsLowerLeft.SetX(left);
itsLowerLeft.SetY(bottom);

itsLowerRight.SetX(right);
itsLowerRight.SetY(bottom);
}

// compute area of the rectangle by finding cornerssides,
// establish width and height and then multiply
int Rectangle::GetArea() const
{
int Width = itsRight-itsLeft;
int Height = itsTop - itsBottom;
return (Width * Height);
}

int main()
{
//initialize a local Rectangle variable
Rectangle MyRectangle (100, 20, 50, 80 );

int Area = MyRectangle.GetArea();

std::cout << "Area: " << Area << "\n";
std::cout << "Upper Left X Coordinate: ";
std::cout << MyRectangle.GetUpperLeft().GetX();
return 0;
}

/*######################################*/
/*I know it's long but if some kind soul out there would be nice enough to explain to me just one point I would really appreciate it. If not just give me an example of using two classes together with a .hpp and .cpp file.*/

2. ## Please help me!!!!!!!!!

Please I need help here, just try that's all I ask for

3. the Point class describes a point in two dimensional space using two integers. Think of a chess board where each square can be located with a row and a column or an x and a y.

A rectangle can be defined by 4 points. Therefore you can think of a class Rectangle that has as data members 4 instances of class Point, one for each of it's corners. However, to construct a Rectangle out of 4 Points you would need to enter 8 ints. To make it easier on the user, the maker of Rectangle said just give me 4 ints and I will generate the Points to be used to make a Rectangle. The first int will be the top line, the second int will one side, the third will be the bottom line, and the 4th will be the other side, or whatever. Where these lines intersect define 4 sets of ints that can be used to make 4 Points. Therefore the user only needs to enter 4 ints rather than 8. They are converted into private variables and then into Points. Could you think of a better scenario to explain using one class in another, maybe. But this is what was chosen.

Moral of the story, user friendly coding is not always programmer friendly coding.

4. ## Thanks

Thanks for your help, your help is greatly appreciated.

Popular pages Recent additions