C++: Can anyone separate this code into separate header and implentation files ?
ID: 3770135 • Letter: C
Question
C++: Can anyone separate this code into separate header and implentation files?
#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; }
int getB() { return b; }
int getC() { return c; }
int getD() { return d; }
int getE() { return e; }
int getF() { return f; }
bool isSolvable()
{
if(getA()*getD() - getB()*getC() == 0)
return false;
return true;
}
double getX()
{
return (double)(getE()*getD() - getB()*getF()) / (getA()*getD() - getB()*getC());
}
double getY()
{
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: ";
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);
if(le.isSolvable())
cout<<"For the given values, X = "<<le.getX()<<" Y = "<<le.getY()<<endl;
else
cout<<"The equation is not solvable."<<endl;
}
Explanation / Answer
See the below code after seperating header file from source file has becomes like this. Generally header file only contains the declarations not defination.
Header File:
// Linearequation.h
#ifndef LINEAREQUATION_H
#define LINEAREQUATION_H
class LinearEquation
{
int a, b, c, d, e, f;
public:
LinearEquation(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6);
int getA();
int getB();
int getC();
int getD();
int getE();
int getF();
bool isSolvable();
double getX();
double getY();
};
#endif
Code:
#include <iostream>
using namespace std;
#include "LinearEquation.h"
LinearEquation::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 LinearEquation::getA() { return a; }
int LinearEquation::getB() { return b; }
int LinearEquation::getC() { return c; }
int LinearEquation::getD() { return d; }
int LinearEquation::getE() { return e; }
int LinearEquation::getF() { return f; }
bool LinearEquation::isSolvable()
{
if(getA()*getD() - getB()*getC() == 0)
return false;
return true;
}
double LinearEquation::getX()
{
return (double)(getE()*getD() - getB()*getF()) / (getA()*getD() - getB()*getC());
}
double LinearEquation::getY()
{
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: ";
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);
if(le.isSolvable())
cout<<"For the given values, X = "<<le.getX()<<" Y = "<<le.getY()<<endl;
else
cout<<"The equation is not solvable."<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.