PLEASE HELP! CLASS C++


1) Implement a class Quadratic that represents second degree polynomials, i.e., polynomials of type
ax2 + bx +c. The class will require three data members corresponding to a, b and c (they should be real numbers) and the following member functions:
· A constructor without arguments

· An overloaded constructor with arguments.

· “evaluate” function to compute the value of polynomial for a given value of x.

· “print” function which prints the data members of a polynomial in the following format:

a*x^2 + b*x + c
for example if a=3, b=4, c=5 the output should be:
3*x^2 + 4*x + 5
· “derivative” function which computes and returns the derivative of the polynomial for a given value of x:

first derivative= 2ax + b
· “sum” function which takes two quadratic polynomials and save their summation in the calling object (see main function below)

Note that if p(x)= ax2 + bx + c and q(x)= a′x2 +b′x +c′, then their summation is the polynomial given by (a + a′)x2 + (b + b′)x + (c + c′).
· “delta” function to compute and return the value of delta (D)

D = b2 – 4ac
· “realRoots” function which finds the real roots of the polynomial. This function returns an integer and it takes two reference parameters x1 and x2 as explained below:

Recall that the roots of p(x) = ax2+bx+c are the solutions of the equation p(x) = 0. This equation
has real solutions if and only if D >=0.
If a ¹ 0 and D >=0, the roots are given by:
x1= (b + sqrt(D)) / 2a
x2 = (–b - sqrt(D)) / 2a
In this case, the function realRoots returns 2 and it sets the reference parameters x1 and x2 to the values of the two roots (we set them to the same value if D = 0).
If a ¹ 0 and D< 0, realRoots returns 0.
If a = 0 and b ¹ 0, the polynomial has only one root −c/b. In this cases the realRoots returns 1
and it sets the reference parameter x1 to the single root of the polynomial.
If both a and b are zero, then the equation has either no solution if c ¹ 0 or infinitely many solutions if c = 0. In the first case realRoots returns 0, and in the second it returns 999.
Test your class using the following program:
int main()
{
quadratic p(1,5,3),q(1,2,1), r;
cout<<"p: ";p.print();
cout<<"q: ";q.print();
cout<<"r: ";r.print();
cout<<"p(2)="<<p.evaluate(2)<<endl;
r.sum(p,q);
cout<<"p+q: ";r.print();
cout<<"derivative of p for x=5: "<<p.derivative (5)<<endl;
double x1,x2;
int nbRoots = p.realRoots(x1,x2)
if (nbRoots ==2)
cout<<"roots of p : "<<x1<<","<<x2<<endl;
else if (nbRoots==1)
cout<<"root of p: "<<x1<<endl;
else if (nbRoots==0)
cout<<"No roots for p"<<endl;
else if (nbRoots=999)
cout<<"Infinetly many solutions for p"<<endl;
cout<<endl;
return 0;
}
Topic archived. No new replies allowed.