-2

I am currently studying for a programming exam and am supposed to write a destructor for the class "BTree" in one of the exercises. The following code is a Binary Tree. I don't know what should be in the body of the destructor because I haven't learned that properly yet. I think there should be something in there with delete because I also created dynamic objects on the heap with new. Thanks in advance.

class BTree
{


public:
    vertex* root;

    BTree()
    {
        root = NULL;
    };

    ~BTree() {
        
    };

    bool isEmpty() { return root == NULL; }

    vertex* Nikita = new vertex("Nikita");
    vertex* Vendor = new vertex("Vendor");
    vertex* faehrt = new vertex("faehrt");
    vertex* nach = new vertex("nach");
    vertex* Alexendria = new vertex("Alexandria");


    void main() {
        root = node(node(create(), Nikita,node(create(), Vendor, create() ))
                    , faehrt,
                    node(create(), nach, node(create(), Alexandria, create() ))
                    );
        cout << empty (right(root)) << endl;
        cout << value(left(root)) << endl << endl;

    };
class vertex{

public:

    int key;
    string data;
    vertex* leftChild;
    vertex* rightChild;
    vertex* parent;
    int height;


    vertex(string data){

        key = ID;
        this->data = data;
        leftChild = NULL;
        rightChild = NULL;
        parent = NULL;
        ID++;
    };

    vertex(){
        key = 0;
        leftChild = NULL;
        rightChild = NULL;
        parent = NULL;
    };

    ~vertex(){

    };
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#include "BTree.h"


int main() {

// Aufgabe 1
    BTree B;

    B.main();

// Aufgabe 2
    BTree C = B;
    C.print();


// Aufgabe 3
    BST D;

    D.main();
    D.print(D.root);
    D.sortvector(); //neu hinzugefügt

// Aufgabe 4
//  D.PrintLayers();
}
Vendor
  • 23
  • 2
  • Welcome to StackOverflow! It is recommended that you go through https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions to get better answers here. – Phalgun Sep 05 '21 at 18:27
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 05 '21 at 18:29

3 Answers3

1

As in the post stated above, you should free your objects in memory. I'm not too sure, but I think if you don't free / delete those objects it will just random unused memory laying around in your program that you can use for something else.

Solution

class BTree
{


public:
    vertex* root;

    BTree()
    {
        root = NULL;
    };

    ~BTree() 
    {
        delete this->Nikita;
        delete this->Vendor;
        delete this->faehrt;
        delete this->nach;
        delete this->Alexendria;

    };

    bool isEmpty() { return root == NULL; }

    vertex* Nikita = new vertex("Nikita");
    vertex* Vendor = new vertex("Vendor");
    vertex* faehrt = new vertex("faehrt");
    vertex* nach = new vertex("nach");
    vertex* Alexendria = new vertex("Alexandria");


    void main() {
        root = node(node(create(), Nikita,node(create(), Vendor, create() ))
                    , faehrt,
                    node(create(), nach, node(create(), Alexandria, create() ))
                    );
        cout << empty (right(root)) << endl;
        cout << value(left(root)) << endl << endl;

    };

Also just a suggestion :) You can use smart pointers to automatically call the deconstructor for your current object. Example:

std::unique_ptr<BTree> Instance = std::make_unique<BTree>();

Instance->YourFunction(Args...);

If I make any mistakes or errors, i am open to constructive criticism :)

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • should the first can be a cant? – Daij-Djan Sep 02 '21 at 05:38
  • I tried to do that with the destructor you wrote in your solution but if I go in Debug Mode it does not delete the Objects. I am highly confused with destructors so I moved on to the other excercises ^^. Yeah I should learn how to use smart pointers.. Our Professor told us also that it's better but He wants from us to do it that way in this exercise. Anyways, thanks for your solution :) – Vendor Sep 04 '21 at 02:15
1

Nothing, you should write nothing in your destructor and not use the new command to allocate your member objects.

Use std::unique_ptr<XXXX> to manage your memory so you do not have to.

Bart
  • 1,405
  • 6
  • 32
  • This is a test exam.. I would use Smart Pointers but our Professor wants us to do it that way. – Vendor Sep 02 '21 at 21:57
0

As little as necessary.

~BTree will need to delete nikita and friends, plus any other vertexs it comes to own. Note that if you require a destructor you often (and definitely will in this case) need a copy constructor and an assignment operator. See this page on The Rule Of Three (and friends) for details.

vertex, on the other hand, likely owns no resources and the Rule of Zero discussed in the earlier link suggests it doesn't need a destructor at all.

Additional useful reading to help; you tie it all together and better understand the point of constructors and destructors: What is meant by Resource Acquisition is Initialization (RAII)?

user4581301
  • 33,082
  • 7
  • 33
  • 54