Hi,
I'm a new C++ programming student and we just learned about classes and objects. I completed the assignment but had a question about doing something extra with my code. The instructions given for the problem were: To use the formula for a straight line (i.e. Ax + By + C = 0) First to create a class for the formula which I did and I named the "Equation" The class has 3 methods and one 4 data members. The data members rep. the two points on a line (x1,y1) and (x2, y2) For example (3,4) and (5,6)Where 3 is x1, 4 is y1, 5 is x2, and 6 y2.
I've used overload constructors as I was taught (previously referred to as convert constructors). Directions also say that the two input points can be represented as a,b and c,d. Then the coefficients A, B and, C in the formula above are equal to the following assignments. A = d - b, B = a - c, and C = b*c - a*d. We are allowed to just print out each of the three calculations separately for A, B, and C, which is what my program does now. But...I'd like to get some help and know how I could print everything out just as a single formula for example only Ax + By + C = 0?? My first thought was I could put everything into a character array or string? But being this is my first program using classes and objects I'm uncertain of how to do all of this. Sorry for such a long description and I apologize if I included to much or too little information. My code is below. Thank you for any advice or tips anyone can provide to get me going in the right direction.
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
class Equation{//Declare Class
private://Data Members
double x1, x2, y1, y2;
public://Methods
double CalculateA(double d, double b)//Overload Constructor
{y2 = d; y1 = b; return y2 - y1;}
double CalculateB(double c, double a)
{x2 = c; x1 = a; return x2 - x1;}
double CalculateC(double a, double b, double c, double d)
{x1 = a; y1 = b; x2 = c; y2 = d; return (y1 * x2) - (x1 * y2);}
};
int main(){
double e,f,g,h;
Equation Formula;//Declare Formula Object
cout << " Enter two sets of coordinates " << endl;
cin >> e >> f;
cin >> g >> h;
if(!cin.eof() && cin.good()){//System Test
cout << "Coordinate 1 is " << Formula.CalculateA(h,f) << endl;//Prints out Point 1
cout << "Coordinate 2 is " << Formula.CalculateB(g,e) << endl;//Prints out Point 2
cout << "Slope of the Line is " << Formula.CalculateC(e,f,g,h) << endl;//Prints out Formula
}
else
{
cout << " Invalid data entered " << endl;
}
return 0;
}