То, что у меня сейчас есть:
lineType &lineType::operator =(lineType& line) ///overloads = so that it can copy data from one line into another
{
if(this == line)
{
///something goes here?
}
return *this;
}
Я понимаю схему, но не понимаю реализацию, я почти уверен, что мне нужно вернуть *this
, но я не понимаю, что предполагается быть. Это все в исходном файле, вот заголовок:
#include <string>
#include <iostream>
#include <iomanip>
class lineType {
public:
void setLine(double a = 0, double b = 0, double c = 0);
// Function to set the line.
void equation() const;
double getXCoefficient() const;
double getYCoefficient() const;
double getCOnstantTerm() const;
void setXCoefficient(double coeff);
void setYCoefficient(double coeff);
void setConstantTerm(double c);
double slope() const;
/// Return the slope. This function does not check if the
/// line is vartical. Because the slope of a vertical line
/// is undefined, before calling this function check if the
/// line is nonvertial.
bool verticalLine() const;
bool horizontalLine() const;
bool equalLines(lineType otherLine) const;
/// Returns true of both lines are the same.
bool parallel(lineType otherLine) const;
/// Function to determine if this line is parallel to otherLine.
bool perpendicular(lineType otherLine) const;
/// Function to determine if this line is perperdicular to otherLine.
void pointOfIntersection(lineType otherLine);
/// If lines intersect, then this function finds the point
/// of intersection.
lineType(double a = 0, double b = 0, double c = 0);
/// Constructor
friend std::iostream& operator >>(std::iostream &in, lineType& line);
friend std::ostream& operator <<(std::ostream &out, const lineType& line);
lineType &operator =(lineType& line); ///work in progress
friend bool operator +(const lineType& line);
friend bool operator -(const lineType& line);
friend bool operator ==(const lineType& line, lineType& otherline);
friend bool operator ||(const lineType& line, lineType& otherline);
friend bool operator&&(const lineType& line, lineType& otherline);
private:
double xCoeff;
double yCoeff;
double constTerm;
double *nums;
bool hasSlope;
};
Остальная часть кода выполняет другие перегрузки, вот код в полном контексте:
#include <cmath>
#include <iomanip>
#include <iostream>
#include "LineType.h"
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; }
double lineType::slope() const { return -xCoeff / yCoeff; }
std::iostream& operator >>(std::iostream& in, lineType& line) ///overloads >> so that line can be input properly
{
double x,y,c;
in >> x;
line.setXCoefficient(x);
in >> y;
line.setYCoefficient(y);
in >> c;
line.setConstantTerm(c);
return in;
}
std::ostream& operator <<(std::ostream &out, const lineType& line) ///Overloads << operator for proper output of line data
{
out << line.getXCoefficient() << "x ";
if (line.getYCoefficient() < 0)
out << "- ";
else
out << "+ ";
out << fabs(line.getYCoefficient()) << "y = " << line.getCOnstantTerm() << std::endl;
}
lineType &lineType::operator =(lineType& line) ///overloads = so that it can copy data from one line into another
{
if(this == line)
{
}
return *this;
}
bool operator +(const lineType& line) ///overrides + so that it returns true if a line is vertical
{
if (line.getYCoefficient() == 0)
return true;
else
return false;
}
bool operator -(const lineType& line) ///overrides - so that it returns true if a line is horizontal
{
if (line.getXCoefficient() == 0)
return true;
else
return false;
}
bool operator ==(const lineType& line, lineType& otherline) ///overrides == so that it returns true if two lines are equal
{
double factor = 1;
if (line.getXCoefficient() != 0)
factor = otherline.getXCoefficient() / line.getXCoefficient();
else
factor = otherline.getYCoefficient() / line.getYCoefficient();
if (line.getXCoefficient() * factor == otherline.getXCoefficient() &&
line.getYCoefficient() * factor == otherline.getYCoefficient() &&
line.getCOnstantTerm() * factor == otherline.getCOnstantTerm())
return true;
else
return false;
}
bool operator ||(const lineType& line, lineType& otherline) ///overrides || so that it returns true if two lines are parallel
{
if (line.getYCoefficient() == 0 && otherline.getYCoefficient() == 0)
return true;
else if ((line.getYCoefficient() != 0 && otherline.getYCoefficient() == 0) ||
(line.getYCoefficient() == 0 && otherline.getYCoefficient() != 0))
return false;
else if ((line.getXCoefficient() / line.getYCoefficient()) == (otherline.getXCoefficient() / otherline.getYCoefficient()))
return true;
else
return false;
}
bool operator&&(const lineType& line, lineType& otherline) ///overrides && so that it returns true if two lines are perpendicular
{
if (line.getYCoefficient() != 0 && otherline.getYCoefficient() != 0) {
if ((line.getXCoefficient() / line.getYCoefficient()) * (otherline.getXCoefficient() / otherline.getYCoefficient()) == -1.0)
return true;
else
return false;
} else if ((line.getXCoefficient() == 0 && otherline.getYCoefficient() == 0) ||
(line.getYCoefficient() == 0 && otherline.getXCoefficient() == 0))
return true;
else
false;
}
lineType::lineType(double a, double b, double c) { setLine(a, b, c); }