0

Say we are making an address book by creating a class AddressBook. We put a variety of tools in the class, including the option to add a member. If we use a structure to keep track of each person's information, it would look like this:

class AddressBook {  
public:
    AddressBook()
    {
        count = 0;
    }

    void AddPerson();
    //More functions..

    struct Entry_Structure
    {
        char firstName[15];
        char lastName[15];
        char idNumber[15];
    };

    Entry_Structure entries[100];
    unsigned int count;
};

Then we could write the following for the AddPerson function:

void AddressBook::AddPerson()
{
    cout << "Entry number " << (count + 1) << " : " << endl;

    cout << "Enter subject's first and last name: ";
    cin >> entries[count].firstName >> entries[count].lastName;

    cout << "Please enter the subject's ID number: ";
    cin >> entries[count].idNumber;

    ++count;                                               
}

However, instead of using a structure for Entry_Structure, I used a class.

That is, class Entry_Structure instead of struct Entry_Structure.

What would I have to change in the program and the following function to make this work?

nainometer
  • 413
  • 3
  • 17
Naomi Jacobson
  • 107
  • 1
  • 1
  • 10

1 Answers1

3

The only practical difference between class and struct is that, by default, class members are private, and struct members are public.

So:

struct Entry_Structure
{
    char firstName[15];
    char lastName[15];
    char idNumber[15];
};

Is equivalent to:

class Entry_Structure
{
public:
    char firstName[15];
    char lastName[15];
    char idNumber[15];
};
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148