63 036
Assignments Done
99,1%
Successfully Done
In July 2018

Answer to Question #57543 in C++ for jonney

Question #57543
Design a class Line that implements a line, which is represented by the formula y = ax+b. Your class should store a and b as double member variables.
Write a member function intersect(L) that returns the x coordinate
at which this line intersects line L. If the two lines are parallel, then your
function should return a message " Parallel" and exit. Write a C++ program that creates a number of Line objects and tests each pair for intersection. Your program should print an appropriate error message for parallel lines.
Expert's answer
#include <iostream>

using namespace std;

class Line
{

private:
double a, b;
public:
Line(double a, double b){
this->a = a;
this->b = b;
}

double intersect(Line L)
{
if(this->a == L.a){
throw "Parallel";
}
return (L.b - this->b) / (this->a - L.a);
}
};

int main() {
Line line1(0, 0);
Line line2(1, 1);
Line line3(2, 2);
Line line4(2, -2);
Line array[] = {line1, line2, line3, line4};
int n = sizeof(array) / sizeof(*array);

for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
try{
if(i != j){
cout << array[i].intersect(array[j]) << endl;
}
}catch(const char * s){
cout << "Error! " << s << endl;
}
}
}
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions