0
votes

My question is whether or not it's ok call the base class constructor as being initialized to nullptr the way I have below in derivedClassA. The code works as expected but it still feels wrong.

For the sake of length I have omitted other classes that are derived from class Tree , but the idea here is that you can create a Tree object that has any number of derived classes, and call getStringExpression() and it will return whatever getStringExpression evaluates to. For example, if an object of type Tree contains a pointer to an object of DerivedClassA, which has right and left children equal to "Car" and "Ride" then Tree.getStringExpression(); would return "(Car + Ride)". Assume the values Car and Ride are returned by concrete classes derived from Tree that return the string "Car" and "Ride" respectively when their getStringExpression() method is called (and they have no children).

It feels wrong to initialize the base class constructor to nullptr the way I have in derivedClassA because if I create a single object of derivedClassA, then this object is the root node with its two children created during its construction. What happens to the constructed Tree in this scenario? Since it's pointer is nullptr it's not really being used or it's sort of disconnected or univolved with the object of derivedClassA...

I apologize in advance because I know this isn't the most straightforward question.

Tree.h-

class Tree
{
public:
    explicit Tree(Tree *rootNode);
    virtual ~Tree() {};
    virtual std::string getStringExpression() const;
private:
    Tree *rootNodePtr = nullptr;
};

Tree.cpp-

#include "Tree.h"

Tree::Tree(Tree *rootNode)
{
    rootNodePtr = rootNode;
}

std::string Tree::getStringExpression() const
{
    if(rootNodePtr != nullptr)
    {
        return rootNodePtr->getStringExpression();
    }
        return "Tree has no children";
}

derivedClassA.h-

#include "Tree.h"
class derivedClassA :
    public Tree
{
public:
    derivedClassA(Tree *leftChild, Tree *rightChild);
    virtual ~derivedClassA();
    virtual std::string getStringExpression() const override;
private:
    Tree *leftChildPtr = nullptr;
    Tree *rightChildPtr = nullptr;

};

derivedClassA.cpp-

#include "derivedClassA.h"

derivedClassA::derivedClassA(Tree *leftChild, Tree *rightChild): Tree(nullptr)
{
    leftChildPtr = leftChild;
    rightChildPtr = rightChild;

}


derivedClassA::~derivedClassA()
{
}

std::string derivedClassA::getStringExpression() const
{
    auto expressionValue = "(" + leftChildPtr->getStringExpression() + "+" + rightChildPtr->getStringExpression() + ")";
    return expressionValue;
}
1

1 Answers

0
votes

It sounds like you have merged the classes of Tree and Node. You are initializing Tree in the constructor with a single "root" tree, when it should have a "root Node that itself has two children. In that case, Node wouldn't inherit from Tree - you would have two separate classes.

Or perhaps you have an abstract Node class and a BinaryNode that inherits it to represnet a node with two children, opening up the door for different node types (nodes with more than two children, or unbalanced children).

So to answer your question, yes it seems odd that you are initializing the "base" with a default value, but I think it's because your class hierarchy is wrong. Meaning derivedClassA is not a Tree.