A one-variable quadratic expression is an arithmetic expression of the form ax^2
ID: 3549098 • Letter: A
Question
A one-variable quadratic expression is an arithmetic expression of the form ax^2+bx+c, where a,b, and cc are some fixed numbers (called the coefficients) and x is a variable that can take on different vaues. Specfy, 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 functions to retreve the current values of the coefficients. There should also be a member function to allow you to "evaluate" the quadratice expression at particular value of x(i.e. the function has one paramter x, and returns the value of the expression ax^2+bx+c).
Also overloa the following operators ( as non member functions) to perform these indicated operations:
quadratic operator+ (
const quadratic& q1,
const quadratic& q2
);
// Postcondition : The return value is the quadratic expression obtained by adding q1 and 12. For example, ther c coefficient of the //return vvalue is the sum of q1's c coefficient and q2's c coefficient.
quadratic operator *(
double r,
const quadratic& q
);
//Post condition: The return a value is the quadratic expression obtained by multiplying each of q's coefficients by the number r.
Notice that the left argument of the overloaded operator * is a double number (rather than a quadratic expression). This allows expression such as 3.14* q, where q us a quadratic expression.
Explanation / Answer
// SAVE IT AS quadratic.h
class quadratic
{
private:
double a;
double b;
double c;
public:
quadratic() { a=0; b=0; c=0; }
quadratic(double a,double b,double c) { this->a = a; this->b= b; this->c = c;}
void set_x_square_coefficient(double c) { a = c; }
void set_x_coefficient(double c) { b = c; }
void set_constant(double b) { c = b; }
double get_x_square_coefficient() const{ return a; }
double get_x_coefficient() const{ return b; }
double get_constant() const{ return c; }
double evaluate(double x)
{
return a*x*x + b*x + c;
}
};
quadratic operator+(const quadratic& q1,const quadratic& q2);
quadratic operator *(double r,const quadratic& q);
// SAVE IT AS quadratic.cpp
#include "quadratic.h"
quadratic operator+(const quadratic& q1,const quadratic& q2)
// Postcondition : The return value is the quadratic expression obtained by adding q1 and 12. For example, ther c coefficient of the
//return vvalue is the sum of q1's c coefficient and q2's c coefficient.
{
return quadratic(q1.get_x_square_coefficient() + q2.get_x_square_coefficient(),
q1.get_x_coefficient() + q2.get_x_coefficient(),
q1.get_constant()+ q2.get_constant());
}
quadratic operator *(double r,const quadratic& q)
//Post condition: The return a value is the quadratic expression obtained by multiplying each of q's coefficients by the number r.
{
return quadratic(r*q.get_x_square_coefficient(),r*q.get_x_coefficient(),r*q.get_constant());
}
// SAVE IT AS main.cpp
#include<iostream>
#include "quadratic.h"
using namespace std;
int main()
{
quadratic q1(1,2,3);
cout << "quadratic q1 evaluated at x = 1 is " << q1.evaluate(1) << endl;
quadratic q2(3,2,1);
quadratic q3 = q1+q2;
cout << "quadratic q3 x^2 coefficients is "<< q3.get_x_square_coefficient() << endl;
cout << "quadratic q3 x coefficients is "<< q3.get_x_coefficient() << endl;
cout << "quadratic q3 constant is "<< q3.get_constant() << endl;
quadratic q4 = 3.14*q3;
cout << "quadratic q4 x^2 coefficients is "<< q4.get_x_square_coefficient() << endl;
cout << "quadratic q4 x coefficients is "<< q4.get_x_coefficient() << endl;
cout << "quadratic q4 constant is "<< q4.get_constant() << endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.