Thread: call a method from another class

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    30

    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();
    my question is about this :
    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Last edited by laserlight; 08-20-2019 at 05:38 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    30
    Ooooh yes it makes sense
    thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 09-20-2018, 12:55 AM
  2. Replies: 0
    Last Post: 09-20-2018, 12:55 AM
  3. how to call a second-level base class method
    By Farnaz in forum C# Programming
    Replies: 1
    Last Post: 07-07-2011, 10:15 AM
  4. call a method from another to a class
    By vipur in forum Tech Board
    Replies: 7
    Last Post: 11-15-2009, 08:39 AM
  5. calling a class method within different class method
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2009, 10:56 AM

Tags for this Thread