Как инициализировать список значениями разного класса?
По заданию необходимо создать двух связный список. Для заполнения поля данных в узле, предварительно создана иерархия классов.Shape(базовый)и Rect,Circle(производные от Shape). Создана функция произвольно генерирующая объекты Rect и Circle. Подскажите пожалуйста, как в конструкторе узла проинициализировать поле data этими объектами?
Shape.h
class Shape
{
public:
enum Color { Red, Black, Green, Blue };
protected:
Color color;
public:
Shape(int x = 0);// конструктор с одним параметром
Shape(const Shape& other);// конструктор крпирования
virtual~Shape();
void Set_Color(int x); // присваим значение
int Get_Color(); // принимаем значение
void printMyShape(); // распичатываем
bool operator == (const Shape& other);
bool operator != (const Shape& other);
friend std::ostream& operator<<(std::ostream& os, const Shape& an);
};
std::ostream & operator<<(std::ostream & os, const Shape& an);
Shape.cpp
Shape::Shape(int x)
{
this->color = static_cast<Color>(x);
}
Shape::Shape(const Shape& other)
{
color = other.color;
}
Shape::~Shape()
{
std::cout << "Now I am in Shape's destructor!" << std::endl;
}
void Shape::Set_Color(int x)
{
this->color = static_cast<Color>(x);
}
int Shape::Get_Color()
{
return this->color;
}
void Shape::printMyShape()
{
std::cout << this->color << std::endl;
}
bool Shape::operator==(const Shape& other)
{
return this->color == other.color;
}
bool Shape::operator!=(const Shape& other)
{
return !(this->color == other.color);
}
std::ostream& operator<<(std::ostream& os, const Shape& an)
{
os << an.color;
return os;
Rect.h
class Rect :public Shape
{
int m_left, m_right, m_top, m_bottom;
public:
Rect();
Rect(int color, int l, int r, int t, int b); // создали конструктор с параметрами
Rect(const Rect& nov); // конструктор копирования
//~Rect();
//virtual~Rect();
//--------------------------------методы------------------------------------------------------
void Norm(); // проверка введенных данных
void Print(); // печать
void SetAll(int a, int b, int c, int d); // присваиваем значения переменным класса
int GetRect_left() const;
int GetRect_right() const;
int GetRect_top() const;
int GetRect_bottom() const;
int GetAll(int& left, int& right, int& top, int& bottom);
int SquareRect();//вычисляем площадь прямоугольника
bool operator ==(const Rect& other);
bool operator !=(const Rect& other);
friend std::ostream& operator<<(std::ostream& os, const Rect& other);
};
std::ostream& operator<<(std::ostream& os, const Rect& other);
Rect.cpp
Rect::Rect() :Shape(color)
{
m_left = 0;
m_right = 0;
m_top = 0;
m_bottom = 0;
}
Rect::Rect(int color, int l, int r, int t, int b):Shape(color)
{
m_left = l;
m_right = r;
m_top = t;
m_bottom =b;
}
Rect::Rect(const Rect& nov):Shape(color)
{
m_left = nov.m_left;
m_right = nov.m_right;
m_top = nov.m_top;
m_bottom = nov.m_bottom;
}
//Rect::~Rect()
//{
//}
void Rect::Norm()
{
if (m_left > m_right)
{
std::swap(m_left, m_right); // меняем местами значения переменных
}
if (m_top > m_bottom)
{
std::swap(m_top, m_bottom);
}
}
void Rect::Print()
{
std::cout << color << " " << m_left << " " << m_right << " " << m_top << " " << m_bottom << std::endl;
}
void Rect::SetAll(int a, int b, int c, int d)
{
this->m_left = a;
this->m_right = b;
this->m_top = c;
this->m_bottom = d;
}
int Rect::GetRect_left() const
{
return this->m_left;
}
int Rect::GetRect_right() const
{
return this->m_right;
}
int Rect::GetRect_top() const
{
return this->m_top;
}
int Rect::GetRect_bottom() const
{
return this->m_bottom;
}
int Rect::GetAll(int& left, int& right, int& top, int& bottom)
{
std::pair <int, int> PAIR1;
PAIR1.first = m_left;
PAIR1.second = m_right;
std::pair <int, int> PAIR2;
PAIR2.first = m_top;
PAIR2.second = m_bottom;
return 0;
}
int Rect::SquareRect()
{
int SquareR = (m_right - m_left ) * (m_bottom- m_top);
return SquareR;
}
bool Rect::operator==(const Rect& other)
{
return this->m_left == other.m_left && this->m_top == other.m_top;
}
bool Rect::operator!=(const Rect& other)
{
return !(this->m_left == other.m_left && this->m_top == other.m_top);
}
std::ostream& operator<<(std::ostream& os, const Rect& other)
{
os << other.GetRect_left() << " " << other.GetRect_right() << " " << other.GetRect_top() << " " << other.GetRect_bottom();
return os;
}
Circle.h
class Circle :public Shape
{
int x, y, radius;
public:
Circle();// конструктор без параметров
Circle(int color, int x, int y, int r); //конструктор с парометрами
Circle(const Rect& x);
Circle(const Circle& other); //конструктор копирования
//~Circle();
//virtual~Circle();
void SetCircle(int x, int y, int r, int color);
int GetLeft();
int GetTop();
int GetRadius();
void PrintCircle();
int SquareCircle();//вычисляем площадь прямоугольника
bool operator == (const Circle& other);
bool operator != (const Circle& other);
friend std::ostream& operator<<(std::ostream& os, const Circle& other);
};
std::ostream& operator<<(std::ostream& os, const Circle& other);
Circle.cpp
Circle::Circle():Shape(color)
{
this->x = 0;
this->y = 0;
this->radius = 0;
}
Circle::Circle(int color, int x, int y, int r) :Shape(color)
{
this->x = x;
this->y = y;
this->radius = r;
}
Circle::Circle(const Rect& x) :Shape(color)
{
this->x = x.GetRect_left();
this->y = x.GetRect_top();
this->radius = x.GetRect_right();
}
Circle::Circle(const Circle& other) :Shape(color)
{
this->x = other.x;
this->y = other.y;
this->radius = other.radius;
}
//Circle::~Circle()
//{
// std::cout << "Now I am in Shape's destructor!" << std::endl;
//}
void Circle::SetCircle(int x, int y, int r, int color)
{
this->x = x;
this->y = y;
this->radius = radius;
this->color = static_cast<Color>(color);
}
int Circle::GetLeft()
{
return this->x;
}
int Circle::GetTop()
{
return this->y;
}
int Circle::GetRadius()
{
return this->radius;
}
void Circle::PrintCircle()
{
std::cout << this->color << " " << this->x << " " << this->y << " " << this->radius << std::endl;
}
int Circle::SquareCircle()
{
int SquareC = 3.14 * (radius * radius);
return 0;
}
bool Circle::operator==(const Circle& other)
{
return this->x== other.x && this->y== other.y;
}
bool Circle::operator!=(const Circle& other)
{
return !(this->x == other.x && this->y == other.y);
}
std::ostream& operator<<(std::ostream& os, const Circle& other)
{
os << other.x << " " << other.y << other.radius;
return os;
}
Node.h
class Node
{
Node* pPrev;
Node* pNext;
Shape* m_data;
Shape* Make();
public:
Node(Node* p, const Shape*);
~Node();
};
Node.cpp
Shape* List::Node::Node::Make()
{
switch (rand()%2)
{
case 0:
return new Rect;
case 1:
return new Circle;
}
}
List::Node::Node(Node* p, const Shape* sh) :m_data(*sh)
{
this->pPrev = p;
this->pNext = p->pNext;
p->pNext = this;
pNext->pPrev = this;
}