0

I am now solving a class declare problem in C++. I create a class phoneRec to store the information of name and phone number, and another class phoneBk, which has 3 member arrays to store the class phoneRec objects that carried name and number information. I declared functions in phoneRec to input and save name and phone number, but I don't know how to declare functions in phoneBk to check and delete the information that from the phoneRec objects.

here is the code that I write, is there any ideas for me to complete the task?

#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include <string.h>
class phoneRec
{
public:

    void setName(char NM[]);
    char *getName();
    
    void setNumber(int num);
    int getNumber();


private:
    int numb;
    char name[80];
    

};

void phoneRec::setName(char NM[])
{
    strncpy(name, NM, 79);
}

char *phoneRec::getName()
{
    return name;
}


void phoneRec::setNumber(int num)
{
    numb=num;
}
int phoneRec::getNumber()
{
    return numb;
}



class phoneBk
{
public:

    void deletemem();
    void group();
    phoneRec *obj;

};

void phoneBk::deletemem() 
{ 
    ;
}

void phoneBk::group()
{
    phoneBk Family[1000];

    phoneBk Friend[1000];

    phoneBk Junk[1000];

}
6
  • char name[80]; is this a requirement over using std::string? Are you permitted to use std::vector? Commented Nov 28, 2020 at 16:28
  • I think phoneBk Family[1000]; should not be an array. A family will not have 1000 phone books. A phone book may have 1000 entries. Commented Nov 28, 2020 at 16:30
  • I can only use array to represent the strings in this task:( Commented Nov 28, 2020 at 16:31
  • I assume phoneRec *obj; you want to declare a dynamic array of phoneRec objects in a phoneBk. Commented Nov 28, 2020 at 16:32
  • the phonebook is to store the contact information, such as I have a name and phone number, after input these data, then I let to store in a array called Family, which can be regard as a category that govern the contact person Commented Nov 28, 2020 at 16:34

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.