Polynomial with Class and Header

Gentlemen,

I'm very confused by this recent assignment I received from my Professor. Here is the actual assignment
CMPS 148: Homework 2
Write a class that models a polynomial equation of the form:
The class should have 3 properties, A, B, and C. You are to provide several functions as well,
including an evaluate function that accepts an “x” and computes the results, and a findRoots function
that executes the quadratic formula to determine the equationʼs roots (x values which cause the
polynomial to evaluate to 0).
Details for the evaluate function:
The function should return a double. There should be a single parameter, called x. When called, the
function computes Ax2 + Bx + C using the polynomialʼs A, B, and C values and returns the result.
Details for the findRoots function:
The quadratic formula is used to find the value(s) of x that lead to the polynomial equaling zero. It is
defined as follows:
Where the two values found can be called root1 and root2. If the value of b2-4ac is negative there
are no real roots (the function never equals 0).
Your findRoots function should return true or false, depending on whether or not there are real roots
(if b2-4ac is positive). The function accepts two double values using pass by reference. If there are
roots, then those two parameters are set within the function.
On the next page is a main function that uses the Polynomial class. For this homework assignment,
you must use this EXACT program – without modification! It is posted as a separate file on
Moodle. You may of course add print statements to help you debug while you work, but I will expect
you to turn in the file without any of your modifications.


Then here is the main
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
#include <iostream>
#include "Polynomial.h"
using namespace std;

void printRoots(Polynomial p) {
    double r1=0, r2=0;

    if ( p.findRoots(r1, r2) ) {

        cout << "\twith roots at " << r1 << " and " << r2 << endl;
    } else {

        cout << "\tNo Roots" << endl;
    }

}


int main() {

Polynomial p1(14, 19, 2); // Provide a constructor to set A, B, C

Polynomial p2;

// Default constructor should initialize to all zeros

double tmp;

cout << "Enter A:  ";
cin >> tmp;
p2.setA(tmp);

cout << "Enter B:  "; // A, B, and C need to be private variables - supply getters and setters.
cin >> tmp;
p2.setB(tmp);

cout << "Enter C:  ";
cin >> tmp;
p2.setC(tmp);

cout << "Two polynomials will be used:  " << endl;
cout << "P1: Predefined:  ";
p1.print(); // Should print as 14x^2 + 19x + 2

cout << "P2: User-Defined: ";
p2.print(); // Should print in the same format, but with the user-entered values.

cout << "Enter an x value to evaluate both polynomials: ";
cin >> tmp;

cout << "P1: " << p1.evaluate(tmp) << endl;
printRoots(p1);
cout << "P2: " << p2.evaluate(tmp) << endl;
printRoots(p2);

}


Here is the Header file I created
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
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

class Polynomial {
public:

    double x, p1, p2;
    int a1, b1, c1;

    Polynomial();
    Polynomial(double a1, double b1, double c1);

    double evaluate (double x);
    double findRoots (double &r1, double &r2);
    double setA(double a);
    double setB(double b);
    double setC(double c);
    void print();

private:

    double a, b, c;

};

#endif // POLYNOMIAL_H 


Lastly here is another source
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
#include "Polynomial.h"
#include <iostream>
#include <cmath>

using namespace std;

double tmp;

Polynomial :: Polynomial() {

    x, a, b, c = 0;

}

double Polynomial :: Polynomial (a1, b1, c1){



}

double Polynomial :: evaluate(double x){

}


    // j * pow (x, 2) + k * x + c = 0


double Polynomial :: setA(double a){

    a = tmp;
}

double Polynomial :: setB(double b){

    b = tmp;

}

double Polynomial :: setC(double c){

    c = tmp;

}

void Polynomial :: print(){

}



Sorry for all the space taken.. Here are my questions:

As you can see I have the constructors defined. I'm wondering how can I possible have the print() constructor and the printRoots() accept two different sets of variables but function in the same constructor?

I could do this but the issue is that I cannot edit main() to make use of seperate constructors / functions.

I thought of a robust and stupid way of using a if statement with a counter in the constructor and having it say if this is the second time you're being used then print this or use these variables instead. I feel there is a simpler way though.

Thanks in advance
I don't understand what you want. Try to provide an example.

> I'm wondering how can I possible have the print() constructor
print() is not a constructor but a method.
Here is the output I'm trying to get :

Sample output – user input in red.
Enter A:1
Enter B:1
Enter C:1
Two polynomials will be used:
P1: Predefined:
14x^2 + 19x + 2
P2: User-Defined: 1x^2 + 1x + 1
Enter an x value to evaluate both polynomials: 8
P1: 1050
with roots at -0.11501 and -1.24213
P2: 73
No Roots
Topic archived. No new replies allowed.