Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Math background: The standard equation of a line is: ax+by=c . Where the coe

ID: 3822496 • Letter: C

Question

C++

Math background: The standard equation of a line is: ax+by=c . Where the coefficients a, b, and c are elements of the real numbers. For coefficients a and b, one, but not both may be zero. Question: For this project, your task is to implement two operators: assignment (=) and extraction (>>). An initial class declaration and partial definition are provided alongside a test program. All of which are contained in lab15.cpp available on blackboard.
Within the the file, the class lineType has been declared but not fully implemented. What remains is the completion of the assignment and extraction operators.
Assignment (=) The purpose of the assignment operator is to copy the state of one lineType instance to another. For example: lineType a(1, 3, 5); lineType b; b = a; cout << b << endl; Produces the output: 1x + 3y = 5


Extraction (>>) The purpose of the extraction operator is to convert the text input from a user to the proper state of a lineType. The user is expected to enter values as (<XCOEFF>, <YCOEFF>, <CONST>). As an example: lineType a; cout << “Enter a line (coeff, coeff, const): “; cin >> a; cout << a << endl; With user input (2, 1, 10) Produces the output: Enter a line (coeff, coeff, const): (2, 1, 10) 2x + 1y = 10 Output: When implemented correctly, the program should produce the following output given the user input of (2, 3, 4).

Output:

Line1: 2x + 3y = 4

Line2: 3x + 5y = 7

Line3: 2x + 3y = -2

Line4: 3x - 2y = 1

Input line5 int the form (a,b,c):(2,3,4)

line5:2x + 3y = 4

line 5 has been copied to line 6 by the assigment operator

Line6: 2x + 3y = 4

Press any key to continue...

Lab15.cpp


#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

class lineType
{
//Overload the stream extraction operators
friend ostream& operator<<(ostream&, const lineType&);
friend istream& operator>>(istream&, lineType&);

public:
const lineType& operator=(const lineType&);
//overload the assignment operator

void setLine(double a = 0, double b = 0, double c = 0);
//Function to set the line.

double getXCoefficient() const;
double getYCoefficient() const;
double getCOnstantTerm() const;

void setXCoefficient(double coeff);
void setYCoefficient(double coeff);
void setConstantTerm(double c);

lineType(double a = 0, double b = 0, double c = 0);
//Constructor

private:
double xCoeff;
double yCoeff;
double constTerm;
};

void lineType::setLine(double a, double b, double c)
{
xCoeff = a;
yCoeff = b;

if (a == 0 && b == 0)
  constTerm = 0;
else
  constTerm = c;
}

double lineType::getXCoefficient() const
{
return xCoeff;
}

double lineType::getYCoefficient() const
{
return yCoeff;
}

double lineType::getCOnstantTerm() const
{
return constTerm;
}

void lineType::setXCoefficient(double coeff)
{
xCoeff = coeff;
}

void lineType::setYCoefficient(double coeff)
{
yCoeff = coeff;
}

void lineType::setConstantTerm(double c)
{
constTerm = c;
}

lineType::lineType(double a, double b, double c)
{
setLine(a, b, c);
}

ostream& operator<<(ostream& osObject,const lineType& line)
{
if (line.xCoeff != 0)
  osObject << line.xCoeff << "x ";

if (line.yCoeff != 0)
{
  if (line.yCoeff < 0)
   osObject << "- ";
  else
   osObject << "+ ";
  osObject << fabs(line.yCoeff) << "y ";
}

osObject << " = " << line.constTerm;

return osObject;
}

/*
* Implement the following operators
*/
const lineType& lineType::operator=(const lineType& otherLine)
{
//Complete me
}

