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

Please Help this, A one-variable quadratic expression is an arithmetic expressio

ID: 646017 • Letter: P

Question

Please Help this,

A one-variable quadratic expression is an arithmetic expression of the form ax^2 + bx + c, where a, b, and c are some fixed numbers (called the coefficients) and x is a variable that can take on different values. Specify, design, and implement a class that can store information about a quadratic expression. The default constructor should set all three coefficients to zero, and another member function should allow you to change these coefficients. There should be constant member function to allow you to

Explanation / Answer

#include <iostream>

using namespace std;

class quadratic{
public:
   double a, b, c;
   quadratic();
   quadratic(double a, double b, double c);
   void setValues(double a, double b, double c);
   friend quadratic operator +(const quadratic& q1, const quadratic& q2){
       return quadratic(q1.a + q2.a, q1.b + q2.b, q1.c + q2.c);
   }
   friend quadratic operator *(double r, const quadratic& q){
       return quadratic(q.a * r, q.b * r, q.c * r);
   }
};

quadratic::quadratic(){
   setValues(0, 0, 0);
}

quadratic::quadratic(double a, double b, double c){
   setValues(a, b, c);
}

void quadratic::setValues(double a, double b, double c){
   this->a = a;
   this->b = b;
   this->c = c;
}

int main(){
   quadratic q1 = quadratic(2, 3, 4);
   quadratic q2 = 2 * q1;
   quadratic q3 = q1 + q2;
   cout << "q1 is " << q1.a << " " << q1.b << " " << q1.c << endl;
   cout << "q2 is " << q2.a << " " << q2.b << " " << q2.c << endl;
   cout << "q3 is " << q3.a << " " << q3.b << " " << q3.c << endl;
   return 0;
}