-
Overloading in C++
So I have to do a program overloading the operators >>, <<, +, –, *, and /. I am quite an amatuer at doing this and I was wondering whether someone could help me...I looked at previous threads on this and tried using it but I use putty and it doesn't really work in there. I need to use outFile and get the numbers from inFile if someone could please help me out it would be greatly appreciated
It needs to do this
A = a + bi
B = c + di
>> It extracts two part of complex number.
<< It inserts the complex number with (a) + (b) i format.
+ A+B = (a + c) + (b + d)i
- A - B = (a - c) + (b - d)i
* A* B = (ac - bd) + (bc + ad)i
/ A / B = (ac + bd)/(c2 + d2)+ (bc -ad)/(c2 + d2)i
Input Sample:
2.5 -2.2
1.0 1.0
Output Sample:
< *~ Complex numbers operations ~*>
A = (2.5) + (-2.2) i
B = (1.0) + (1.0) i
A + B is (3.5) + ( -1.2) i
A - B is .. . .
A * B is .. . .
A / B is .. . .
< end >
-
The general rule here is that you do the coding, and we try to give you good advice when you get stuck. ;)
What have you done so far?
-
well.. i really don't know where to start
besides
#include <iostream>
using namespace std;
int main ()
:)
i know that I am supposed to make a class
so I got something like this
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
class complex
public:
void readnums(ifstream& inFile); //i don't know if this counts for the << operator
void divide
void multiply
void subtract
void addition
void print (ofstream& outFile)//again i don't know if this counts for the >> operator
private:
float a;
float b;
float c;
float d;
but like i said i am a real beginnier and am so lost on overloading
-
i was going to use this code but i don't know how to get it to print to the outFile and read from the inFile ... I tried but i keep getting so many errors
Code:
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
class complex
{
float real;
float imag;
public:
complex()
{}
complex(float x,float y)
{
real=x;
imag=y;
}
complex operator + (complex);
complex operator - (complex);
complex operator * (complex);
complex operator / (complex);
void display(void)
{
cout << real<< " +i" << imag<< endl;
}
};
complex complex :: operator +(complex c)
{
complex c2;
c2.real=real+c.real;
c2.imag=imag+c.imag;
return (c2);
}
complex complex :: operator -(complex c)
{
complex c2;
c2.real=real-c.real;
c2.imag=imag-c.imag;
return (c2);
}
complex complex :: operator *(complex c)
{
complex c2;
c2.real = ((real * c.real) - (imag * c.imag));
c2.imag = ((real * c.imag) + (imag * c.imag));
return (c2);
}
complex complex :: operator /(complex c)
{
complex c2;
c2.real=((real * c.real) + (imag * c.imag))/((real * c.real) + (imag * c.imag));
c2.imag=((imag * c.real) - (real * c.imag))/((real * c.real) + (imag * c.imag));
return (c2);
}
int main()
{
complex c1,c2,c3;
int op;
char ch,y,Y;
c1 = complex(5.6,2.7);
c2 = complex(3.5,5.6);
cout<< "Two Complex numbers Are :"<< endl;
c1.display();
c2.display();
do
{
cout<< endl<< "******** MENU *********"<< endl;
cout<< "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5.Exit"<< endl;
cout<< "Enter Your Choice : ";
cin>>op;
switch(op)
{
case 1:
c3 = c1 + c2;
cout<< "Addition of Two complex Nos. :";
c3.display();
break;
case 2:
c3 = c1 - c2;
cout<< "Subtraction of Two complex Nos. :";
c3.display();
break;
case 3:
c3 = c1 * c2;
cout<<" Multiplication of Two complex Nos. :";
c3.display();
break;
case 4:
c3 = c1 / c2;
cout<< "division of Two complex Nos. :";
c3.display();
break;
case 5:exit(0);
default: cout<< endl<< "Aborting!!!!!!!INVALID CHOICE"<< endl;
}
cout<< " Do you want to continue(Y/y)";
cin>>ch;
}
while(ch=='y'||ch=='Y');
return 0;
}
-
Would you PLEASE indent the code?
Also note that the operators should take a const reference to its arguments, if you know what that means.
Would you also care to elaborate on what is wrong and what you expect to happen?