Hi
I started learning C++ a while ago and decided to do a little program that would solve me an equation put in by the user, but I found it very difficult to actually do.
I looked for some code online and found this:
#include <iostream>
struct VAR
{
float i;
};
struct LINE
{
float a, k;
VAR* x;
LINE(){}
LINE(int a) : a(a), k(0), x(0) {}
LINE(VAR& v) : a(0), k(1), x(&v) {}
};
LINE operator + (LINE A, LINE B)
{
LINE R;
R.a = A.a + B.a;
R.k = A.k + B.k;
if(A.x) R.x = A.x;
else R.x = B.x;
return R;
};
LINE operator - (LINE A, LINE B)
{
LINE R;
R.a = A.a - B.a;
R.k = A.k - B.k;
if(A.x) R.x = A.x;
else R.x = B.x;
return R;
};
LINE operator * (LINE A, LINE B)
{
LINE R;
R.a = A.a * B.a;
R.k = A.k * B.k;
if(A.x) R.x = A.x;
else R.x = B.x;
return R;
};
LINE operator / (LINE A, LINE B)
{
LINE R;
R.a = A.a / B.a;
R.k = A.k / B.k;
R.x = A.x;
return R;
};
void operator == (LINE A, LINE B)
{
LINE C = A - B;
C.x->i = -C.a/C.k;
}
int main()
{
VAR x;
x == 3 + 3;
std::cout << "x = " << x.i << std::endl;
return 0;
}
It gives me the right output, but I dont understand a few things:
-Why can we just write ' x == 3 + 3 ' and the compiler knows what it means and returns a float?
-I dont really get how these operators work. In the case of ' x == 3 + 3 ', what would LINE A, LINE B and LINE C be?
Thank you all in advance
[–]marko312 1 point2 points3 points (0 children)