Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I I am using classes in c++ (Optional - Extra Credit) Write an AddressBook class

ID: 3843218 • Letter: I

Question

I
I am using classes in c++

(Optional - Extra Credit) Write an AddressBook class that manages a collection of objects. Use the Person class developed in question 2. An AddressBook will allow a person to add, delete, or search for a Person object in the address book. The add method should add a person object to the address book. The delete method should remove the specified person object from the address book. The search method that searches the address book for a specified person and prints the list of persons matching the specified criteria. The search can be done either by first name, last name, or person id. Write a main program class to test your AddressBook.

Explanation / Answer

//.h file code:

#include <string>

namespace com
{
   namespace chegg
   {
       namespace emp
       {

           class Person
           {
           private:
               int personid = 0;
               std::wstring firstname;
               std::wstring lastname;
           public:
               virtual int getPersonid();
               virtual void setPersonid(int personid);
               virtual std::wstring getFirstname();
               virtual void setFirstname(const std::wstring &firstname);
               virtual std::wstring getLastname();
               virtual void setLastname(const std::wstring &lastname);

           };
       }
   }
}

//.cpp file code:

namespace com
{
   namespace chegg
   {
       namespace emp
       {

           int Person::getPersonid()
           {
               return personid;
           }

           void Person::setPersonid(int personid)
           {
               this->personid = personid;
           }

           std::wstring Person::getFirstname()
           {
               return firstname;
           }

           void Person::setFirstname(const std::wstring &firstname)
           {
               this->firstname = firstname;
           }

           std::wstring Person::getLastname()
           {
               return lastname;
           }

           void Person::setLastname(const std::wstring &lastname)
           {
               this->lastname = lastname;
           }
       }
   }
}

Addressbook class

//.h file code:

#include <string>
#include <vector>

namespace com
{
   namespace chegg
   {
       namespace emp
       {


           class AddressBook
           {
           public:
               std::vector<Person*> obj;

               virtual void add(Person *p);

               virtual void delete_Renamed(Person *p);

               virtual std::vector<Person*> Search(Person *p);
               static void main(std::vector<std::wstring> &args);
           };
       }
   }
}

//.cpp file code:

namespace com
{
   namespace chegg
   {
       namespace emp
       {

           void AddressBook::add(Person *p)
           {
               obj.push_back(p);
           }

           void AddressBook::delete_Renamed(Person *p)
           {
//JAVA TO C++ CONVERTER TODO TASK: The Java Collection 'remove(Object)' method is not converted:
               obj.remove(p);
           }

           std::vector<Person*> AddressBook::Search(Person *p)
           {
               std::vector<Person*>::const_iterator itr = obj.begin();

               while (itr != obj.end())
               {
                   auto element = *itr;
                   if (element->equals(p))
                   {
                       return obj;
                   }
                   itr++;
               }
               return nullptr;
           }

           void AddressBook::main(std::vector<std::wstring> &args)
           {
               Person *p = new Person();
               p->setLastname(L"bags");
               p->setFirstname(L"pa");
               p->setPersonid(1);
               AddressBook *ob = new AddressBook();
               ob->add(p);


           }
       }
   }
}