Jinak něco jde vyřešit líp.. určitě by byla lepší nějaká hotová kolekce, místo vlastního pole (pokud se jedná o takovou knihu)
Udělal jsem ještě na rychlo jeden příklad ve smyslu tý knihy... si z toho můžeš odvodit co máš špatně.. jinak nechápu, proč vlastně používáč C syntax v C++ (třeba ta alokace)
#include <iostream>
#include <string>
using namespace std;
typedef struct sHuman
{
string Title;
string Name;
string Address;
sHuman() { }
sHuman(string title, string name, string address)
{
Title = title;
Name = name;
Address = address;
}
} Human;
class Book
{
Human** m_people;
int m_capacity;
int m_current;
public:
Book(const int capacity) : m_capacity(capacity), m_current(0)
{
*m_people = new Human[capacity];
}
~Book()
{
clear();
}
bool add(Human* human)
{
if (m_current < m_capacity)
{
*(m_people + m_current++) = human;
return true;
}
return false;
}
bool remove(const int index)
{
if (m_current >= 0)
{
delete *(m_people + index);
for (int i = index; i < m_current - 1; i++)
*(m_people + i) = *(m_people + i + 1);
m_current--;
return true;
}
return false;
}
void clear()
{
for (int i = 0; i < m_current; i++)
delete *(m_people + i);
m_current = 0;
}
void outPeople() const
{
for (int i = 0; i < m_current; i++)
{
Human* h = *(m_people + i);
cout << "Jmeno: " << h->Title << " " << h->Name << " - " << h->Address << endl;
}
}
int Capacity() const { return m_capacity; }
int CurrentIndex() const { m_current; }
};
int main()
{
Book book(2);
book.add(new Human("Ing","Jan","Praha 1"));
book.add(new Human("Mgr","Karel","Praha 2"));
if (!book.add(new Human("Ing","Petr","Praha 3")))
{
cout << "Kniha je plna!\n";
}
book.remove(0);
book.add(new Human("Bc", "Lenka", "Praha 4"));
book.outPeople();
book.clear();
book.outPeople();
return 0;
}