I need some assistance with getting the logic for my Polynomial class to work. The program is supposed to create two Polynomials and then add/subtract them together via overloaded operators.
Right now, the code compiles, but does not output any of the objects. I figure it's with my set/get functions and/or my data members.
Unfortunately, I have been working on this for 3 days now (not this specific issue) and I cannot connect where the issue(s) reside. I would appreciate if you could flat out tell me where my errors are and what I can do to improve them.
#pragma once
#include <iostream>
constint MAX = 2;
class Polynomial
{
public:
Polynomial();
~Polynomial();
void setPolynomial(int ex, int tms);
void getPolynomial();
Polynomial& operator+(const Polynomial& p);
Polynomial& operator-(const Polynomial& n);
private:
double *pointer;
int terms[MAX];
int exponent;
};
#include "stdafx.h"
#include <iostream>
#include "Polynomial.h"
usingnamespace std;
Polynomial::Polynomial()
{
for (int i = 0; i < 2; i++)
{
terms[i] = 0;
}
}
Polynomial::~Polynomial()
{
if (pointer)
{
delete [] pointer;
pointer = NULL;
}
}
void Polynomial::setPolynomial(int ex, int tms)
{
for (int i = terms[MAX] - 1; i >= 0; i--)
{
for (int j = 0; j < terms[MAX]; j++)
{
pointer[j] = tms;
pointer[i] = ex;
}
}
}
void Polynomial::getPolynomial()
{
for (int i = terms[MAX] - 1; i >= 0; i--)
{
cout << pointer[i] << "x^" << i << endl;
}
}
Polynomial & Polynomial::operator+(const Polynomial & p)
{
Polynomial poly;
for (int d = 0; d < terms[MAX]; d++)
{
poly.terms[MAX] = this->terms[MAX] + p.terms[MAX];
}
return poly;
}
Polynomial & Polynomial::operator-(const Polynomial & n)
{
Polynomial nomial;
for (int e = 0; e < terms[MAX]; e++)
{
nomial.terms[MAX] = this->terms[MAX] - n.terms[MAX];
}
return nomial;
}
#include "stdafx.h"
#include <iostream>
#include "Polynomial.h"
usingnamespace std;
int main()
{
Polynomial polyOne;
Polynomial polyTwo;
Polynomial polyThree;
int result;
cout << "Welcome to the Polynomial Tester Program!" << endl;
cout << endl;
cout << "Here, this program creates two unique polynomial objects from a user-defined class known as Polynomial." << endl;
cout << "The program will then add and subtract both objects and display the results.\nLet's begin!" << endl;
cout << endl;
polyOne.setPolynomial(8, 2);
polyTwo.setPolynomial(4, 6);
cout << "Here are the results of Polynomial #1: " << endl;
polyOne.getPolynomial();
cout << "Here are the results of Polynomial #2: " << endl;
polyTwo.getPolynomial();
polyThree = polyOne + polyTwo;
cout << "Here is the result when both polynomials are added together: " << endl;
polyThree.getPolynomial();
polyThree = polyOne - polyTwo;
cout << "Here is the result when both polynomials are subtracted from one another: " << endl;
polyThree.getPolynomial();
cout << "Thank you for using the Polynomial Tester Program! We hope to see you again!" << endl;
system("pause");
}
First off, thank you guys for all of your responses. I appreciate the help.
Bdanielz, the output right now prints all the cout << statements correctly, but none of the Polynomial objects print out.
Golden Lizard, can you clarify a bit more what you mean? I understand what you are saying at face value, but I do not understand why this needs to be done? The book I have does a poor job at explaining how to use the pointer in the dynamic array (I believe that's the format I need to use here?).
gunnerfunner, my apologies. Exponent is left over code from my first draft of this project. I used it and another int coefficient variable to obtain the Polynomial values. I got the values to print out this way using the pass-by-reference operators, but I only obtained the coefficient and nothing more.
Why is it incorrect to pass by reference and not by value in this case? This is how my book sets it up (and I am starting to think I do not have that great of a book...)
Why is it incorrect to pass by reference and not by value
I said return, not pass ... this is because the methods return variables poly and nomial respt whose scope are limited to the respt functions' braces. So once these functions return the variables are lost unless you make a copy of them which is what returning by value does. Returning by reference returns the actual variables that cease to exist once the functions defining them return
BTW, what is the generic form of polynomial that you have in mind for this class: ax^2 + bx + c?