I'm a beginner in C++ and I've just started learning about OOP. In the following program I've added objects of the same classes and displayed the result. However, I'm not able to understand the fact that if I pass the objects to the function by value then how is the change reflected in the calling function. The addNumbers()
function expects two objects of the class Complex and the object which is used to invoke the function (c3.addNumbers(c1, c2)
) is implicitly passed to the function but how are the values of c3.real
and c3.imaginary
affected in the calling function since addNumbers()
has no access to their "location" in the memory. Any help will be appreciated!
Thanks in advance!
class complex {
private:
int real;
int imaginary;
public:
/* Using member initializers to assign values to members */
complex()
: real(0)
, imaginary(0)
{}
void readData(int x, int y);
void printData();
void addNumbers(complex, complex);
};
void complex::readData(int x, int y)
{
real = x;
imaginary = y;
}
void complex::printData()
{
cout << real << "+" << imaginary << "i" << endl;
}
void complex::addNumbers(complex c1, complex c2)
{
real = c1.real + c2.real;
imaginary = c1.imaginary + c2.imaginary;
}
int main(void)
{
complex c1, c2, c3;
c1.readData(-5,17);
c2.readData(11,7);
c3.addNumbers(c1,c2);
c3.printData();
return 0;
}
I made a few comments in your original code to explain why real and imaginary are affect below. (Look for //MABVT)
In addition: I will provide another useful example for you to progress further!
REVIEW
ALTERNATIVE
This should be quite much for you as a beginner.
Some explanation
Static global vs. local operator overloads
Reading on the topic: http://en.cppreference.com/w/cpp/language/operators
All the operators you use (+, -, *, /, %, +=, -=, ...) are just functions, which are predefined for primitive types and provided by libstd for STD types.
You can override/define them though.
I did that in two ways:
And:
const Type&
Reading with much more info on const: https://www.cprogramming.com/tutorial/const_correctness.html
Const reference to instances of whatever type will ensure two things for you:
In combination that means: You don't have to copy the instance (pass-by-value), but provide it's address reference only (pass-by-reference). Usually that enhances performance, especially once you pass around large and complex objects.
When you call
c3.addNumbers(c1, c2))
,addNumbers
receives implicitely the pointer toc3
not a copy ofc3
. This pointer can be used explicitely with thethis
keyword.So your function can be rewritten like this:
which is strictly equivalent to your original
addNumbers
function.In other words: each time you use a class member inside a member function, an implicit
this->
is prepended to that member; so ifmember
is a class member, thenmember
is always equivalent tothis->member
inside a class member function.The imaginary and the real are private properties, but they can be accessed through the member function (also known as the object's method). When the c3.addNumbers (c1, c2) statement is executed, it will be equivalent to the following two statements:
c3.real = c1.real + c2.real;
c3.imaginary = c1.imaginary + c2.imaginary
The reason we can access c3.real and c3.imaginary is because the addNymbers () function is a member of the Complex class.