C++ PROGRAMMING please help within an hour so i can give 5 stars :-) (looks hard
ID: 3822440 • Letter: C
Question
C++ PROGRAMMING
please help within an hour so i can give 5 stars :-)
(looks hard but its easy only a couple lines of code need to be implemented)
Related math background: The equation of a line: ax+by c is the standard equation for a line, where a and b cannot both be zero, and where a, b, and c are real numbers. Question Download lab15.cpp from the blackboard. In this file, the definition of the class lineType has given which represents a line equation. In this lab, you are asked to complete the body for two operators overloading function as following: 1) overload the stream extraction operator for the input stream. The user input should be in (a,b,c format where Xcoef a, Ycoef b and ConstTerm c variables for the line object. For example, the code cin line: should accept (2,3,4) as the line input format. 2) overload the assignment operator to assign a line object into another line object. It will copy all member variables from its right operand to its left operand For example, linel line2; will copy all member variables from line2 to the linel object Then, without changing the main function, execute your program to get the same output. Submit a single cpp file.Explanation / Answer
#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;
};
//--------------------------------functions implementation---------------------------------------
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);
}
const lineType& lineType::operator=(const lineType& otherLine)
{
xCoeff = otherLine.xCoeff;
yCoeff = otherLine.yCoeff;
constTerm = otherLine.constTerm;
return *this;
}
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;
}
istream& operator>>(istream& isObject, lineType& line)
{
isObject>>line.xCoeff>>line.yCoeff>>line.constTerm;
}
//----------------------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;
}
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): line5: 2x + 3y = 4
line5 has been copied to line6 by the assignment operator
line6: 2x + 3y = 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.