I'm stuck

Hi I'm just trying to understand some lines of code in my c++ book from Sam's 24 hour. It has to do with classes and objects. I don't understand what these lines accomplish:

void Rectangle::setUpperLeft(Point location)
{
upperLeft = location;
upperRight.setY(location.getY());
lowerLeft.setX(location.getX());
top = location.getY();
left = location.getX();

is location an object in this? I thought the period comes after an object name but I don't see where the object is declared in this program.

#include "Rectangle.hpp"

Rectangle::Rectangle(int newTop, int newLeft, int newBottom, int newRight)
{
top = newTop;
left = newLeft;
bottom = newBottom;
right = newRight;

upperLeft.setX(left);
upperLeft.setY(top);

upperRight.setX(right);
upperRight.setY(top);

lowerLeft.setX(left);
lowerLeft.setY(bottom);

lowerRight.setX(right);
lowerRight.setY(bottom);
}

void Rectangle::setUpperLeft(Point location)
{
upperLeft = location;
upperRight.setY(location.getY());
lowerLeft.setX(location.getX());
top = location.getY();
left = location.getX();
}

void Rectangle::setLowerLeft(Point location)
{
lowerLeft = location;
lowerRight.setY(location.getY());
upperLeft.setX(location.getX());
bottom = location.getY();
left = location.getX();
}

void Rectangle::setLowerRight(Point location)
{
lowerRight = location;
lowerLeft.setY(location.getY());
upperRight.setX(location.getX());
bottom = location.getY();
right = location.getX();
}

void Rectangle::setUpperRight(Point location)
{
upperRight = location;
upperLeft.setY(location.getY());
lowerRight.setX(location.getX());
top = location.getY();
right = location.getX();
}

void Rectangle::setTop(int newTop)
{
top = newTop;
upperLeft.setY(top);
upperRight.setY(top);
}

void Rectangle::setLeft(int newLeft)
{
left = newLeft;
upperLeft.setX(left);
lowerLeft.setX(left);
}

void Rectangle::setBottom(int newBottom)
{
bottom = newBottom;
lowerLeft.setY(bottom);
lowerRight.setY(bottom);
}

void Rectangle::setRight(int newRight)
{
right = newRight;
upperRight.setX(right);
lowerRight.setX(right);
}

int Rectangle::getArea() const
{
int width = right - left;
int height = top - bottom;
return (width * height);
}

// compute area of the rectangle by finding corners,
// establish width and height and then multiply
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() << "\n";
return 0;
}


Here's the header program:

#include <iostream>

class Point
{
// no constructor, use default
public:
void setX(int newX) { x = newX; }
void setY(int newY) { y = newY; }
int getX() const { return x;}
int getY() const { return y;}
private:
int x;
int y;
};

class Rectangle
{
public:
Rectangle(int newTop, int newLeft, int newBottom, int newRight);
~Rectangle() {}

int getTop() const { return top; }
int getLeft() const { return left; }
int getBottom() const { return bottom; }
int getRight() const { return right; }

Point getUpperLeft() const { return upperLeft; }
Point getLowerLeft() const { return lowerLeft; }
Point getUpperRight() const { return upperRight; }
Point getLowerRight() const { return lowerRight; }

void setUpperLeft(Point location);
void setLowerLeft(Point location);
void setUpperRight(Point location);
void setLowerRight(Point location);

void setTop(int newTop);
void setLeft (int newLeft);
void setBottom (int newBottom);
void setRight (int newRight);

int getArea() const;

private:
Point upperLeft;
Point upperRight;
Point lowerLeft;
Point lowerRight;
int top;
int left;
int bottom;
int right;
};
closed account (48T7M4Gy)
loaction is an object which is an instance of a Point as described in the Point class.
closed account (48T7M4Gy)
A Rectangle object, in this case myRectangle, is made up of 4 Points, each Point having an x coordinate and a y coordinate.

A Rectangle has the ability (method) to set the coordinates of the various corners by specifying the corner Points and a Point similarly is able to get or set its x and y coordinates.

So for the upper right corner of myRectangle, if you have a Point variable called location you set the upper right corner using the setUpperRight() method.

ie myRectangle.setUpperRight(location);

This will set the private member variables top and right and upperRight of myRectangle. Each Rectangle you declare has it's own set of private members.

You will find it helpful to to write/extend the driver program, ie int main() etc and test the ideas/questions also. Reading the classes without trying them out is difficult sometimes. Sams book website probably has a driver.
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) To answer your specific question:

is location an object in this? I thought the period comes after an object name but I don't see where the object is declared in this program.


location is the argument to the setUpperLeft method, defined as a Point object:

1
2
3
4
5
6
void Rectangle::setUpperLeft(Point location)
//                           ^^^^^^^^^^^^^^
//                           definition here
{
  // ...
}
Last edited on
Thank you all for your replies. So location is declared as an object of the Point class while it is an argument of the setUpperLeft function? But then in the definition aren't they treating location as a variable and setting it equal to upperLeft? Thanks

1
2
3
4
5
6
7
8
void Rectangle::setUpperLeft(Point location)
{
    upperLeft = location; 
    upperRight.setY(location.getY());
    lowerLeft.setX(location.getX());
    top = location.getY();
    left = location.getX();
}
I'm not sure what you're confused about. location behaves here just as any argument passed into a function. If the variables concerned were integers, it would be the same, e.g.

void MyClass::setMyInt(int value)
{
myInt = value;
}

You've seen functions before, right? You've seen arguments being passed into functions, and being used inside those functions?

aren't they treating location as a variable and setting it equal to upperLeft? Thanks


Look again at the code:

upperLeft = location;

Which object is having its value changed here?
location is having its value changed to upperLeft right?

I think I'm just getting bogged down with details here. I'm just trying to grasp concepts. Thanks for writing back I appreciate it.
closed account (48T7M4Gy)
A statement like upperleft = location sets the value of upperleft to the same value that location is. You are incorrectly interpreting in the reverse of the real situation.

eg
1
2
3
int xx = 77;
int yy = 21; // yy is 77
yy = xx; // yy is now 77 
location is having its value changed to upperLeft right?

No. I don't mean to sound disparaging, but assignment is one of the most basic operations in C++. If you haven't grasped that, you need to go right back to the start and make sure you understand the basics.

Consider the following statement:

a = 5;

What is having its value changed here?

Now compare with:

upperLeft = location;

Last edited on
closed account (48T7M4Gy)
I think I'm just getting bogged down with details here


I don't think you are, and besides you aren't forced to attend. The problem you might be suffering is a prevalent and virulent infection called Sams. It only lasts for 24 hours and unless you are fully conversant with C++ which Sams publications require, the only vaccine is getting a decent book to learn as a beginner.

Unfortunately, most of us here have been sucked in by Sams so we know the trauma of that - I certainly do. C++ for Dummies is better if you reckon that's the way to approach this subject and, believe me, I understand why you might. Good luck with it whatever you decide.

https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
Last edited on
Topic archived. No new replies allowed.