我正在尝试编写数学集合类。我的想法是制作一个称为Set的基类。然后我写了Set类的两个子类。即FiniteSet类和CountableSet类(可以处理无限集合)。我的问题是,那些子类彼此依赖,而我无法解决这个问题。我也将欣赏完全不同的解决方案。
Device.RequestStoreReview()
答案 0 :(得分:1)
在定义类之前声明它们,例如:
//--------------------------------------------------------
//Set class
//--------------------------------------------------------
class Set
{
public:
//some virtual functions
protected:
//some attributes
};
//--------------------------------------------------------
// Declare FiniteSet class
//--------------------------------------------------------
class FiniteSet : public Set
{
public:
//implements all virtual functions
//function which needs to know CountableSet:
Set unionWith(Set* otherSet);
private:
//some attributes
};
//--------------------------------------------------------
// Declare CountableSet class
//--------------------------------------------------------
class CountableSet : public Set
{
public:
//implements all virtual functions
//function which needs to know FiniteSets
Set intersectWith(Set* otherSet);
private:
//some attributes
};
// Define FiniteSet::unionWith
Set FiniteSet::unionWith(Set* otherSet);
{
if(typeid(CountableSet) != typeid(*otherSet))
{
//the other set is finite. We can simply add all
//elements from otherSet to this set.
}
else
{
//create a CountableSet and return it
}
}
// Define CountableSet::intersectWith
Set CountableSet::intersectWith(Set* otherSet)
{
if(typeid(FiniteSet) == typeid(*otherSet))
{
//do something and return FiniteSet
}
else
{
//do something and return occasionally CountableSet
}
}