0

I have program on phone book. With the first class

class entry
{
  string name;
  int number;
   ...
}

With the 2nd class handling array of objects

   class phoneBook
{
   Entry objs[100];//maximum 100 users
   //getter and setter below
   ...
}

I have main() function to control them.

int main(){
    phoneBook p;
    p.count(count);
    p.add(count,name,number);
    p.print();
     ...
}

My questions is with the line:

 phoneBook p;

Each time I call this line, it will initialize the objs[100] in class phoneBook. Suppose I am imputing objs[2], my objs[1] will be empty then. How to fix my structure please?

6
  • 3
    I very often say that: int is not the right type for telephone numbers. Telephone numbers are strings of digits, not integer numbers. For example, in many parts of the world, most telephone numbers start with 0. Commented Sep 6, 2015 at 23:14
  • 2
    Other than that, your question is too broad. Read the next few chapters in the book or website that you use to learn C++, they will point you to the right data structures. We can't explain that here, because your question indicates that we would have to write that book to explain this to you, sorry. Commented Sep 6, 2015 at 23:15
  • I'm not entirely sure exactly what you're asking but every time you call phoneBook p; you are creating a new empty phone book. Are you creating a new phone book every time you add an entry? Commented Sep 6, 2015 at 23:49
  • Hi,Gary. Thanks for your reply. Yeah, you see the point. Every time, I call phonebook p, I create a new phonebook. But I want to avoid to create a new one, but I dont't know how. Thanks. Commented Sep 7, 2015 at 0:04
  • 1
    You'll need to read some introductory C++/object oriented tutorials. I think the answer to that is outside the scope of what we can provide here... Object oriented programming can be weird at first but you'll catch on :) Commented Sep 7, 2015 at 0:18

1 Answer 1

2

Your phoneBook class should only allocate space, if necessary. Since you have no idea the content of an entry, at initialization, setting entry values makes no sense.

I highly recommend that you use a more dynamic structure than an array, such as std::vector, std::list or std::map.

Your phoneBook class needs methods to *append, or insert or read` new entries. This will allow you to fill in the allocated spaces with reasonable data.

If you are using an array for the entries, there is only space allocated for the entries. The append method would initially place a new entry at objs[0]. The next entry at objs[1] and so on.

Also, search StackOverflow or the web for "c++ phone book example" which should yield a bunch of examples for you to ponder. Remember, search first before posting here. Or debug first before posting here.

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.