istream& operator>>(istream& isObject, lineType& line)
{
//Complete me
}
//----------------------DriverProgram-----------------------
int main()
{
lineType line1(2, 3, 4);
lineType line2(3, 5, 7);
lineType line3(2, 3, -2);
lineType line4(3, -2, 1);

lineType line5;
lineType line6;

cout << "line1: " << line1 << endl;
cout << "line2: " << line2 << endl;
cout << "line3: " << line3 << endl;
cout << "line4: " << line4 << endl;
  

cout << "Input line5 in the form (a, b, c): ";
cin >> line5;
cout << "line5: " << line5 << endl;


cout << "line5 has been copied to line6 by the assignment operator" << endl;
line6 = line5;
cout << "line6: " << line6 << endl;
return 0;
}

Explanation / Answer

Code:

#include <iostream>
#include <cmath>
#include <iomanip>
#include<cstdio>
using namespace std;
class lineType
{
//Overload the stream extraction operators
friend ostream& operator<<(ostream&, const lineType&);
friend istream& operator>>(istream&, lineType&);
public:
const lineType& operator=(const lineType&);
//overload the assignment operator
void setLine(double a = 0, double b = 0, double c = 0);
//Function to set the line.
double getXCoefficient() const;
double getYCoefficient() const;
double getCOnstantTerm() const;
void setXCoefficient(double coeff);
void setYCoefficient(double coeff);
void setConstantTerm(double c);

lineType(double a = 0, double b = 0, double c = 0);
//Constructor
private:
double xCoeff;
double yCoeff;
double constTerm;
};
void lineType::setLine(double a, double b, double c)
{
xCoeff = a;
yCoeff = b;
if (a == 0 && b == 0)
constTerm = 0;
else
constTerm = c;
}
double lineType::getXCoefficient() const
{
return xCoeff;
}
double lineType::getYCoefficient() const
{
return yCoeff;
}
double lineType::getCOnstantTerm() const
{
return constTerm;
}
void lineType::setXCoefficient(double coeff)
{
xCoeff = coeff;
}
void lineType::setYCoefficient(double coeff)
{
yCoeff = coeff;
}
void lineType::setConstantTerm(double c)
{
constTerm = c;
}
lineType::lineType(double a, double b, double c)
{
setLine(a, b, c);
}
ostream& operator<<(ostream& osObject,const lineType& line)
{
if (line.xCoeff != 0)
osObject << line.xCoeff << "x ";
if (line.yCoeff != 0)
{
if (line.yCoeff < 0)
osObject << "- ";
else
osObject << "+ ";
osObject << fabs(line.yCoeff) << "y ";
}
osObject << " = " << line.constTerm;
return osObject;
}
/*
* Implement the following operators
*/
const lineType& lineType::operator=(const lineType& otherLine)
{
//Complete me
xCoeff = otherLine.getXCoefficient();
yCoeff = otherLine.getYCoefficient();
constTerm = otherLine.getCOnstantTerm();
}
istream& operator>>(istream& isObject, lineType& line)
{
double x,y,c;
char ch;
//here ch is used to hold ( , and ) characters
cin>>ch>>x>>ch>>y>>ch>>c>>ch;

line.setLine(x,y,c);
}
//----------------------DriverProgram-----------------------
int main()
{
lineType line1(2, 3, 4);
lineType line2(3, 5, 7);
lineType line3(2, 3, -2);
lineType line4(3, -2, 1);

lineType line5;
lineType line6;
cout << "line1: " << line1 << endl;
cout << "line2: " << line2 << endl;
cout << "line3: " << line3 << endl;
cout << "line4: " << line4 << endl;
  
cout << "Input line5 in the form (a, b, c): ";
cin >> line5;
cout << "line5: " << line5 << endl;


cout << "line5 has been copied to line6 by the assignment operator" << endl;
line6 = line5;
cout << "line6: " << line6 << endl;
cout<<"Press any key to continue"<<endl;
//If you are on windows you can use getch(); to wait
return 0;
}

Output:

line1: 2x + 3y = 4
line2: 3x + 5y = 7
line3: 2x + 3y = -2
line4: 3x - 2y = 1
Input line5 in the form (a, b, c): (2,3,4)
line5: 2x + 3y = 4
line5 has been copied to line6 by the assignment operator
line6: 2x + 3y = 4
Press any key to continue

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote