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;
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; }
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.
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.
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.
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.