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;

}

Better try another approach. See your problem area. You have Lines (defined here by a linear equation, but Line is not Equation only) and Points (defined by its coorinates). Well, let's declare Line and Point classes!

The Point is a simple pair of doubles. Let

struct Point // No need to protect coordinates, a class with public members is a stucture.
// Of course, you may use class, not a structure form, but why?..
{
   double x, y;
   // It's useful to add Point constructor.
   Point(double xval, double yval): x(xval), y(yval) {}
   // And redefine default constructor as (0,0) point:
   Point():x(0.0), y(0.0) {}
   // Let Point is capable to print himself.
   void print() const { cout << "(" << x << "," << y << ")"; }
};

Next step: declare Line:

class Line
{
// Better start with public members:
public:
   // Redefine default constructor:
   Line(): A(0.0), B(0,0), C(0.0) {} // It's not a true line!
   Line(double a, double b, double c): A(a), B(b), C(c) {}
   //  Print himself...
   void print() const { cout << A << "*x+" << B << "*y+" << C << "=0"; }
   // Invent other public member functions later...
   ...
private: // or protected
   double A, B, C;
};

See your task specifications. You must calculate line equation coefficients by two points.
OK, let's add member functions:

... // Line declaration here  
  // Think about better name...
  void set(const Point& p1, const Point& p2);
...
};
...
void Line::set(const Point& p1, const Point& p2)
{
    // calculate and set A, B, C members
    A = p2.y - p1.y;
    ... // your homework here...
}

Let's try this stuff:

// main() body here
Point p1(3,4);
Point p2(5,6);
Line line(p1,p2);

line.print();
cout << endl; // Flush output with newline

It's in the bag. All done.

In actual fact we want to expand Line interface. For example, if A == 0 and B == 0 then we have bad line (it's not a line). It's a good idea to print a proper message instead of senseless 0*x+0*y+other=0. Fortunately, we can achieve that: modify print() meber function - that's all. May be, better separate this function declaration and definition. More better: declare totally new member function like this:

...
std::string Line::toString() const
{
    char abcbuf[128]; // or 256, or ... select a proper room for...
    ::sprintf(abcbuf,"%g*x+%g*y+%g=0",A,B,C);
    return abcbuf; // don't worry: new std::string object will be returned
}

With Point and Line classes we have a solid base to attack new problem areas. Oh, mellfluous word reusability...

Good luck!

Oh, add once more constructor to Line:

Line::Line(const Point& p1, const Point& p2)
{
   set(p1,p2);
}

or rewrite main part of my post above:

...
Line line;

line.set(p1,p2);
...
This article has been dead for over six months. Start a new discussion instead.