• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ Class Question

BChico

Platinum Member
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?
 
"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
 
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?
 
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:😱perator ==(Complex a)
{
if(a.real==real && a.imag==imag)
{
return true;
}
else return false;
}

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

Complex Complex:😱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:😱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
 
... 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.
 
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:😱perator -(Complex a)
{
return Complex(real - a.real, imag - a.imag);
}
 
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
 
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
 
Well that shortened the functions alot...

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

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

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

Complex Complex:😱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:😱perator -(Complex a)
{
return Complex(real - a.real, imag - a.imag);
}

But that wasnt working right...how should i do it?
 
Umm..is this what you mean...im still getting an error...

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

Complex Complex:😱perator + (Complex(double d)), Complex a)
{
return 0;
}
 
Using that as the prototype, its giving me an error that says, that it can only have zero or one parameter.
 
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....
 
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?
 
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!
 
Back
Top