Jumping from Java to C ++

As stated in this thread, I am very new to C ++, but I have some experience with java. To start learning C ++, I had a (not very original) idea to make a simple command line calculator. What I'm trying to do is store numbers and operators in a binary tree.

#include <iostream>
using namespace std;

class Node
{
  bool leaf;
  double num;
  char oper;
  Node* pLNode;
  Node* pRNode;

public:

  Node(double n)
  {
    num = n;
    leaf = true;
    pLNode = 0;
    pRNode = 0;
  }

  Node(char o, Node lNode, Node rNode)
  {
    oper = o;
    pLNode = &lNode;
    pRNode = &rNode;
    leaf = false;
  }

  bool isLeaf()
  {
    return leaf;
  }

  double getNumber()
  {
    return num;
  }

  char getOperator()
  {
    return oper;
  }

  Node* getLeftNodePointer()
  {
    return pLNode;
  }

  Node* getRightNodePointer()
  {
    return pRNode;
  }

  //debug function
  void dump()
  {
    cout << endl << "**** Node Dump ****" << endl;
    cout << "oper: " << oper << endl;
    cout << "num: " << num << endl;
    cout << "leaf: " << leaf << endl;
    cout << "*******************" << endl << endl;
  }

};

class CalcTree
{
  Node* pRootNode;
  Node* pCurrentNode;
public:

  Node* getRootNodePointer()
  {
    return pRootNode;
  }

  Node* getCurrentNodePointer()
  {
    return pCurrentNode;
  }

  void setRootNode(Node node)
  {
    pRootNode = &node;
  }

  void setCurrentNode(Node node)
  {
    pCurrentNode = &node;
  }

  double calculateTree()
  {
    return calculateTree(pRootNode);
  }

private:

  double calculateTree(Node* nodePointer)
  {
    if(nodePointer->isLeaf())
    {
      return nodePointer->getNumber();
    }
    else
    {
      Node* leftNodePointer = nodePointer->getLeftNodePointer();
      Node* rightNodePointer = nodePointer->getRightNodePointer();
      char oper = nodePointer->getOperator();

      if(oper == '+')
      {
    return calculateTree(leftNodePointer) + calculateTree(rightNodePointer);
      }
      else if(oper == '-')
      {
    return calculateTree(leftNodePointer) - calculateTree(rightNodePointer);
      } 
      else if(oper == '*')
      {
    return calculateTree(leftNodePointer) * calculateTree(rightNodePointer);
      }
      else if(oper == '/')
      {
    return calculateTree(leftNodePointer) / calculateTree(rightNodePointer);
      }
    }
  }
};

int main(int argc, char* argv[])
{
  CalcTree tree;
  tree.setRootNode(Node('+', Node(1), Node(534)));
  cout << tree.calculateTree() << endl;
  return 0;
}

I have a few questions about this code:

  • It compiles, but does not do what is intended. It seems that after tree.setRootNode (Node ('+', Node (1), Node (534))); in the main correct rule it is correctly initialized, but the left one is not. Compiling and running this gives 534 for me (gcc, freebsd). What is wrong here?

  • It seems that in C ++ people prefer to define class members outside the class, e.g.

    class A {public: void member (); };

    A:: member() {std:: cout < "Hello world" < :: ;}

    ?

  • ++ (, ..).

  • java eclipse. Atm emacs ++. - () ++ ide, stfu emacs, ?:)

+3
5

-, . SO ++, ++ . , , , , , .

, :

  Node(char o, Node lNode, Node rNode)
  {
    oper = o;
    pLNode = &lNode;
    pRNode = &rNode;
    leaf = false;
  }

3 , . , , , , . . . . , :

  Node(char o, Node *lNode, Node *rNode)
      :oper(o), pLNode(lNode), pRNode(rNode), leaf(false)
  { }

:

  CalcTree tree;
  tree.setRootNode(new Node('+', new Node(1), new Node(534)));
  cout << tree.calculateTree() << endl;

. - , . , , Node ( , -):

  ~Node() {
      delete pLNode;
      delete pRNode;
  }

, null-ify: delete . , CalcTree, :

  ~CalcTree() {
      delete pRootNode;
  }

CalcTree, 0! : , , () , , . . , , , ( shared_ptr). - . , . , . Node:

  private:
      Node(Node const&);
      Node& operator=(Node const&);

CalcTree, .

, :

, ++

, , ( , ). , , , , . , , , , , . , , - . , , . ++ , , - .

++

, :

  • mDataMember
  • getDataMember
  • localVariable
  • , 4 (, , . , , !)

java eclipse. Atm emacs ++.

emacs ++ eclipse Java. eclipse Java ( CDT eclipse ++). , ( ), Visual ++ - IDE, . SO : IDE ++ ( google stackoverflow, , ).

+3
  • -, " ++". STL ++ , . ( ++, ).

  • ++ . . , . , . , ( , ).

  • , Eclipse/CDT Linux. Windows Microsoft Visual ++ Express Edition. , .

  • , . , , time. (, - ) , ++ .

+5
  • , ,

    Node (char o, Node lNode, Node rNode)

Node(char o, Node &lNode, Node &rNode)

( ),

Node(char o, Node *lNode, Node *rNode)
  1. : (.h ) , .o, ++, . :. , , .h. Java, .class. ++ , . , , ++ .o. , Turing-complete .h/.cpp.

  2. . ++ , C , BSD GNU , Microsoft . Java, .

  3. Eclipse, NetBeans, KDevelop, vim emacs - . Windows, Visual Studio .

+1

++ . Google . "" .;)

IDE: CDT (C [/++], Eclipse). ++ Eclipse: http://www.eclipse.org/linuxtools/ . .

KDevelop, KDE.

0
2. It seems in c++ people prefer to define members of a class outside the 
class[..]

. . cpp . inline ( , ). , , .

3. I'd very much like some pointers on c++ conventions (naming, indenting etc.)

. .

4. I'm used to coding java with eclipse. 

Eclipse ++. (ogle) , . , ++; -)

0

Source: https://habr.com/ru/post/1703345/


All Articles