Not sure...

Hi there

I am not sure if I should be using delete [], at the end of main.

I am creating new dynamic object with use of new, but this is stored in an array.
Should I be using delete, should i delete each object created (for example in the loop and then use delete?) or delete should not be used here?

thanks :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  #include <iostream>
#include <string>

class Publication
{
protected:
    std::string title;
    double price;
    double sales[3];
public:
    Publication() : title("n/a"), price(0.0), sales{ 0 }
    {}

    virtual void getData() = 0;

    virtual void putData() = 0;

};
class Book : public Publication
{
    int pageCount;

public:
    Book() : pageCount(0)
    {}
    void getData()
    {
        std::cout << "Please provide title: ";
        std::cin >> title;
        std::cout << "Please provide $$: ";
        std::cin >> price;
        std::cout << "Sales in 1st Month: ";
        std::cin >> sales[0];
        std::cout << "Sales in 2nd Month: ";
        std::cin >> sales[1];
        std::cout << "Sales in 3rd Month: ";
        std::cin >> sales[2];
        std::cout << "Please provide page count: ";
        std::cin >> pageCount;
    }
    void putData()
    {
        std::cout << "Book title: " << title << " Book cost: $" << price << std::endl;
        std::cout << "sales in month 1,2 and 3: " << sales[0] << ", " << sales[1] << ", " << sales[2] << std::endl;
        std::cout << "Number of pages: " << pageCount << std::endl;
    }
};
class Tape : public Publication
{
    double tapeTime;
public:
    Tape() : tapeTime(0.0)
    {}
    void getData()
    {
        std::cout << "How long is the tape in minutes: ";
        std::cin >> tapeTime;
    }
    void putData()
    {
        std::cout << "Lenght of the tape in minutes: " << tapeTime << std::endl;
    }
};


int main()
{

    std::cout << "Please provide" << std::endl;
    Book* bk[100];
    Tape* tp[100];
    int numberB = 0;		// number of books
    int numberT = 0;		// number of tapes
    char choice;

    do {


        std::cout << "Please add (t)ape or (b)ook: ";
        std::cin >> choice;

        if (choice == 'b') {
            bk[numberB] = new Book;
            bk[numberB++]->getData();
        } else {
            tp[numberT] = new Tape;
            tp[numberT++] = new Tape;
        }
        std::cout << "Enter another? y/n: ";
        std::cin >> choice;
    } while (choice == 'y');

    system("cls");
    bk[0]->putData();
    bk[1]->putData();
    bk[2]->putData();

    delete []bk;
    delete []tp;
return 0;
}
Last edited on
You should only use delete on each object that has been created with new.

1
2
3
4
5
6
7
for (int i = 0; i < numberB; i++) {
	delete bk[i];
}

for (int i = 0; i < numberT; i++) {
	delete tp[i];
}
or, to explain that a little more, bk isnt a pointer, its an array that contains pointers. Each pointer inside bk needs a delete if it was allocated with new.

This construct is a little clunky. You already capped your allowed size to 100, why not just have

bk = book[100];

and have done with it? You can track how many you have the same way so you don't access into book farther than you have used it, that is unchanged.

The memory you are "saving" by not allocating them until needed is tiny, and the extra pointers introduce potential for bugs and aggravations.

And a vector would be great for handling this, growing to whatever size you need on demand AND getting rid of the need for pointers.

I think its important to learn pointers and how to do what you are doing, but in this code segment, the pointers serve no useful purpose.

the destructor of `Publication' should be virtual
virtual ~Publication() = default;
To understand why consider this
1
2
3
4
Publication *a;
a = new Book();
//...
delete a; //this will call ~Publication() but not ~Book() 


¿why does `Tape' ignore its members title, price and sales (which got through inheritance)?
Topic archived. No new replies allowed.