Hi ![:)]()
Can someone explain me how this code works ?
At line 37,class test hasn't got a operator+ with parameter test and int,also
How does this work? The class has just one operator+ that takes two objects.Is the int converted to a test object? How?
If the class wouldn't have a constructor which has as parameters a int,the code wouldn't work...
Also is the constructor used here ? :| How does it apply ? Is there a rule or something ?
Thank you,have a nice evening
Can someone explain me how this code works ?
#include <iostream>
using namespace std;
class test{
private:
int num;
int den;
public:
test();
test(int);
test(int,int);
void print();
friend test operator+(const test&,const test&);
};
test::test(){}
test::test(int nnum) {
num=nnum;
den=1;
}
test::test(int nnum,int nden){
num=nnum;
den=nden;
}
void test::print(){
cout<<"num: "<<num<<" den: "<<den;
}
test operator+(const test& one,const test& two) {
test toreturn(one.num+two.num,one.den+two.den);
return toreturn;
}
int main() {
test obj1(5);
test obj2;
obj2=obj1+1; //here
obj1.print();
cout<<endl;
obj2.print();
cin.ignore();
return 0;
}
At line 37,class test hasn't got a operator+ with parameter test and int,also
test operator+(const test&,int).
How does this work? The class has just one operator+ that takes two objects.Is the int converted to a test object? How?
If the class wouldn't have a constructor which has as parameters a int,the code wouldn't work...
Also is the constructor used here ? :| How does it apply ? Is there a rule or something ?
Thank you,have a nice evening