I am having trouble finding out if I did the header correctly

#include <iostream>
#ifndef TREE
#define TREE

// Node class
class Node 
{
    int key;
    Node* left;
    Node* right;
public:
    Node() 
    { 
        key=-1; 
        left = NULL; 
        right = NULL; 
    };
    void setKey(int aKey) { key = aKey; };
    void setLeft(Node* aLeft) { left = aLeft; };
    void setRight(Node* aRight) { right = aRight; };
    int Key() { return key; };
    Node* Left() { return left; };
    Node* Right() { return right; };
};

// Tree class
class Tree {
     Node* root;
public:
     Tree();
     ~Tree();
     Node* Root() { return root; };
     void addNode(int key);
     void inOrder(Node* n);
     void preOrder(Node* n);
     void postOrder(Node* n);
private:
     void addNode(int key, Node* leaf);
     void freeNode(Node* leaf);
};

#endif

Am I allowed to put an include statement there?

Recommended Answers

All 2 Replies

Move the include statement down below to line 4, after the ifdef statement. Yes, you can put include statements in header files, it's done all the time.

You can hide node constructor body withing tree.cpp file as well.
Accessor functions could be const.
int Key() const { return key; };
Node* Left() const { return left; };
Node* Right() const { return right; };

void setKey(int aKey) could be void setKey(const int& aKey).
Same with 2 addNode functions.
void addNode(const int& key);
void addNode(const int& key, Node* leaf);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.