Declare and implement a class called Linear that is an abstraction of a linear e
ID: 3548704 • Letter: D
Question
Declare and implement a class called Linear that is an abstraction of a linear equation
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:
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.