I am very new to CPP and I came across operator overloading
today. It was a bit confusing for me at first and then I kinda caught up. But still, I have doubts about this specific line, Test operator + (Test arg)
, where Test
is the class name and the parameters inside the parenthesis should be my test1
object and tets 2
object. I just don't get it, how this->a
gives the exact value of test.a. Here is my code and it works as expected.
#include<iostream>
using namespace std;
class Test{
public:
int a;
void getValue(int n){
std::cout << "Enter the value for"<<n<<":" << std::endl;
std::cin >> a;
}
Test operator + (Test arg)
{
Test test3;
test3.a=a + arg.a;
return test3;
}
};
int main(){
Test test1,test2,test3;
test1.getValue(1);
test2.getValue(2);
test3=test1+test2;
cout<<"The value in test3 is:"<<test3.a;
}