Create a class called AddressBook that uses either C++ - Vector in the data section of the class. Since this is a template / generic type you need to make sure that you specify that ArrayList or vector will hold a Person. You should use the Person class you created in Assignment 1. Essentially, we are creating a collection class. In other words, the AddressBook class will be a collection of Person classes. Constructors AddressBook(); - default constructor. Should have no functionality. AddressBook(string first, string last); - Adds a Person class to the collection and sets address field to an empty string AddressBook(string first, string last, string address); - Adds a Person class to the collection. Mutators setPerson(string first, string last, string address); - Adds a Person to the collection Accessors C++ All accessors will return a pointer to a Person Person *getPerson(); - Returns the next Person in the collection. Each time this function is called it will return the next Person in the list or NULL if the AddressBook is empty. Once you get to the end of the list you should start from element 0 and return the first person. Person *findPerson(string last); - returns the found Person or NULL if Person is not in the AddressBook Person *findPerson(string first, string last); - returns the found Person or NULL of Person is not in the AddressBook void print();- Prints out the entire AddressBook
Create a class called AddressBook that uses either
- C++ - Vector
in the data section of the class. Since this is a template / generic type you need to make sure that you specify that ArrayList or vector will hold a Person. You should use the Person class you created in Assignment 1. Essentially, we are creating a collection class. In other words, the AddressBook class will be a collection of Person classes.
Constructors
- AddressBook(); - default constructor. Should have no functionality.
- AddressBook(string first, string last); - Adds a Person class to the collection and sets address field to an empty string
- AddressBook(string first, string last, string address); - Adds a Person class to the collection.
Mutators
setPerson(string first, string last, string address); - Adds a Person to the collection
Accessors
C++ All accessors will return a pointer to a Person
- Person *getPerson(); - Returns the next Person in the collection. Each time this function is called it will return the next Person in the list or NULL if the AddressBook is empty. Once you get to the end of the list you should start from element 0 and return the first person.
- Person *findPerson(string last); - returns the found Person or NULL if Person is not in the AddressBook
- Person *findPerson(string first, string last); - returns the found Person or NULL of Person is not in the AddressBook
- void print();- Prints out the entire AddressBook
Please can you write it in C++ coding form because I never took Java.
Can you help me solve this problem in C++ forms using the Classes / Constructors method like:
main.cpp
Person.h
Person.cpp
addressbook.cpp
addressbook.h
PLease write the coding in c++ forms.
Here is my coding to help you to understand the assigment.
Person.h
#include<iostream>
#include<string.h>
using namespace std;
//declare class person
class Person
{
//declare data members of class
private:
string firstName;
string lastName;
string address;
//declare member functions of class
public:
void help();
Person();
Person(string first, string last);
Person(string first, string last, string address);
void setFirstName(string first);
void setLastName (string last);
void setAddress (string address);
string getFirstName();
string getLastName();
string getAddress();
};
Person.cpp
#include "Person.h"
#include<iostream>
#include<string.h>
using namespace std;
//function to reduce redundancy
void Person::help()
{
//sets address to empty string
address="";
}
//default constructor
Person::Person()
{
//sets first and last name to empty string and calls help function to set address to empty string
firstName="";
lastName="";
help();
}
//parameterized constructor
Person::Person(string first, string last)
{
//set values of data members of the class
firstName=first;
lastName=last;
help();
}
//another parameterized constructor
Person::Person(string first, string last, string address)
{
//set values of data members of the class
firstName=first;
lastName=last;
address=address;
}
void Person::setFirstName(string first)
{
//set firstname
firstName=first;
}
void Person::setLastName(string last)
{
//set lastname
lastName=last;
}
void Person::setAddress(string address)
{
//set address
this->address=address;
}
string Person::getFirstName()
{
//return first name
return firstName;
}
string Person::getLastName()
{
//return lastname
return lastName;
}
string Person::getAddress()
{
//return address
return address;
}
main.cpp
#include "Person.h"
#include<iostream>
#include<string.h>
using namespace std;
void getPeople(Person a[])
{
int i;
string f,l,add;
//for every Person
for(int i=0;i<5;i++)
{
cout<<"Enter details of Person "<<(i+1)<<endl;
cout<<"Enter First Name ";
//input firstname
getline(cin,f);
//set firstname by calling method of class
a[i].setFirstName(f);
cout<<"Enter Last Name ";
//input lastname
getline(cin,l);
//set lastname by calling method of class
a[i].setLastName(l);
cout<<"Enter address ";
//input address
getline(cin,add);
//set address by calling method of class
a[i].setAddress(add);
}
}
void printPeople(Person a[])
{
int i;
cout<<"FirstName\tLastName\tAddress\n"<<endl;
//for every person
for(int i=0;i<5;i++)
{
//print its details
cout<<a[i].getFirstName()<<"\t\t"<<a[i].getLastName()<<"\t\t"<<a[i].getAddress();
cout<<endl;
}
}
int main()
{
//declare array of class person of size 5
Person p[5];
//call function to get inputs
getPeople(p);
//call function to print array of class Person
printPeople(p);
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 6 images