Question
A straight line is expressed by the equation, where is the slope of the line and is the -intercept, i.e. the coordinate of the location
A straight line is expressed by the equation, where is the slope of the line and is the -intercept, i.e. the coordinate of the location where the line crosses the -axis (see Figure 1) Define a class named Line whose attributes and to model a line. Use inline style to define the class. Use the provided template file, main.epp to write your code. The requirements for the program are as follows:
1 Define a constructor that accepts four parameters which are , and , representing the coordinates and of two points that the line passes through. The slope and -intercept of the line are then calculated by:
Declare this constructor such that it can also serve as a default constructor. In this case, it will model a line that passes through the points and , which should result in and .
Sol195:
Here is the implementation of the Line class with the constructor that meets the requirements mentioned:
class Line {
private:
double m, c; // slope and y-intercept
public:
// Constructor that accepts two points (default is (0,0) and (1,1))
Line(double x1=0, double y1=0, double x2=1, double y2=1) {
m = (y2 - y1) / (x2 - x1); // calculate slope
c = y1 - m*x1; // calculate y-intercept
}
// Getters for slope and y-intercept
double getSlope() const {
return m;
}
double getYIntercept() const {
return c;
}
};
This class has a constructor that accepts four parameters, but with default values set to (0,0) and (1,1), it can serve as a default constructor as well. The slope and y-intercept are calculated in the constructor using the provided formula. The class also has getters for the slope and y-intercept.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started