A linear equation is an equation of the form f(x)=ax+b, where a is the equation\
ID: 3548703 • Letter: A
Question
A linear equation is an equation of the form f(x)=ax+b, where a is the equation's a coefficient and b is the equations b coefficient, and x is a variable. A client should be able to use the class to compute the value of the linear equation for an input value of x, and print the linear equation on the screen. The class will have 2 data members to represent the coefficients a and b, and the following methods.
Write the class declaration and also define all the following methods:
1. Two argument Constructor with default parameters
2. One mutator function, setLinear that takes 2 parameters
3. One acessor function, getLinear that returns the two coefficients a, and b
4. Compute function that returns the computed value from the function, for any input x of type float
5. Print function that prints the linear equation in the form f(x) = 5x+6
Also use appropriate use of the const keyword in member function declaration & implementation, and while passing parameters
Write your program in a SINGLE ".cpp" file. That is you do NOT have to write, separate linear.h and linear.cpp files.
YOUR class should work with the main() program provided below:
// Main program
int main(void)
{
// Create the linear equation f(x) = 5x + 3
Linear f(5.0,3.0);
// Display the equation
cout << "The equation is f(x) = ";
f.print();
cout << endl;
// Compute values of f(x) for x={ 0,1,2,3 }
for (float x = 0.0; x < 4.0; x++)
cout << "The value of f(" << x << ") is "<< f.compute(x) << endl;
return 0;
}
SAMPLE OUTPUT
The equation is f(x) = 5x + 3
The value of f(0) is 3
The value of f(1) is 8
The value of f(2) is 13
The value of f(3) is 18
#include <iostream>
using namespace std;
//Insert your Class definition below
//Implement your Class's Member functions below
int main (void)
{
//create the linear equation f(x) = 5x +3
Linear f(5.0,3.0);
// Display the equation
cout <<"The equation is f(x) = ";
f.print();
cout << endl;
// Compute values of f(x) for x= { 0, 1, 2, 3}
for (float x = 0.0; x < 4.0; x++)
cout << " The value of f (" << x << ") is "<< f.compute(x) << endl;
return 0;
}
Explanation / Answer
#include<iostream>
using namespace std;
class Linear
{
private:
float a,b;
public:
Linear(float,float);
void print();
void setLinear(float x,float y);
void getLinear();
float compute(int x);
};
Linear::Linear(float y=10,float z=20)
{
a=y;
b=z;
}
void Linear::print()
{
cout<<a<<"x + "<<b<<endl;
}
float Linear::compute(int z)
{
return a*z+b;
}
void Linear::setLinear(float x,float y)
{
a = x;
b = y;
}
void Linear::getLinear()
{
cout<<"Coffecients : a = "<<a<<", b = "<<b<<endl;
}
int main(void)
{
// Create the linear equation f(x) = 5x + 3
Linear f(5.0,3.0);
// Display the equation
cout<<"The equation is f(x) = ";
f.print();
cout<<endl;
// Compute values of f(x) for x={ 0,1,2,3 }
for(float x = 0.0; x < 4.0; x++)
cout<<"The value of f("<<x<<") is "<<f.compute(x)<<endl;
f.getLinear();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.