CS 161 Proficiency Exam Choose one of the following problems must have separate
ID: 3892735 • Letter: C
Question
CS 161 Proficiency Exam Choose one of the following problems must have separate interface (.hpp) and implementation (cpp) files, with no Tinline" function definitions. All data members must be private. A (univariate) quadratic function Quadratic that ha c (you may use single-letter names for these, since it's a well-known math function). The class should have the following methods: has the form ax2+ bx + C. Write a class named s three double data members, one for each of the coefficients a, b and get methods for each data member a default constructor that initializes each coefficient to 1.0 a constructor that takes three parameters and uses them to initialize the data members. a method named valueFor that takes as a double parameter the value to use for x. The method should return the value of the Quadratic (ax?+ bx+ c) given that value of x. a method called calcDiscriminant that takes no parameters and returns the discriminant of the Quadratic. The expression b-4ac gives the discriminant of a quadratic equation. . . demonstrates the functionality of your Quadratic class (it Write a main function that doesn't have to demonstrate every get method, but should demonstrate at least one).Explanation / Answer
//Quadratic.h
#pragma once
class Quadratic
{
double a, b, c;
public:
Quadratic();
Quadratic(double coe1, double coe2, double coe3);
double valueFor(int xVal);
double calcDiscriminant();
double getA();
double getB();
double getC();
};
---------------------------------------
//Quadratic.cpp
#include"Quadratic.h"
Quadratic::Quadratic()
{
a = 1;
b = 1;
c = 1;
}
Quadratic::Quadratic(double coe1, double coe2, double coe3)
{
a = coe1;
b = coe2;
c = coe3;
}
double Quadratic::valueFor(int xVal)
{
double result;
result = a * xVal*xVal + b * xVal + c;
return result;
}
double Quadratic::calcDiscriminant()
{
double discriminant;
discriminant = b * b - 4 * a*c;
return discriminant;
}
double Quadratic::getA()
{
return a;
}
double Quadratic::getB()
{
return b;
}
double Quadratic::getC()
{
return c;
}
----------------------------------------------------------------------
//Main.cpp
#include<iostream>
#include"Quadratic.h"
using namespace std;
int main()
{
int a=2, b=3, c=4;
Quadratic q(a, b, c),q1;
cout<<"Value of Qudratic equation q: "<<q.valueFor(2)<<endl;
cout<<"Value of quadratic equation q1: "<<q1.valueFor(-1)<<endl;
cout << "Discriminant of q: " << q.calcDiscriminant() << endl;
cout << "Discriminant of q1: " << q1.calcDiscriminant() << endl;
//check get functions of coefficients a,b ,c
cout << "Coefficients of a , b and c are : " << q.getA() << " , " << q.getB() << " and " << q.getC() << endl;
}
/*output:
Value of Qudratic equation q: 18
Value of quadratic equation q1: 1
Discriminant of q: -23
Discriminant of q1: -3
Coefficients of a , b and c are : 2 , 3 and 4
*/