Как создать класс контейнер который будет обладать динамическим списком объектов, являющихся экземплярами производных классов?
Я уже поработал над ним, главное задание - это создать иерархию классов, конструкторы перемещения и копирования с которыми я вроде справился.
На данный момент нужно реализовать класс контейнер который будет обладать динамическим списком объектов, являющихся экземплярами производных классов по иерархии наследования. Как это можно сделать?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Car
{
public:
string* m_name;
public:
Car() {} // Конструктор по умолчанию
~Car()
{
delete m_name;
}
Car(const Car& x) // Конструктор копирования
{
this->m_name = new string;
this->m_name = x.m_name;
}
Car& operator=(const Car& x)
{
if (&x == this)
return *this;
delete m_name;
this->m_name = new string;
this->m_name = x.m_name;
return *this;
}
Car(Car&& x) : m_name(x.m_name)
{
x.m_name = nullptr;
}
Car& operator=(const Car&& x)
{
if (&x == this)
return *this;
delete m_name;
this->m_name = x.m_name;
*x.m_name = nullptr;
return *this;
}
string getName()
{
return string();
}
virtual void info()
{
cout << "Vehicle : " << endl;
}
};
class BMW : public Car
{
public:
BMW() {} // Конструктор по умолчанию
BMW(const Car& x) // Конструктор копирования
{
this->m_name = new string;
this->m_name = x.m_name;
}
BMW& operator=(const Car& x)
{
if (&x == this)
return *this;
delete m_name;
this->m_name = new string;
this->m_name = x.m_name;
return *this;
}
BMW(Car&& x)
{
x.m_name = nullptr;
}
BMW& operator=(const Car&& x)
{
if (&x == this)
return *this;
delete m_name;
this->m_name = x.m_name;
*x.m_name = nullptr;
return *this;
}
void info() override
{
cout << endl;
cout << "BMW M3 " << endl;
cout << "250 kmph " << endl;
cout << "333 hp " << endl;
cout << "4 speed automatic " << endl;
cout << "rear weel drive " << endl;
}
};
class CarContainer
{
private: vector<Car*>_container;
Car o;
public: CarContainer() = default;
CarContainer(CarContainer&) = delete;
CarContainer(CarContainer&& another) : _container(move(another._container)) {}
~CarContainer();
};
int main()
{
Car* unicycle;
unicycle = new Car;
unicycle->info();
delete unicycle;
unicycle = new BMW;
unicycle->info();
delete unicycle;
}