# Thread: call a method from another class

1. ## call a method from another class

Hi all,

i have difficulty understanding the following :
i have two classes : Point and Rectangle.

Code:
```class Point // holds x,y coordinates
{

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

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

~Rectangle () {}

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

private:
Point itsUpperLeft;
Point itsUpperRight;
Point itsLowerLeft;
Point itsLowerRight;
};

int main()
{
//initialize a local Rectangle variable
Rectangle MyRectangle (100, 20, 50, 80 );
std::cout << MyRectangle.GetUpperLeft().GetX();```
MyRectangle.GetUpperLeft().GetX()

i know that an object of Point can access a public method of Point.
But here i have a function (method of Rectangle class) from type Point that return a Point object. that's fine...why is this method able to call a method from the class Point ?

so a public method of the class X cannot only be accessed by an X object but also from a function of type X that can return an X object? 2. It might be easier to think of it in this way, that this:
Code:
`std::cout << MyRectangle.GetUpperLeft().GetX();`
is equivalent to:
Code:
```Point temp = MyRectangle.GetUpperLeft();
std::cout << temp.GetX();```
Now you can see that "why is this method able to call a method from the class Point ?" doesn't make sense: GetUpperLeft doesn't call GetX. 3. Ooooh yes it makes sense thanks a lot Popular pages Recent additions #### Tags for this Thread

int, point, rectangle, return, void 