|
-
Mar 12th, 2018, 09:47 PM
#1
Thread Starter
Member
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;
}
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|