0

I have a genuine question about namespaces in C++. I am working on one of my assignments that involve binary trees. I just made my header file and I am working on my source code. As I started copy-pasting function prototypes to the .cpp file I noticed typing "class name" + :: every time made it exhausting and messy.

I was also told in one of my classes that using the std namespace is considered bad practice so I just stopped using it, but no one ever told me what namespaces actually are and how they work. I googled it and I got a brief understanding of how they work and why std is considered bad practice

I emailed my professor about using the namespace of the class (by adding the line using namespace ShelterBST and she said "No namespaces for now", which is completely fine, I guess that's part of her teaching policy but that got me wondering...

I know that by saying ShelterBST::someFunction I am already using the namespace of the class, but why not just say using namespace ShelterBST at the top of the file?

Could someone please explain this to me on a freshman college level please. Thank you

Here is my code for better understanding:

/**
 * @author Jakob Balkovec
 * @file shelterBST.h -> @headerfile
 * @brief ShelterBST class variables and methods declaration
 */

#ifndef SHELTERBST_H
#define SHELTERBST_H

#include <string>

class ShelterBST {

private:   
  /** @struct Pet, used to store the age and the naem of a pet in a shelter */
  struct Pet {
    std::string name;
    int age;
    Pet(std::string name = "", int age = 0) : name(name), age(age) {} //Constructor
  };
  
  /** @struct TreeNode, used to implement a tree
   * @members: 
   * Pet *pet -> pointer to a Pet object
   * TreeNode *left -> left child
   * TreeNode *right -> right child
   * 
   * @note noth develop in a new subtree if left or right != NULL
   */
  struct TreeNode {
    struct Pet *pet;
    struct TreeNode *left;
    struct TreeNode *right;
  };
  
  /** @note BST base*/
  struct TreeNode *root;
  
  /** @note Methods*/
  struct TreeNode* insert(struct TreeNode* root, Pet *pet);
  struct TreeNode* search(struct TreeNode* root, std::string name);
  struct TreeNode* findParent(struct TreeNode* root, std::string name);
  struct TreeNode* findPredecessor(struct TreeNode* root);
  struct TreeNode* deleteNode(struct TreeNode* root, std::string name);
  struct TreeNode* destroyTree(struct TreeNode* root);

  void inOrder(struct TreeNode* root);
  void postOrder(struct TreeNode* root);
  void preOrder(struct TreeNode* root);

  int numberOfNodes(struct TreeNode* root);
  int numberOfInternalNodes(struct TreeNode* root);
  int numberOfNodesAtLevel(struct TreeNode* root, int level);
  int findHeight(struct TreeNode* root);

  bool isBalanced(struct TreeNode* root);
  /** @note Methods END*/
  
public:
  /** @note Methods*/
  ShelterBST(); //constructor
  ~ShelterBST(); //destructor

  void inOrderDisplay(); //call private
  void preOrderDisplay(); //call private
  void postOrderDisplay(); //call private
  void test();

  struct TreeNode* insertPet(std::string name, int age);
  struct TreeNode* searchPet(std::string name);

  /** @note using function overloading*/
  TreeNode* findParent(std::string name);
  TreeNode* findInorderPredecessor(std::string name);
  TreeNode* deleteNode(std::string name);

  int numberOfNodes();
  int numberOfInternalNodes();
  int numberOfNodesAtLevel(int level);
  int findHeight();

  bool isBalanced();
  /** @note Methods END*/
};


#endif //SHELTER_BST

and some prototypes:

struct ShelterBST::TreeNode* ShelterBST::insert(struct TreeNode* root, Pet *pet) {

}
struct ShelterBST::TreeNode* ShelterBST::search(struct TreeNode* root, std::string name) {

}
struct ShelterBST::TreeNode* ShelterBST::findParent(struct TreeNode* root, std::string name) {

}
struct ShelterBST::TreeNode* ShelterBST::findPredecessor(struct TreeNode* root) {

}
struct ShelterBST::TreeNode* ShelterBST::deleteNode(struct TreeNode* root, std::string name) {

}
struct ShelterBST::TreeNode* ShelterBST::destroyTree(struct TreeNode* root) {

}
  • Class name is not a namespace, they are entirely different things. Both are scopes and have same notation, but they are different concepts. – Sami Kuhmonen Apr 15 '23 at 04:57

0 Answers0