Sobes.tech
Junior — Senior

Code optimization and debugging (option 4)

livecode

Task condition

It is necessary to rewrite the presented fragment, eliminating memory management errors and violations of OOP principles.

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

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

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