C++ Class Question

BChico

Platinum Member
May 27, 2000
2,742
0
71
We are doing overloaded functions right now, and i have the +,-,and* ops loaded correctly, in that i can do class+class and class+double. But i cannot do double+class. The prof said that the constructor should automatically convert the double into a type of class. How is this possible?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
"convert the double into a type of class." What does this mean? A double is a pod ("plain ol' data") type. If you want to add your class with a double, with the double on the left, I'm pretty sure the only thing you can do is implement a standalone operator+ with double as the left hand side and your type as the right hand side.

myclass operator+(double d, myclass x)
{
....
}


edit: I think your instructor was saying that your class has a conversion constructor that will take a double as an argument and construct an appropriate 'myclass' from it. So you would do:

myclass(some_double) + some_instance_of_myclass
 

BChico

Platinum Member
May 27, 2000
2,742
0
71
Hmm...if you are adding myclass+myclass, you only need one parameter in the prototype correct? So then to do double on the left side of the op, im going to need to repeat all of the prototypes and defs. Is there anyway to have the double or any other type, converted automattically to myclass?
 

BChico

Platinum Member
May 27, 2000
2,742
0
71
class Complex
{
private:
double real;
double imag;
public:
Complex(double r, double i);
Complex(double real_part);
Complex();
bool operator == (Complex A);
Complex operator + (Complex A);
// Sum = C + A
Complex operator * (Complex A);
// Product = C * A
Complex operator - (Complex A);
// Differealnce = C - A
friend ostream& operator << (ostream& OS, Complex A);
friend istream& operator >> (istream& IS, Complex &A);
double re();
double im();
};

Complex::Complex (double r, double i)
{
real = r;
imag = i;
}

Complex::Complex (double r)
{
real = r;
imag = 0;
}

Complex::Complex()
{
real=imag=0;
}

bool Complex::eek:perator ==(Complex a)
{
if(a.real==real && a.imag==imag)
{
return true;
}
else return false;
}

Complex Complex::eek:perator +(Complex a)
{
Complex x;
x.real = a.real + real;
x.imag = a.imag + imag;
return x;
}

Complex Complex::eek:perator *(Complex a)
{
Complex x;
x.real = a.real * real - a.imag * imag;
x.imag = real * a.imag + a.real * imag;
return x;
}

Complex Complex::eek:perator -(Complex a)
{
Complex x;
x.real = real - a.real;
x.imag = imag - a.imag;
return x;
}



Error on:
z = d + x;
cout << &quot;d+x: &quot; ;
cout << z << endl;
z = d - x;
cout << &quot;d-x: &quot; ;
cout << z << endl;
z = d * x;
cout << &quot;d*x: &quot; ;
cout << z << endl;

Says cant add double to complex
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
... In place of "double + myclass".

Yes, you need to create all of the various overloaded operators you need with doubles as the left-hand side, if you want them. You could just template them and rely on your class having the appropriate conversion constructors for whatever types might be used.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Why do you have to make the iostream operators friends when you have public accessor methods for your real and imaginary members? And you could implement things a lot more concisely, i.e:

Complex Complex::eek:perator -(Complex a)
{
return Complex(real - a.real, imag - a.imag);
}
 

BChico

Platinum Member
May 27, 2000
2,742
0
71
Ugh..thats the way she taught it too us...she said we can do whatever we want though...give ya 5 bucks to finish this for me...lol
 

BChico

Platinum Member
May 27, 2000
2,742
0
71
I like programming, its a requirement, but i do enjoy it...im just at the bad part in pledging and i have a math 205 exam tomorrow, linear methods, which i have a good amount of studying to do
 

BChico

Platinum Member
May 27, 2000
2,742
0
71
Well that shortened the functions alot...

bool Complex::eek:perator ==(Complex a)
{
return (a.real==real && a.imag==imag);
}

Complex Complex::eek:perator +(Complex a)
{
return Complex(real + a.real, imag + a.imag);
}

Complex Complex::eek:perator *(Complex a)
{
return Complex(a.real * real - a.imag * imag, real * a.imag + a.real * imag);
}

Complex Complex::eek:perator -(Complex a)
{
return Complex(real - a.real, imag - a.imag);
}

Im still not quite sure how to make the other work...

I was trying:

double Complex::eek:perator -(Complex a)
{
return Complex(real - a.real, imag - a.imag);
}

But that wasnt working right...how should i do it?
 

BChico

Platinum Member
May 27, 2000
2,742
0
71
Umm..is this what you mean...im still getting an error...

Complex operator + (Complex(double d), Complex a);

Complex Complex::eek:perator + (Complex(double d)), Complex a)
{
return 0;
}
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
That syntax makes no sense. I typed *exactly* what you should do in my first post.

Complex operator+(double d, Complex a)
{
........
}
 

BChico

Platinum Member
May 27, 2000
2,742
0
71
Using that as the prototype, its giving me an error that says, that it can only have zero or one parameter.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
It should only say that if you're trying to implement Complex::eek:perator+ with two arguments (which you should not do).
 

BChico

Platinum Member
May 27, 2000
2,742
0
71
Yeah like this:

Complex operator +(double d, Complex a);

Isnt that what you said to do...im not really that familiar with overloaded operators as you can see....
 

BChico

Platinum Member
May 27, 2000
2,742
0
71
Ok nice...that got the erros taken off...at least it compiles now. To add and sutract the double to the myclass, do i have to convert it to myclass first?
 

BChico

Platinum Member
May 27, 2000
2,742
0
71
I think this works:

Complex operator +(double d, Complex a)
{
return Complex(d)+a;
}

Complex operator -(double d, Complex a)
{
return Complex(d)-a;
}

Complex operator *(double d, Complex a)
{
return Complex(d)*a;
}

Thanks for your help!