2

Recently, I am learning Inheritance and Polymorphism in C++.

I made three classes: Node, uni_dir_Node(uni-direction Node), and bi_dir_Node(bi-direction Node).

Here is my code:

class Node {
   protected:
       string name;
       Node* next;
       virtual void connect(Node* _Node) = 0;
};

class uni_dir_Node : public Node {
    void connect(Node* _Node) {
        this->next = next;
    }
};

class bi_dir_Node : public Node {
    Node* previous;

    void connect(Node* next_Node, Node* previous_Node) {
        this->next = next;
        this->previous = previous_Node;
    }
};

int main()
{
    Node* head = new bi_dir_Node;
    return 0;
}

Of course there is a compiler error in this code.

My question is, the function connect() in class uni_dir_Node has one parameter but for the function connect() in class bi_dir_Node has two parameters. How do I keep this inheritance structure and make it legal?

Is there any good way to solve this problem?

4
  • the functions needs to have the same signature. Commented Oct 8, 2015 at 13:33
  • 1
    A Node is constructable with a Node *. A bi_dir_Node is not. Therefore a bi_dir_Node cannot be a Node. You can add a constructor for bi_dir_Node that takes a single Node * and sets the other Node * to nullptr or makes previous = next = node, but that is just asking for trouble. Your problem is just not suitable for inheritance and you shouldn't force it. Commented Oct 8, 2015 at 13:39
  • 1
    @nwp It's not a constructor, but a pure virtual function. Otherwise the argument is right: either provide an implementation of connect() with only one argument, or fully reschedule the inheritance. Commented Oct 8, 2015 at 13:56
  • I see... Thanks for ur suggestion. It makes me understand the Inheritance more. Commented Oct 9, 2015 at 1:35

2 Answers 2

1

As others have said, the problem is that bi_dir_Node doesn't have a void connect(Node* _Node) method.

Conceptually, what's going on is that inheritance indicates an "is a" relationship. Saying that bi_dir_Node inherits from Node means that bi_dir_Node is a Node, so anything that a Node can do, a bi_dir_Node can do.

You're trying to say that bi_dir_Node is a Node but that it can't do everything a Node can: specifically, it can't Connect with a single argument.

The solution is to either provide a single-argument Connect for bi_dir_Node or to remove or redesign the inheritance structure. For example, in C++, templates may be a better approach: you can make uni_dir_Node and bi_dir_Node completely separate (not part of the same inheritance hierarchy) and write template classes and template functions that are generic enough to operate on both.

Sign up to request clarification or add additional context in comments.

Comments

0

As says nwp, to have polymorphism, you need to have functions with the same prototypes in the derived classes.

By prototype, it means :

  1. same return type
  2. Same parameter list
  3. const keyword at the end of the prototype must be present on derived if present on base method
  4. same method name of course

This is because all functions need to be called the same way and same semantic whether it is a base or derived object.

virtual key word must be put on the base class method prototype.
The virtual behavior is inherited. So it can be put or not on derived class overriden methods.

Advanced stuff - not often useful :

Since C++ 98 (prehistory !), if a base class method is

Base * Method();

The derived method can be :

Derived * Method();

This is because Derived* IS A KIND of Base*

Hope it clarifies

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.