• 11-27-2001
incognito
/*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.*/:) :) :) :) :) :) :) :)
• 11-28-2001
incognito