Question: CODE IN C++: You are to design a class Line that implements a line in the plane, which is represented by the formula y =
CODE IN C++:
You are to design a class Line that implements a line in the plane, which is represented by the formula y = ax+b. Your class should store a and b as double member variables and write a member function intersect() such that L.intersect(K) returns the x coordinate of the unique point at which lines L and K intersect is such a point exists. If there is not a unique such point, the function should throw an appropriate exception.
The two possible exceptional situations are where the two lines
are the same and hence have an infinite number of common points; or
are parallel and hence have no point of intersection.
For this, you will need subclasses EqualLines and ParallelLiines of the builtin exception class (see the description of custom classe in the slides). The what() method of
EqualLines should provide the string "The lines are equal: infinite intersection" and ParallelLiines should provide the string "The lines are parallel: no intersection" When either exception is caught, the message should be printed. This will occur in the program that uses the Line class.
You will be provided two files: line.h containing the declarations for all the needed classes; and a partially completed file line.cpp where you should provide the implementation of the Line class. You are to alter only the Line.cpp file.
In a separate file, you should create a test program that creates a number of Line objects and tests each pair for intersection. You should use this program to test your classes.
line.h
#include
#include
using namespace std;
class EqualLines: public exception
{
};
class ParallelLines: public exception
{
public:
};
class Line {
public:
Line(double slope, double y_intercept): a(slope), b(y_intercept) {};
double intersect(const Line L) const;
double getSlope() const {return a;};
double getIntercept() const {return b;};
// return the y-coordinate of the point with x-coordinate z:
double get_y(double z) const;
private:
double a;
double b;
};
line.cpp
#include
#include "line.h"
double Line::intersect(const Line L) const
{
} // end of intersect function
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
