69 668
Assignments Done
Successfully Done
In February 2019

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

double a, b;
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++){
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!


No comments. Be first!

Leave a comment

Ask Your question

Privacy policy Terms and Conditions