Polynomial Class

Hi folks,

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.

Thank you for your help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#pragma once
#include <iostream>

const int 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"
using namespace 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"
using namespace 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");
}
Last edited on
What does your input look like?
Last edited on
1
2
3
4
5
for (int j = 0; j < terms[MAX]; j++)
{
			pointer[j] = tms;
			pointer[i] = ex;
}


You try to use a simple pointer as an array. You need to allocate memory for it to be considered an array

 
double * pointer=new double[number];
Last edited on
Operator overloads should return by value, not reference
Data member int exponent is unused, what does it do?
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?
Last edited on
Topic archived. No new replies allowed.