I need the following in C++. Please pay close attention to the specifications. C
ID: 3744180 • Letter: I
Question
I need the following in C++. Please pay close attention to the specifications.
Create a class, called Quadratic, according to the following requirements:
- Private member coefficients for a, b, and c of type double
- Default constructor to initialize coefficients to 0
- Public member function(s) to set coefficients
- Public constant member functions to retrieve coeffient values
- Member functions to evaluate and return (do not display this value in function; return as a double value) quadratic for given value of x
- Overload operators for + and *:
- Overload + operator as member function:
quadratic operator +(const quadratic& q_rhs);
// Postcondition:The return value is the quadratic expression obtained by adding each of the coefficients of the current object by each of the corresponding coefficients of q_rhs
// object. For example, 'a' coefficient of the return value is the result of adding the 'a' coefiicient of the current object by the 'a' coefficient of q_rhs. The 'b' coefficient of the return value is // the result of adding the 'b' coefficient of q_rhs. The 'c' coefficient of the return value is the result of adding the 'c' coefficient of the current object by the 'c' coefficient of q_rhs.
- Overload * operator for as friend function:
quadratic operator *(double m const quadratic& q);
// Postcondition: The return value is the quadratic expression is obtained by multiplying each of q's coefficients by the number m.
Include the following additional programming requirements:
- Only the member functions to retrieve coefficient values (i.e. accessors) can be declared inline
- Use the provided cpp file (do not make any changes to this file) that includes an overload insertion operator (<<) to display the quadratic epression using accessor functions in the format:
ax^2 + bx + c
This file also contains a function that takes a single pass by reference quadratic parameter and updates the coefficients (using mutator function) for subsequent display of the quadratic (using the overloaded insertion operator). A main function includes the following three tests using quadratic objects:
- Evaluate quadratic for given coefficients and x
- Create new quadratic using overloaded '+' operator and evaluate
- Create new quadratic using overload '*' operator and evaluate
- Include a sample run of your program as a multi-line comment at the end of the specification file. Attach copies of the quadratic specification and implementation files to this sheet for full credit.
The program shouls display the following output
// File: Lab2.cpp // Desc: A program which asks user to input values for a quadratic expression and evaluating value of x. It then asks expression and multiply the chosen by the user. expression before returning that the expression for a user input the user to add a value to the expression by a certain value, both #include #include "Quadratic . h" usinq namespace std; // overloading output as non-member and non-friend function std::ostream& operatorExplanation / Answer
ScreenShot
--------------------------------------------
Program
Quadratic.h
#pragma once
#include<iostream>
using namespace std;
class Quadratic {
//Member variables
private:
double a;
double b;
double c;
//Member functions
public:
Quadratic();
void setA(double first);
void setB(double second);
void setC(double third);
double getA() const;
double getB() const;
double getC() const;
friend ostream &operator<<(ostream &out, const Quadratic &quad);
double evalQuadX(double);
Quadratic operator +(const Quadratic& q_rhs);
friend Quadratic operator*(double m,const Quadratic& q_rhs);
};
Quadratic.cpp
//Include header file
#include "Quadratic.h"
//Default constructor
Quadratic::Quadratic() {
a = 0;
b = 0;
c = 0;
}
//Mutators
void Quadratic::setA(double first) {
a = first;
}
void Quadratic::setB(double second) {
b = second;
}
void Quadratic::setC(double third) {
c = third;
}
//Accessors
double Quadratic::getA() const {
return a;
}
double Quadratic::getB() const {
return b;
}
double Quadratic::getC() const {
return c;
}
//Overload output operator
ostream &operator<<(ostream &out, const Quadratic &quad) {
out << quad.getA() << "X^2+ " << quad.getB() << "X+ " << quad.getC() << endl;
return out;
}
//Evaluate x multiplied expression
double Quadratic::evalQuadX(double x) {
return ((a*(x*x)) + b * x + c);
}
//Overload + operator
Quadratic Quadratic::operator +(const Quadratic& q_rhs) {
Quadratic q;
q.a = this->a + q_rhs.a;
q.b = this->b + q_rhs.b;
q.c = this->c + q_rhs.c;
return q;
}
//Overload * operator
Quadratic operator*(double m,const Quadratic& q_rhs) {
Quadratic q;
q.setA(q_rhs.getA()*m);
q.setB(q_rhs.getB()*m);
q.setC(q_rhs.getC()*m);
return q;
}
Lab2.cpp
//Include header file
#include "Quadratic.h"
//Display quadratic expression
void createQuad(Quadratic &quad) {
double a, b, c;
cout << " a: ";
cin >> a;
cout << " b: ";
cin >> b;
cout << " c: ";
cin >> c;
quad.setA(a);
quad.setB(b);
quad.setC(c);
cout << " The quadratic expression is: " << quad << endl;
quad.setC(c);
cout << " The quadratic expression is: " << quad << endl;
}
int main()
{
double x, multiplier;
Quadratic eq1, eq2, eq1a2, eq1m;
//Create first quadratic equation
cout << "Input the values for the first quaratic expression." << endl;
createQuad(eq1);
cout << "Enter a value for x: ";
cin >> x;
cout << "Evaluation of the quadratic expression is: " << eq1.evalQuadX(x) << endl;
//Create second quadratic to add with first
cout << " Input the values for the second quadratic expression to add to the first." << endl;
createQuad(eq2);
eq1a2 = eq1 + eq2;
cout << "Added quadratic expression is: " << eq1a2 << endl;
cout << "Enter a value for x: ";
cin >> x;
cout << "Evaluation of the quadratic expression is: " << eq1a2.evalQuadX(x) << endl;
//Create third quadratic to multiply with first
cout << " Input the multiplier value for the third quadratic expression to multiply with the first: ";
cin >> multiplier;
eq1m = multiplier * eq1;
cout << "Multiplied quadratic expression is: " << eq1m << endl;
cout << "Enter a value for x: ";
cin >> x;
cout << "Evaluation of the quadratic expression is: " << eq1m.evalQuadX(x) << endl;
return EXIT_SUCCESS;
}
---------------------------------------------------
Output
Input the values for the first quaratic expression.
a: 3.2
b: 5.5
c: 1.7
The quadratic expression is:
3.2X^2+ 5.5X+ 1.7
The quadratic expression is:
3.2X^2+ 5.5X+ 1.7
Enter a value for x: 2
Evaluation of the quadratic expression is: 25.5
Input the values for the second quadratic expression
to add to the first.
a: 1.8
b: 5
c: 3.2
The quadratic expression is:
1.8X^2+ 5X+ 3.2
The quadratic expression is:
1.8X^2+ 5X+ 3.2
Added quadratic expression is: 5X^2+ 10.5X+ 4.9
Enter a value for x: 3.5
Evaluation of the quadratic expression is: 102.9
Input the multiplier value for the third quadratic expression
to multiply with the first: 7.7
Multiplied quadratic expression is: 24.64X^2+ 42.35X+ 13.09
Enter a value for x: 8.1
Evaluation of the quadratic expression is: 1972.76
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.