Results 1 to 5 of 5

Thread: Can someone explain this at a dummy level for me, please? (OOP classes, objects, etc)

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    35

    Question Can someone explain this at a dummy level for me, please? (OOP classes, objects, etc)

    Hello, everyone.

    It's been a while since I don' ask questions here. I've been studying and the more I study, the more I realise I ignore a bunch of things....

    Well, now I'm focusing on OOP, and I've learned some stuff, but sometimes I get with examples I can't understand, like the one in this page: https://www.geeksforgeeks.org/g-fact-93/

    I'm going to comment parts of the code, in order to make my doubts a little clearer, but please, explain at dummy level as much as you can. Also, I know that I might be wrong on what I'm guessing about the code. My coments are the ones using /* */, the ones using // are part of the example in the webpage

    The language is C++
    Code:
    #include <iostream>
     
    using namespace std;
     
    class Complex         /* I understand a class named "Complex" is being created */
    {
    private:
        double real;       /* Two variables are being created as "private", so they aren't visible for sub classes */
        double imag;
     
    public:
        // Default constructor
        Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {} /* A constructor is created and initializes the private variables above, with the parameters received when creating an object */
     
        // A method to compare two Complex numbers
        bool operator == (Complex rhs) {             /*Now first doubt: what is "operator"? a variable? a method? why is it compared against something? what is "(Complex rhs)" what is rhs? please this part dummy level!!!!! */
           return (real == rhs.real && imag == rhs.imag)? true : false; /* I understand all this return part, but I don't know where it is returning to */
        }
    };
     
    int main()
    {
        // a Complex object
        Complex com1(3.0, 0.0); /* It´s creating an object using the constructor. real = 3.0, and imag = 0.0 */
     
        if (com1 == 3.0)  /* now, what is com1 being compared against?, I understand com1 is an object, but why compare against 3.0 if there is also a 0.0...? I really don't understand this comparison */
           cout << "Same";  /* I understand the if else statement and the cout parts */
        else
           cout << "Not Same";
         return 0;
    }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Can someone explain this at a dummy level for me, please? (OOP classes, objects,

    Operators in programming are pretty much the same as operators in mathematics. In this code:
    Code:
    x = a + c;
    The addition operator (+) is used to sum two values and the assignment operator (=) is used to assign the result to a variable. In this case, the equality operator (==) is being defined for the Complex class to enable you to compare two Complex objects to see whether they are equal. You take the equality operator for granted when it comes to things like numbers, Booleans and strings but there is no inherent method of comparing complex types for equality so the author of a class has to define the equality operator if they want to be able to compare instances of that type.

    This:
    Code:
    bool operator == (Complex rhs)
    is an operator declaration just as this:
    Code:
    bool Equals (Complex rhs)
    is a method declaration. You could define that method and use it but the operator is more natural and thus superior for a common operation like equality comparison. Just as for the method, 'rhs' is a parameter of type Complex. 'rhs' would stand for "right-hand side", i.e. the right-hand side of the mathematical expression using the operator. Where you would use the method like this:
    Code:
    if (someComplex.Equals(someOtherComplex))
    you use the operator like this:
    Code:
    if (someComplex == someOtherComplex)
    That's what's happening here:
    Code:
    if (com1 == 3.0)
    I haven't used C++ for a long time but I'm assuming that that double value 3.0 is being implicitly converted to a Complex with a real part equal to that value and an imaginary part of zero. Presumably the double value specified becomes that value of the first field and other fields take on their default value.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2016
    Posts
    35

    Re: Can someone explain this at a dummy level for me, please? (OOP classes, objects,

    Thank you very much, Sir. Your explanation is clear enough. I didn't know an operator could be defined (didn't know "operator" is a reserved word). I really appreciate your help. One last question, since I thought I had it understood, I tried the code with some tests to make sure I understood, but I found that:

    With this code I get "not same" at the output:
    Code:
       
        Complex com1(3.0, 1.0); 
        if (com1 == (3.0, 1.0));...
    And by doing this, then I get "same":
    Code:
       
        Complex com1(3.0, 1.0); 
        if (com1 == Complex(3.0, 1.0));...
    I'm guessing this has to do with the implicity, but I just don't understand completely.

    If you can help, I really appreciate it. If you don't, I appreciate your help anyway, it's much clearer now than before!!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Can someone explain this at a dummy level for me, please? (OOP classes, objects,

    As I said, I haven't used C++ for a long time (about 15 years) so I'm not sure what it does and doesn't do implicitly. That second code snippet is obviously creating a Complex object because it does so explicitly. I'm not really sure what the first code snippet would create. When using just the double value, maybe it tries to convert the value on the right to a Complex by whatever means it can and one of those means is invoking the constructor with just one argument. It appears that both arguments are optional so maybe there's a mechanism that can implicitly invoke a constructor with a single argument if the value provided is the type of that parameter. You could test that theory with other types that have fields and constructor arguments of types other than double.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Can someone explain this at a dummy level for me, please? (OOP classes, objects,

    I just followed the link in your first post and it says exactly that in the second section. If there is a constructor with only one parameter and it is declared without the 'explicit' keyword, that constructor can be invoked implicitly to convert a value of the same type as the parameter to an instance of that type. Even though the constructor has two parameters in this case, those parameters are optional (by virtue of the fact that a default value is provided in the declaration) so the constructor can be invoked with one argument of type double. That means that a value of type double can be implicitly converted to an instance of that type using that constructor.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width