Question: Create a struct named AddressBook that stores multiple Entry structs (
ID: 3794311 • Letter: Q
Question
Question: Create a struct named AddressBook that stores multiple Entry structs (that you created in the previous exercise). Your code must be saved in a file named AddressBook.h and should reuse your code from Entry.h.
Member functions for AddressBook should be able to add an entry and print all the entries in the address book. Your code will be tested with the file addressBook.cpp, which reads in several entries from standard input, stores them in an AddressBook instance and prints them all out.
addressBook.cpp:
Sample Input:
5
Jack
Daniel
jack@yahoo.com
Jose
Cuervo
jose@yahoo.com
Johnnie
Walker
johnnie@yahoo.com
Pierre
Smirnoff
pierre@yahoo.com
Richard
Hennessy
richard@yahoo.com
Sample Output:
First Name: Jack
Last Name: Daniel
Email: jack@yahoo.com
First Name: Jose
Last Name: Cuervo
Email: jose@yahoo.com
First Name: Johnnie
Last Name: Walker
Email: johnnie@yahoo.com
First Name: Pierre
Last Name: Smirnoff
Email: pierre@yahoo.com
First Name: Richard
Last Name: Hennessy
Email: richard@yahoo.com
Explanation / Answer
please find the code below:
AddressBook.h
--------------------------------------------------
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// AddressBook class
class AddressBook{
// private members declared here.
private:
string name;
string lastname;
string email;
// vector will hold all the AddressBook details.
std::vector<AddressBook*> addBook;
public:
// default Constructor
AddressBook(){
}
// parameterised Constructor.
AddressBook(string name, string lastname, string email){
this->name = name;
this->lastname = lastname;
this->email = email;
}
// add function to add the AddressBook reference
void add(AddressBook* ab){
addBook.push_back(ab);
}
// Display the vector
void print(){
for(int i=0;i<addBook.size();i++){
AddressBook* temp = addBook[i];
cout << '-----------------------------------------------' << endl;
cout << "Name: " << temp->name << endl;
cout << "LastName: " << temp->lastname << endl;
cout << "Email: " << temp->email << endl;
cout << '-----------------------------------------------' << endl;
}
}
};
---------------------------------------------------
The complete program:
CODE:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// AddressBook class
class AddressBook{
// private members declared here.
private:
string name;
string lastname;
string email;
// vector will hold all the AddressBook details.
std::vector<AddressBook*> addBook;
public:
// default Constructor
AddressBook(){
}
// parameterised Constructor.
AddressBook(string name, string lastname, string email){
this->name = name;
this->lastname = lastname;
this->email = email;
}
// add function to add the AddressBook reference
void add(AddressBook* ab){
addBook.push_back(ab);
}
// Display the vector
void print(){
for(int i=0;i<addBook.size();i++){
AddressBook* temp = addBook[i];
cout << "-----------------------------------------------" << endl;
cout << "Name: " << temp->name << endl;
cout << "LastName: " << temp->lastname << endl;
cout << "Email: " << temp->email << endl;
cout << "-----------------------------------------------" << endl;
}
}
};
int main(){
int n=3;
string name, lastname, email;
cin >> name;
cin >> lastname;
cin >> email;
AddressBook* myBook = new AddressBook(name,lastname,email);
AddressBook* current;
for(int i=1;i<n;i++){
cin >> name;
cin >> lastname;
cin >> email;
current = new AddressBook(name,lastname,email);
myBook->add(current);
}
myBook->print();
return 0;
}
OUTPUT:
I have randomly given name as 'a', lastname as 'aa' and email as 'aaa'.
$ g++ addressbook.cpp
$ ./a.out
a
aa
aaa
b
bb
bbb
c
cc
ccc
-----------------------------------------------
Name: b
LastName: bb
Email: bbb
-----------------------------------------------
-----------------------------------------------
Name: c
LastName: cc
Email: ccc
-----------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.