I make a suggested method by adding some flags in the phoneRec to mark for group and delet status. A deleted record is marked as Deleted. But it is not removed until you issue a flush() command. That is very typical way. Because actually remove elements from an array require reallocate and re-arrange the whole array. It shall not be done very frequently.
The class phoneRec
enum GROUP {Family=0, Friend=1, Junk=2};
enum STATUS {Active=0, Deleted=1};
class phoneRec
{
public:
phoneRec(): group(Junk), deleted(Active){;}
phoneRec(const int n, const char *a): numb(n), group(Junk), deleted(Active)
{
memcpy(this->name, a, 79);
}
phoneRec(const int n, const char *a, int g): numb(n), group(g), deleted(Active)
{
memcpy(this->name, a, 79);
}
void setName(const char NM[]);
const char *getName() const;
void setNumber(const int num);
int getNumber() const;
void setGroup(const int g) {group = g;}
void print() const
{
std::cout << this->name;
std::cout << " No = " << this->numb;
std::cout << " group = " << this->group << std::endl;
}
int status() const { return this->deleted;}
void del() { this->deleted = Deleted;}
private:
int numb;
char name[80];
int group;
int deleted;
};
I change strncpy to memcopy (easier to use). And the class PhoneBK keep an array of PhoneRec(MaxRec).
class phoneBk
{
public:
phoneBK() = delete;
phoneBk(const int nx): MaxRec(nx), curNum(0), nDel(0)
{
p_rec = new phoneRec [MaxRec];
}
~phoneBk() { delete [] p_rec ;}
phoneRec operator[](const int i) const {return p_rec[i];}
phoneRec& operator[](const int i) {return p_rec[i];}
int n() const {return this->curNum;} // return current record
int AddNewRec(const int nu, const char *nm)
{
if (curNum >= MaxRec) return 0;
p_rec[curNum] = phoneRec(nu, nm, Junk);
++curNum;
return 1;
}
int AddNewRec(const int nu, const char *nm, const GROUP g)
{
if (curNum >= MaxRec) return 0;
p_rec[curNum] = phoneRec(nu, nm, g);
++curNum;
return 1;
}
void deleteRec() // mark the current record deleted
{
p_rec[curNum].del();
++nDel;
}
void deleteRec(const int i) // mark ith record deleted
{
p_rec[i].del();
++nDel;
}
void flush() // refresh array removing deleted
{
phoneRec *tmp = new phoneRec [MaxRec];
int nc = 0;
for (int i=0; i<curNum; i++)
{
if (p_rec[i].status() == Deleted) continue;
memcpy((void*)(tmp+nc), (void*)(p_rec+i), sizeof(phoneRec) );
++nc;
}
delete [] this->p_rec;
this->p_rec = tmp;
tmp = nullptr;
this->curNum -= nDel;
this->nDel = 0;
}
private:
phoneRec *p_rec; // phone array.
int curNum; // current enroll number.
const int MaxRec; // Max records allow.
int nDel; // total number marked deleted.
};
And finally, a test main()
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
phoneBk fon(10);
fon.AddNewRec(12345, "aaaa", Friend);
fon.AddNewRec(22222, "bbbb", Friend);
fon.AddNewRec(33333, "cccc", Friend);
fon.AddNewRec(44444, "dddd", Family);
fon.AddNewRec(55555, "ffff", Family);
fon.AddNewRec(66666, "gggg", Family);
fon.AddNewRec(12345, "aaaa", Junk);
fon.AddNewRec(12345, "aaaa", Junk);
std::cout << "Current number = "<< fon.n() << std::endl;
for (int i=0; i<fon.n(); i++) fon[i].print();
fon.deleteRec(); // del current rec
fon.deleteRec(3); // del 4th rec
fon.flush(); //fresh array
std::cout << "Current number = "<< fon.n() << std::endl;
for (int i=0; i<fon.n(); i++) fon[i].print();
return 0;
}
The result:
$ ./a.exe
Current number = 8
aaaa No = 12345 group = 1
bbbb No = 22222 group = 1
cccc No = 33333 group = 1
dddd No = 44444 group = 0
ffff No = 55555 group = 0
gggg No = 66666 group = 0
aaaa No = 12345 group = 2
aaaa No = 12345 group = 2
Current number = 6
aaaa No = 12345 group = 1
bbbb No = 22222 group = 1
cccc No = 33333 group = 1
ffff No = 55555 group = 0
gggg No = 66666 group = 0
aaaa No = 12345 group = 2
char name[80];is this a requirement over usingstd::string? Are you permitted to use std::vector?phoneBk Family[1000];should not be an array. A family will not have 1000 phone books. A phone book may have 1000 entries.phoneRec *obj;you want to declare a dynamic array ofphoneRecobjects in aphoneBk.