Thread: C++ Classes

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    C++ Classes

    Hi everyone, I am studing on classes and I found an example that I cannot figure out how it works

    Code:
    class Distance{
    private:
            int feet;
            int inch;
    public:
                 Distance (); //Constructor
            void getDist  ();
            void showDist ();
            Distance addDist( Distance d2 );
            Distance subDist( Distance d2 );
     };
    and this is the definition

    Code:
    Distance Distance:: addDist( Distance d2 )
    {
        Distance temp;
     
        temp.feet = feet + d2.feet;
        temp.inch = inch + d2.inch;
     
        if( temp.inch >= 12)
        {
            temp.feet++;
            temp.inch -= 12;        
        }
        return temp;    
    }
    I cannot understand this line inside the class: Distance addDist( Distance d2 );

    It creates an object "addDist" passing as parameter an other object "d2"?

    Also I don't understand the syntax of this
    line: Distance Distance:: addDist( Distance d2 )

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Nikosant03
    I cannot understand this line inside the class: Distance addDist( Distance d2 );

    It creates an object "addDist" passing as parameter an other object "d2"?
    No, it declares that the class named Distance has a member function named addDist that has a Distance parameter and a return type of Distance.

    Quote Originally Posted by Nikosant03
    Also I don't understand the syntax of this line: Distance Distance:: addDist( Distance d2 )
    That is the start of the definition (implementation) of the addDist member function of the Distance class.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of classes with classes inside
    By questionmark in forum C++ Programming
    Replies: 3
    Last Post: 10-04-2013, 03:39 PM
  2. Conversions between base classes and derived classes
    By tharnier in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2011, 10:50 AM
  3. Classes access other classes local variables
    By parad0x13 in forum C++ Programming
    Replies: 6
    Last Post: 01-14-2010, 04:36 AM
  4. Classes with Other Classes as Member Data
    By njd in forum C++ Programming
    Replies: 2
    Last Post: 09-27-2005, 09:30 AM
  5. Help accessing classes and derived classes
    By hobbes67 in forum C++ Programming
    Replies: 8
    Last Post: 07-14-2005, 02:46 PM

Tags for this Thread