phone_book.cpp: libpb.c: libpb.h: Q3) Port the pre-project from C to C++. phone
ID: 3719211 • Letter: P
Question
phone_book.cpp:
libpb.c:
libpb.h:
Q3) Port the pre-project from C to C++. phone book.cpp is already done. Replace all of the C code in libpb.c and libpb.h (provided in the supplementary folder) with C++ code. Rename libpb.c to libpb.cpp and libpb.h to libpb.hpp. Convert the personal-info strudture into a class with three private data member strings for the first name, last name, and phone num- ber. To access private members of a class, there are typically public get and set functions. Get/set member function prototypes for the data members of the class personal info are shown below (to be added into your libpb.hpp file). There is a get and a set for each private data member. A set function will set the private member equal to the functions input. A get function will return the private member's value. This is typically good practice for things that should not be accessed directly (think of them as hidden from the end user)Explanation / Answer
//phone_book.cpp
#include "libpb.hpp"
#include "libpb.cpp"
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
int main()
{
char cont;
string find_name,str;
phone_book pb;
personal_info person;
cout<<" *********************************** ";
cout<<" Start eith entering new contacts! ";
cout<<" ************************************ ";
cout<<" would you like to enter a new contact(Y/N):";
while(pb.get_num_people()<20)
{
cin>>cont;
if(cont=='Y')
{
cout<<" Enter a first Name : ";
cin>>str;
person.setfirst(str);
cout<<" Enter "<<person.getfirst()<<" 's last name : ";
cin>>str;
person.setlast(str);
cout<<" Enter "<<person.getfirst()<<" 's phone number : ";
cin>>str;
person.setphone(str);
pb.add_person(person);
}
else if(cont=='N') break;
else if(cont==' ') continue;
else cout<<" Error: User entered '"<<cont<<";. Must enter either 'Y' or 'N' "<<endl;
cout<<endl<<"Would like to enter a new name(Y/N) : ";
cout<<" ************************************ ";
cout<<" Now you can search for name ! ";
cout<<" ************************************* ";
cout<<" Would you like to search for name(Y/N)? ";
}
while(1)
{
cin>>cont;
if(cont=='Y')
{
cout<<" Enter a person's name to search for : ";
cin>>find_name;
pb.search_pb(find_name);
}
else if(cont=='N') break;
else if(cont==' ') continue;
else cout<<" Error: User entered '"<<cont<<";. Must enter either 'Y' or 'N' "<<endl;
cout<<endl<<"Would like to search for name(Y/N) : ";
}
return 0;
}
//libpb.cpp
#include "libpb.hpp"
#include<iostream>
#include<string>
#include<vector>
using namespace std;
void personal_info::setfirst(string first)
{
this->first=first;
}
void personal_info::setlast(string last){
this->last=last;
}
void personal_info::setphone(string phone)
{
this->phone=phone;
}
string personal_info::getfirst()
{
return first;
}
string personal_info::getlast(){
return last;
}
string personal_info::getphone()
{
return phone;
}
void phone_book::add_person(personal_info person)
{
this->people.push_back(person);
}
void phone_book::search_pb(string find_name)
{
int num_found=0;
for(int i=0;i<people.size();i++)
{
if(find_name.compare(people[i].getfirst()))
{
cout<<" Name : "<<people[i].getfirst()<<" "<<people[i].getlast();
cout<<" Phone : "<<people[i].getphone();
num_found++;
}
}
if(num_found==0)
{
cout<<" No Entries with that name. ";
}
int phone_book :: get_num_people(){
return people.size();
}
}
//libpb.hpp
#ifndef __LIBPB_H__
#define __LIBPB_H__
#include<string>
#include<vector>
using namespace std;
class personal_info
{
public:
void setfirst(string first);
void setlast(string last);
void setphone(string phone);
string getfirst();
string getlast();
string getphone();
private:
string first;
string last;
string phone;
};
class phone_book
{
private:
vector <personal_info> people;
public:
void add_person(personal_info person);
void search_pb(string);
int get_num_people();
};
#endif
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.