I'm having a problem with the thirs case of my program...I'm trying to get a value for y only from two sets of coordinates. This works fine in the first two cases since I'm calling findY from slopeAndIntercept...but when I get into the third case my constructors get mixed up...any help?
Code:
// THIS IS THE .H FILE
#include <iostream>
using namespace std;
//File: Line.h
class Line
{
public:
//constructors
Line( double newSlope = 0, double newIntercept = 0);
Line( double newx1, double newy1, double newx2, double newy2 );
void output1(); //output for slope and y-intercept
void output2(); //output for the coordinates
double findY(double x); // find y-coord from given x-coord
private:
double slope;
double intercept;
double x1, y1, x2, y2;
};
#include "line.cpp"
Code:
//LINE .CPP
Line::Line( double newSlope, double newIntercept )
: slope(newSlope), intercept(newIntercept){}
Line::Line( double newx1, double newy1, double newx2, double newy2 )
: x1(newx1), y1(newy1), x2(newx2), y2(newy2){}
void Line::output1(){
cout << "Initializing slope of line to: " << slope << endl;
cout << "Initializing y-intercept to: " << intercept << endl;
}
void Line::output2(){
cout << "\n( x1, y1 ) = ( " << x1 << ", " << y1 << " )" << endl;
cout << "( x2, y2 ) = ( " << x2 << ", " << y2 << " )" << endl;
}
double Line::findY(double x)
{
if ( (slope != 0) && (intercept != 0) ){
return (slope*x + intercept);
} else {
slope = (y2 - y1)/(x2 - x1);
intercept = (y1 - slope*(x1));
return (slope*x + intercept);
}
}
Code:
//MAIN.CPP
#include "Line.h"
int main()
{
double x = 0;
double y = 0;
cout << "\n----------<Case1>---------\n" << endl;
Line slopeAndIntercept(1, 2), coordinates(2, 4, 3, -1);
slopeAndIntercept.output1();
coordinates.output2();
x = 12;
cout << "\nCalculating y-coordinate when x = " << x << endl;
y = slopeAndIntercept.findY(x);
cout << "When x = " << x << " , y = " << y << endl;
cout << "\n----------<Case2>---------\n" << endl;
Line slopeAndIntercept1(4, -1), coordinates1(0, 0, 0, 0);
slopeAndIntercept1.output1();
x = 7;
cout << "\nCalculating y-coordinate when x = " << x << endl;
y = slopeAndIntercept1.findY(x);
cout << "When x = " << x << " , y = " << y << endl;
cout << "\n----------<Case3>---------\n" << endl;
Line slopeAndIntercept2(0, 0),coordinates2(0, 0, 1, 1);
coordinates2.output2();
x = 2;
cout << "\nCalculating y-coordinate when x = " << x << endl;
y = coordinates2.findY(x);
cout << "When x = " << x << " , y = " << y << endl;
return 0;
}
thanks, axon