Question

C++, MUST Include comments and follow instructions. I will downvote if specifications are not met or...

C++, MUST Include comments and follow instructions. I will downvote if specifications are not met or the answer is copy and pasted from another expert

A (univariate) quadratic function has the form ax2+ bx + c.  Write a class named Quadratic that has three double data members, one for each of the coefficients a, b and c (you may use single-letter names for these, since it’s a well-known math function).  The class should have the following methods:

  • a default constructor that initializes each coefficient to 1.0
  • a constructor that takes three parameters and uses them to initialize the data members.
  • set methods for each data member: setA, setB and setC.
  • 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 (ax2+ bx + c) given that value of x.
  • a method called calcDiscriminant that takes no parameters and returns the discriminant of the Quadratic. The expression b2– 4ac gives the discriminant of a quadratic equation.

Write a function (not part of the Quadratic class) named meanValueFor that takes two parameters: a vector of pointers to Quadratics and a value to use for x. It should return the average value of all the Quadratics pointed to by the vector's elements, using the second parameter as the value of x for each Quadratic. This function should be in a separate file, which will need to #include Quadratic.hpp.

Here's a simple example of how your class and function could be used:

Quadratic q1;
Quadratic q2;
std::vector<Quadratic*> vec;
vec.push_back(&q1);
vec.push_back(&q2);
double result = meanValueFor(vec, 1.0);

The files must be named: Quadratic.hpp, Quadratic.cpp, meanValueFor.cpp

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code Image:

Sample Output:

Code to Copy:

Quadratic.hpp:

#pragma once

//Quadratic.hpp

//Implementation of Quardratic class

class Quadratic {

private:

     //Declare the variable a,b,c as

     //type of double

     double a, b, c;

public:

     //Declaration of default constructor

     Quadratic();

     //Declaration of Parmaeterized constructor

     Quadratic(double a, double b, double c);

     //Declaraion of setA function

     void setA(double a);

     //Declaraion of setB function

     void setB(double b);

     //Declaraion of setC function

     void setC(double c);

     //Declaration of valueFor function

     double valueFor(double x);

     //Declaration of calcDiscriminant function

     double calcDiscriminant();

};

Quadratic.cpp:

//Quadratic.cpp

#include<iostream>

#include"Quadratic.hpp"

using namespace std;

//Implementation default constructor

//assign 1.0 to a,b,c

Quadratic::Quadratic() {

     a = 1.0;

     b = 1.0;

     c = 1.0;

}

//Implementation of parameterized constructor with

//parameters a,b,c as type of double

Quadratic::Quadratic(double a, double b, double c) {

     this->a = a;

     this->b = b;

     this->c = c;

}

//Implementation of setA function

void Quadratic::setA(double a) {

     this->a = a;

}

//Implementation of setB function

void Quadratic::setB(double b) {

     this->b = b;

}

//Implementation of setC function

void Quadratic::setC(double c) {

     this->c = c;

}

//Implementation of valueFor function

//that find Quadratic function value for given parameter value x     

double Quadratic::valueFor(double x)

{

     return a*x*x + b*x + c;

}

//Implementation of calcDiscriminant function

//to find Discriminate of Quadratic function

double Quadratic::calcDiscriminant() {

     return b*b - 4 * a*c;

}

meanValueFor.cpp:

// meanValueFor.cpp : Defines the entry point for the console application.

//

//meanValueFor.cpp

#include"Quadratic.cpp"

#include<vector>

//Implementation of meanValueFor function

double meanValueFor(vector<Quadratic*>vec, double x)

{

     //Declare sum as initalize to 0

     double sumofValues = 0;

     //Declare k as integer and assign 0    

     int k = 0;

     //Iterate the loop to calculate

     //the sum of all Quadratic function values for given x

     while (k < vec.size())

     {

          //calculate the sum

          sumofValues = sumofValues + (*vec[k]).valueFor(x);

          //Increment the x

          k++;

     }

     //return the value of sum/vec.size()

     return sumofValues / vec.size();

}

//Implementation of main function

int main() {

     //Declarate q1 object for Quardratic class

     Quadratic q1;

     //Declarate q2 object for Quardratic class

     Quadratic q2;

     //set the A value for through object q2

     q2.setA(6);

     //set the B value for through object q2

     q2.setB(4);

     //set the C value for through object q2

     q2.setC(2);

     //Declared a vector to store all Quadratic functions

     std::vector<Quadratic*> vec;

     //push object q1 to vector vec

     vec.push_back(&q1);

     //push object q2 to vector vec

     vec.push_back(&q2);

     //store the return value of meanValueFor function

     double result = meanValueFor(vec, 1.0);

     //Display statement

     cout << result << "\n";

     return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++, MUST Include comments and follow instructions. I will downvote if specifications are not met or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ For this program you are going to create a class that helps to solve...

    Using C++ For this program you are going to create a class that helps to solve the quadratic equation. The equation has the following form: ax2 + bx + c = 0 The roots of the equation are: x1 = -b + sqrt(b2 - 4 * a * c) and x2 = -b - sqrt(b2 - 4 * a * c) The phrase in the parenthesis is called the discriminant. If the value of the discriminant is positive, the equation...

  • This is the code I wrote for myprogramminglab It works for 8 out of 9 inputs they use and I...

    Write a full class definition for a class named GasTank , and containing the following members:A data member named amount of type double.A constructor that no parameters. The constructor initializes the data member amount to 0.A function named addGas that accepts a parameter of type double . The value of the amount instance variable is increased by the value of the parameter.A function named useGas that accepts a parameter of type double . The value of the amount data member...

  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...

    Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following: 1. h 2. e 3. l 4. l 5. o Q2. Write a recursive function in C++ int sumArray(const int* arr, unsigned int size) { ... } which takes an array of integers,...

  • Write a class named HalfOpenCylinder that has two data members: a double for the height in inches and a double for the radius in inches. It should have a constructor that takes two parameters and uses...

    Write a class named HalfOpenCylinder that has two data members: a double for the height in inches and a double for the radius in inches. It should have a constructor that takes two parameters and uses them to initialize the data members. It should have a default constructor that initializes the height to 10 and the radius to 2. It should have a method named surfaceArea that returns the the surface area (in square inches) of a cylinder with that...

  • The class declaration (interface) and the function definitions (implementation) must be in separate files - the...

    The class declaration (interface) and the function definitions (implementation) must be in separate files - the interface or "header" file has a .hpp extension and the implementation has a .cpp extension. As usual, all data members should be private. Write a class called BankAccount that has: a string data member called customerName, a string data member called customerID, and a double data member called customerBalance a constructor that takes two strings and a double (name, ID, balance) and uses them...

  • put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following:...

    put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following: A private double 1D array instance variable named numbers. A constructor that takes one parameter, a 1D double array. The constructor should throw an IllegalArgumentException with the message "Array length of zero" if the parameter has a length of 0. If the exception is not thrown, the constructor should create the numbers array and copy every element from the parameter into the numbers instance...

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT