I am having trouble with what I believe to be the insertion operator overload of my program. It is an assignment for a beginners c++ class in which I am supposed to use descendant functions to perform tasks with complex numbers and vectors. When I input a number for either class it is either not being read in correctly or assigned to the array correctly. I have been trying to sort this out for over an hour now and nothing I try seems to work.
The classes:
class pairs
{
protected:
double a;
double b;
public:
pairs(): a(0), b(0){}
pairs(const pairs&p): a(p.a), b(p.b){}
pairs(double x, double y): a(x), b(y){}
pairs operator +(pairs& second){
return pairs(a+second.a, b+second.b);
}
pairs operator -(pairs& second){
return pairs(a-second.a, b-second.b);
}
bool operator ==(pairs& second){
bool tf=false;
if (a==second.a && b==second.b)
tf=true;
return tf;
}
};
class comp:public pairs
{
public:
comp():pairs(){}
comp(const pairs&p):pairs(p){}
comp(double x, double y):pairs(x, y){}
comp operator *(comp& second){
comp mew;
mew.a=(a*second.a)-(b*second.b);
mew.b=(a*second.b)+(b*second.a);
return mew;
}
comp operator /(comp& second);
friend ostream& operator << (ostream& fout, comp& num){
if(num.b<0)
cout<<num.a<<num.b<<"i";
else
cout<<num.a<<"+"<<num.b<<"i";
return fout;
}
friend istream& operator >> (istream& fin, comp num){
char sym, i;
cout<<"Enter a complex number in a+bi or a-bi form: ";
cin>>num.a>>sym>>num.b>>i;
if(sym=='-')
num.b*=-1;
return fin;
}
};
class vect:public pairs
{
public:
vect():pairs(){}
vect(const pairs&p):pairs(p){}
vect(double x, double y):pairs(x, y){}
vect operator*(double num){
return vect(a*num, b*num);
}
int operator*(vect num){
int j;
j=(a*num.a)+(b*num.b);
return j;
}
friend ostream& operator << (ostream& fout, vect& num){
cout<<"<"<<num.a<<","<<num.b<<">";
return fout;
}
friend istream& operator >> (istream& fin, vect num){
char beak, com;
cout<<"Enter vector in <a,b> form: ";
cin>>beak>>num.a>>com>>num.b>>beak;
return fin;
}
};
The call to the insertion operators looks like:
comp temp;
int store;
cin>>temp;
cout<<"Where do you want to store this (enter 1-6): ";
cin>>store;
while(store<1 || store>6)
{
cout<<"Invalid location. re-enter: ";
cin>>store;
}
six[store-1]=temp;
break;
It's the same for vect if you change 'comp temp' to 'vect temp' An array of comp or vect size 6 is passed into the function, which is why six[] is not shown as being declared.
I tried running the program and printing temp before it was assigned to the array and both values where still at zero and I am at a loss for why that may be.
Any advice is greatly appreciated. :]