4. (Smart Pointers and STL Algorithms) In this exercise we create a simple example of STL containers
Question:
4. (Smart Pointers and STL Algorithms)
In this exercise we create a simple example of STL containers whose members are smart pointers.
To this end, consider the following class hierarchy:
class Base
{ // Base class private:
public:
Base() { }
virtual void print() const = 0;
protected:
virtual ~Base() { cout << "Base destructor"; }
};
class Derived : public Base
{ // Derived class private:
public:
Derived() : Base() { }
~Derived() { cout << "Derived destructor"; }
void print() const { cout << "derived object";}
};
Answer the following questions:
a) Create a list whose elements are smart pointers to Base.
b) Create a factory function to create instances of class Derived and then add these instances to the list.
c) Test the functionality (try to break the code) and check that there are no memory leaks.
Step by Step Answer: