【发布时间】:2021-09-05 22:59:11
【问题描述】:
我有一些带有几何图形的 C++ 类,我需要重载比较运算符(例如 >、<、= 等)和输出运算符 <<当我需要打印或比较这些类的任何实例时,我会参考它们区域的值(我使用getArea() 得到)。
我试图将这些运算符重载放在父类 GeometricShape 中,但我遇到了各种编译器错误,这对我来说没有多大意义。
如果我将运算符重载放在每个子类中,它可以正常工作,但它非常丑陋且不理想(大量代码重复)。
如何删除此代码重复并将运算符重载移动到父类?
using namespace std;
#include <iostream>
#include <stdlib.h>
#include <math.h>
class GeometricShape
{
public:
double getArea();
};
class Poligon : public GeometricShape
{
protected:
double base;
double height;
public:
Poligon(double base, double height) : base(base), height(height) {}
Poligon(float base, float height) : Poligon(double(base), double(height)) {}
Poligon(int base, int height) : Poligon(double(base), double(height)) {}
double getBase()
{
return this->base;
}
double getHeight()
{
return this->height;
}
void setBase(double b)
{
this->base = b;
}
void setHeight(double a)
{
this->height = a;
}
};
class Rectangle : public Poligon
{
public:
Rectangle(double base, double height) : Poligon(base, height) {}
Rectangle(float base, float height) : Poligon(base, height) {}
Rectangle(int base, int height) : Poligon(base, height) {}
double getArea()
{
return base * height;
}
// HOW TO MOVE THIS CODE INTO GeometricShape and reuse it???
friend ostream& operator<<(ostream& out, Rectangle &obj)
{
out << obj.getArea();
return out;
}
friend bool operator==(Rectangle &obj1, Rectangle &obj2)
{
return obj1.getArea() == obj2.getArea();
}
friend bool operator>(Rectangle &obj1, Rectangle &obj2)
{
return obj1.getArea() > obj2.getArea();
}
friend bool operator<(Rectangle &obj1, Rectangle &obj2)
{
return obj1.getArea() < obj2.getArea();
}
friend bool operator <= (Rectangle &obj1, Rectangle &obj2)
{
return obj1.getArea() <= obj2.getArea();
}
friend bool operator >= (Rectangle &obj1, Rectangle &obj2)
{
return obj1.getArea() >= obj2.getArea();
}
};
class Triangle : public Poligon
{
public:
Triangle(double base, double height) : Poligon(base, height) {}
Triangle(float base, float height) : Poligon(base, height) {}
Triangle(int base, int height) : Poligon(base, height) {}
double getArea()
{
return (base * height)/2;
}
// HOW TO MOVE THIS CODE INTO GeometricShape and reuse it???
friend ostream& operator<<(ostream& out, Triangle &obj)
{
out << obj.getArea();
return out;
}
friend bool operator==(Triangle &obj1, Triangle &obj2)
{
return obj1.getArea() == obj2.getArea();
}
friend bool operator>(Triangle &obj1, Triangle &obj2)
{
return obj1.getArea() > obj2.getArea();
}
friend bool operator<(Triangle &obj1, Triangle &obj2)
{
return obj1.getArea() < obj2.getArea();
}
friend bool operator <= (Triangle &obj1, Triangle &obj2)
{
return obj1.getArea() <= obj2.getArea();
}
friend bool operator >= (Triangle &obj1, Triangle &obj2)
{
return obj1.getArea() >= obj2.getArea();
}
};
class Circle : public GeometricShape
{
double radius;
public:
Circle(double radius) : radius(radius) {}
double getArea()
{
return (radius * radius) * M_PI;
}
double getRadius()
{
return this->radius;
}
void setRadius(double r)
{
this->radius = r;
}
// HOW TO MOVE THIS CODE INTO GeometricShape and reuse it???
friend ostream& operator<<(ostream& out, Circle &obj)
{
out << obj.getArea();
return out;
}
friend bool operator==(Circle &obj1, Circle &obj2)
{
return obj1.getArea() == obj2.getArea();
}
friend bool operator>(Circle &obj1, Circle &obj2)
{
return obj1.getArea() > obj2.getArea();
}
friend bool operator<(Circle &obj1, Circle &obj2)
{
return obj1.getArea() < obj2.getArea();
}
friend bool operator <= (Circle &obj1, Circle &obj2)
{
return obj1.getArea() <= obj2.getArea();
}
friend bool operator >= (Circle &obj1, Circle &obj2)
{
return obj1.getArea() >= obj2.getArea();
}
};
【问题讨论】:
-
getArea应该是虚拟的,并且操作符应该基于GeomatricShape(并成为其好友)。上述相同的派生(即Poligon、Rectangle等)然后提供getArea的虚拟覆盖。我强烈怀疑classic slicing problem 会出现在你的未来,顺便说一句,如果你打算收集这些东西。 -
“如何将此代码移动到 GeometricShape” → 不要。将其移出并使其成为独立功能。
-
缺少
constBTW。 -
如果您显示不能与编译器错误消息一起工作的代码,这会更容易。这对您来说没有意义,但编译器错误消息包含很多有用的信息,我们可以帮助您理解它。
-
附带说明,了解您的用例也会有所帮助。你甚至可能一开始就不需要继承。
标签: c++ inheritance operator-overloading ostream comparison-operators