hey guys, I have an assignment that has the following requirements:
1. Write a class Line which represents a line in 2D
2. Two constructors: one with slope and intercept
one with (x1, y1) (x2, y2)
3. Member function findY(double) which for a given line
takes an x-value and returns the y-value
4. Write short main program which exercises the class and
show output
Below is what I did...I would appreciate any suggestions or comments about code, and if you think this is enough for the given requirements. Thanks,
axon
Code:
/* Author:
* Class: CS201
* Date: 09.08.03
* Assgn: Homework 0
* System: g++ and MSVC++
*/
#include <iostream>
using namespace std;
class Line
{
public:
//constructors
Line( double newSlope, double newIntercept );
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;
};
//--------------------------------------
int main()
{
double x = 0;
double y = 0;
Line slopeAndIntercept(1, 2), coordinates(2, 4, 3, -1);
slopeAndIntercept.output1();
coordinates.output2();
x = 12;
cout << "\nCalculating y-coordinate when x = 12: " << endl;
y = slopeAndIntercept.findY(x);
cout << "When x = 12, y = " << y << endl;
return 0;
}
//-----------------------------------
//defining functions and constructors
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)
{
return (slope*x + intercept);
}