I am having some trouble on a project and its probably something simple but I cant figure it out. It is my first time working with classes and I've been messing with it all day so the code might be a little off. Anyway my problem is I am suppose to make a class that can add or subtract two polynomials, or multiply a polynomial by a constant, but I cant get the member class variables to change. After I input the degree and the constants and the class goes to the next object everything resets to zero. I made a separate code that used functions instead and everything worked fine so I just need to know how to change the member class variables so when I input the numbers in they will work with every object in the class.
Here's some of my code.
class Polynomial10
{
private:
double coef[9];
int degree;
public:
Polynomial10();//Constructor for a new Polynomial10
Polynomial10(int d, double c[]);
int getDegree(){return degree;};
void print(); //Print the polynomial in standard form
void read(); //Read a polynomial from the user
void add(); //Add a polynomial
void multc(); //Multiply the poly by scalar
void subtract(); //Subtract polynom
};
Polynomial10::Polynomial10()
{
degree=0;
for (int i=0; i<9; i++)
{
coef[i]=0;
}
}
Polynomial10::Polynomial10(int d, double c[]) //I don't think this is needed
{
degree=d;
for (int i=0; i<9; i++)
{
coef[i]=c[i];
}
}
void Polynomial10::read()
{
do
{
cout << "Enter degree of a polynom between 1 and 10 : ";
cin >> degree;
} while (degree < 1 || degree > 10);
cout << "Enter space separated coefficients starting from highest degree\n";
for (int i=0; i<=degree; i++)
{
cin >> coef[i];
}
Polynomial10 a(degree,coef); // I was just screwing around here hoping //something would work
}
void Polynomial10::print()
{
int i;
int m;
int n=degree;
m=degree;
//after this it's a lot of code to make the output look nice
int main ()
{
cout << "\nCGS 2421: The Polynomial10 class\n\n" << endl;
Polynomial10 polydgree;
Polynomial10 prnt;
Polynomial10 rd;
Polynomial10 plus;
Polynomial10 subtrct;
Polynomial10 multic;
int enteredone;
int choice;
enteredone=0;
do {
choice=choicemenu();
switch(choice)
{
case 0:
return 0;
case 1:
enteredone=1;
rd.read();
break;
case 2:
if ( enteredone != 1 )
{
cout << "Enter the polynomial first" << endl;
break;
}
if ( enteredone == 1 )
{
prnt.print();
break;
}
case 3:
if ( enteredone != 1 )
{
cout << "Enter the polynomial first" << endl;
break;
}
if ( enteredone == 1 )
{
plus.add();
break;
}
case 4:
subtrct.subtract();
break;
case 5:
multic.multc();
break;
}
} while (choice != 0);
}
Also could someone tell be the benefits of having this instead of what I have.