Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me with this Design and implement a class quadratic to represent a q

ID: 3806573 • Letter: P

Question

Please help me with this

Design and implement a class quadratic to represent a quadratic function, a function of the form f(x) = ax^2 +bx +c Include private data members for coefficients a, b and c. Define a default constructor and a constructor that initializes a, b and c to the values of its three parameters. Overload the input extraction operator to input a Quadratic object as three coefficients. Overload the output insertion operator to display the quadratic in this form: -2x^2 -4x+35 Define a member function to evaluate the function at a value for x provided as a parameter, returning the value calculated as the function result. Also, define a member function that displays the two real roots of the quadratic or an appropriate message if the function has a double root or no real roots. You will use the quadratic formula in this function. This programming is for C++

Explanation / Answer

Ans: The code in c++ is explained with comments

#include<iostream>

#include<math.h>

// using namespace

using namespace std;

//quadratic class

class Quadratic

{

private :

int a,b,c,x; // a, b, c, x as in eqaution

public:

Quadratic()

{

a=b=c=0;

}

Quadratic(int a,int b,int c)

{

this->a=a;

this->b=b;

this->c=c;

}

//overloading

friend ostream &operator<<(ostream &out, Quadratic &q)

{

out<<" Quadratic equation is ";

out <<q.a<<" X * X + "<<q.b<<" X +"<<q.c ;

return out;

}

friend istream &operator>>(istream &in, Quadratic &q)

{

// for entering a b c values

cout<<" Enter values of a,b,c of quadratic equation: ";

// starting process for getting solved

in>>q.a>>q.b>>q.c;

return in;

}

Quadratic operator +(Quadratic);

void eval();

void findRoots(); // find the roots

};

/* void Quadratic::getData()

{

cout<<" Enter values a, b and c values ";

cin>>a>>b>>c;

}

void Quadratic::putData()

{

cout<<" Values of a,b and c are "<<a<<" "<<b<<" "<<c;

}

*/

void Quadratic::eval()

{

cout<<" Enter x value ";

// equation calculation

cin>>x;

int result=(a*x*x)+(b*x)+c;

cout<<" For x= "<<x<<" Answer : "<<result<<endl;

}

void Quadratic::findRoots()

{

int temp=(b*b)-(4*a*c);

if(temp>0)

{

// using formula for calculating roota

float r1=(-b+sqrt(temp))/(2.0*a);

float r2=(-b-sqrt(temp))/(2.0*a);

cout<<" Roots are real and different";

cout<<" Root1= "<<r1;

cout<<" Root2= "<<r2;

}

else if(temp==0)

{

cout<<" Roots are real and equal";

float r=(-b+sqrt(temp))/(2.0*a);

cout<<" Equal Roots are"<<r;

}

else

{

cout << " Roots are complex and different." << endl;

//if real roots

float real = -b/(2*a);

// imaginery roots

float imaginary =sqrt(-temp)/(2*a);

cout << "Root1 = " << real << "+" << imaginary << "i" << endl;

cout << "Root2 = " << real << "-" << imaginary << "i" << endl;

}

}

Quadratic Quadratic::operator +(Quadratic q)

{

// storing values using temp

Quadratic temp;

temp.a=this->a+q.a;

temp.b=this->b+q.b;

temp.c=this->c+q.c;

return temp;

}

int main()

{

Quadratic q1,q2,q3; // final step to evalute roots

cin>>q1;

cin>>q2;

q3=q1+q2;

cout<<" Quadratc equation 1, the roots and result ";

cout<<q1;

// for equation 1

q1.findRoots();

q1.eval();

cout<<" Quadratic equation 2, the roots and result ";

cout<<q2;

// for equation 2

q2.findRoots();

q2.eval();

cout<<" Quadratic equation 3, the roots and result ";

// for equation 3

q3.findRoots();

q3.eval();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote