C++ : Design a class named LinearEquation for a 2 X 2 system of linear equations
ID: 3770010 • Letter: C
Question
C++: Design a class named LinearEquation for a 2 X 2 system of linear equations:
ax + by = e
cx + dy = f
x = (ed - bf)/(ad-bc)
y = (af - ec)/(ad-bc)
The class contains:
- Private data fields a, b, c, d, e, and f.
- A constructor with the arguments for a, b, c, d, e, and f.
- Six get functions for a, b, c, d, e, and f.
- A function named isSolvable() that returns true if ad - bc is not 0.
- Functions getX() and getY() that return the solution for the equation.
Write a test program that prompts the user to enter a, b, c, d, e, and f and displays the result. If ad - bc is 0, report that the equation has no solution
Explanation / Answer
Here is the code for you. If you need any further refinements, just get back to me.
#include <iostream>
using namespace std;
class LinearEquation
{
int a, b, c, d, e, f;
public:
LinearEquation(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6)
{
a = arg1;
b = arg2;
c = arg3;
d = arg4;
e = arg5;
f = arg6;
}
int getA() { return a; } //Accessors for the private variables.
int getB() { return b; }
int getC() { return c; }
int getD() { return d; }
int getE() { return e; }
int getF() { return f; }
bool isSolvable() //Function to check whether the equation is solvable or not, based on the values.
{
if(getA()*getD() - getB()*getC() == 0)
return false;
return true;
}
double getX() //Function to calculate the value of X as per the given equation.
{
return (double)(getE()*getD() - getB()*getF()) / (getA()*getD() - getB()*getC());
}
double getY() //Function to calculate the value of Y as per the given equation.
{
return (double)(getA()*getF() - getE()*getC()) / (getA()*getD() - getB()*getC());
}
};
int main()
{
int a, b, c, d, e, f;
cout<<"Enter the value of a: "; //Reads the values for a, b, c, d, e, f.
cin>>a;
cout<<"Enter the value of b: ";
cin>>b;
cout<<"Enter the value of c: ";
cin>>c;
cout<<"Enter the value of d: ";
cin>>d;
cout<<"Enter the value of e: ";
cin>>e;
cout<<"Enter the value of f: ";
cin>>f;
LinearEquation le (a, b, c, d, e, f); //Creates an object of type LinearEquation.
if(le.isSolvable()) //If the equation is solvable.
cout<<"For the given values, X = "<<le.getX()<<" Y = "<<le.getY()<<endl; //Print the values of X and Y.
else
cout<<"The equation is not solvable."<<endl; //Else, display a message, that its not solvable.
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.