好吧,标题几乎说明了这一点。我已经发布了我的代码的一部分,我不知道如何将 1 添加到三角形的边(a、b 和 c)......任何帮助将不胜感激!
class Triangle
{
public:
Triangle();
Triangle(double a, double b, double c);
~Triangle();
double get_side_a() const;
double get_side_b() const;
double get_side_c() const;
Triangle& operator++();
void operator++(int dummy);
private:
double* sides;
};
Triangle::Triangle()
{
sides = new double[3];
sides[0] = 0;
sides[1] = 0;
sides[2] = 0;
} // Constructor
Triangle::Triangle(double a, double b, double c)
{
sides = new double[3];
sides[0] = a;
sides[1] = b;
sides[2] = c;
} // Constructor with parameters
Triangle::~Triangle()
{
if(sides)
{
delete[] sides;
sides = 0;
}
} // Destructor
double Triangle::get_side_a() const
{
return sides[0];
} // get_side_a
double Triangle::get_side_b() const
{
return sides[1];
} // get_side_b
double Triangle::get_side_c() const
{
return sides[2];
} // get_side_c
Triangle& Triangle::operator++()
{
*this = *this + 1;
return *this;
} // Oprator ++ (prefix)
void Triangle::operator++(int dummy)
{
++(*this);
} // Operator ++ (postfix)
int main()
{
Triangle tri1, tri2;
cout << "Enter side legths for triangle 1:" << endl;
cin >> tri1;
cout << endl << "Enter side legths for triangle 2:" << endl;
cin >> tri2;
cout << endl;
cout << tri1 << endl;
cout << tri2 << endl;
return 0;
}
我收到一个错误:在 Triangle& Triangle::operator++()...
编辑:众所周知,C++ 会烧脑,但必须亲身体验其他东西。每个人的 10 倍。