Sobes.tech
Junior — Middle+

Find and fix the errors in the provided C++ code

livecode

Task condition

This fragment of C++ code has issues related to dynamic memory management and inheritance. Without changing the class structures, fix the errors so that the program correctly creates, copies, and deletes objects.

class Foo
{
    public:
    Foo(int size) { i = new int[size]; }
    ~Foo() { delete[] i; }
    private:
    int* i;
};

class Bar : public Foo
{
    public:
    Bar(int size) : Foo(size) { i = new char[size]; }
    ~Bar() { delete[] i; }
    private:
    char* i;
};

void main()
{
    Foo* f = new Foo(100);
    Foo* b = new Bar(200);
    *f = *b;
    delete f;
    delete b;
}