C code: //libpb.c #include\"libpb.h\" void add_person(struct phone_book *pb, str
ID: 3716866 • Letter: C
Question
C code:
//libpb.c
#include"libpb.h"
void add_person(struct phone_book *pb, struct personal_info person)
{
int count = pb->num_people;
strcpy(pb->person[count].first, person.first);
strcpy(pb->person[count].last, person.last);
strcpy(pb->person[count].phone, person.phone);
count++;
pb->num_people = count;
}
void search_pb(struct phone_book pb, char find_name[])
{
int i=0, matched=0;
while(i<pb.num_people)
{
if (strcmp(find_name, pb.person[i].first) == 0)
{
matched=1;
printf(" Name: %s %s ", pb.person[i].first, pb.person[i].last);
printf("Phone: %s ", pb.person[i].phone);
}
i++;
}
if(!matched)
printf("No entries with that name. ");
}
//libpb.h
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#define MAX 20
struct personal_info
{
char first[25];
char last[25];
char phone[15];
};
struct phone_book
{
struct personal_info person[MAX];
int num_people;
};
void add_person(struct phone_book *pb, struct personal_info person);
void search_pb(struct phone_book pb, char find_name[]);
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. Renamelibpb.c to libpb.cpp and libpb.h to libpb.hpp. Convert the personal info structure 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).
2
Public member function prototypes for the personal info class:
Also, convert the structure phone book to a class with just one private member: vector<int> people. We do not need to have a data member to keep the number of people in the list (why?). There should be three public member functions for this class, with the prototypes given below. Recall add person() and search pb() function in libpb.c, and convert them to public member functions of the phone book class written in C++. Add a new functionget num people() which returns the size of the phonebook, i.e., number of the people in the phonebook.
Public member function prototypes for the phone book class:
void add person(personal info person); void search pb(string);
int get num people();
Change all printf statements to cout statements. The references to other data members of the class must also be changed accordingly.
Hints: add person(person) must push the new personal info class onto the vector of phone book, as opposed to assigning the structure to a position in the array in the pre- vious C code. Note that, we do NOT need to define or limit the size of the phonebook here, since the size of a vector in C++ can grow as needed.
Note: Remember to scope your member functions when writing the definitions in libpb.cpp
by using the scope resolution operator ::
Explanation / Answer
ScreenShot
lippb.h
//Header Files
#include<iostream>
#include<string>
#include<vector>
using namespace std;
//Personal information class
class personal_info
{
//Member Variables
string first;
string last;
string phone;
//Member function to get each member variables
public:
void setfirst(string first);
void setlast(string last);
void setphone(string phone);
string getfirst();
string getlast();
string getphone();
};
//Mutators to set all member variables values
void personal_info::setfirst(string first) {
first = first;
}
void personal_info::setlast(string last) {
last = last;
}
void personal_info::setphone(string phone) {
phone = phone;
}
//Accessors to get all member variables values
string personal_info::getfirst() {
return first;
}
string personal_info::getlast() {
return last;
}
string personal_info::getphone() {
return phone;
}
//Class phonebook
class phone_book
{
//Member variable
vector<int> people();
//Member functions to add and search values
public:
void add_person(personal_info person);
void search_pb(string);
int get_num_people();
};
libpb.cpp
#include"libpb.h"
//Get the size of vector
int phone_book::get_num_people() {
return this->people.size();
}
//add values in phonebook
void phone_book::add_person(personal_info person)
{
this->people.pushback(person.getfirst());
this->people.pushback(person.getlast());
this->people.pushback(person.getphone());
}
//Search through phone book
void phone_book::search_pb(string)
{
personal_info person;
int i = 0, matched = 0;
while (i<get_num_people())
{
if (this->people.popback(person).getFirst() != 0)
{
matched = 1;
cout << " Name: %s %s " << this->people.popback(person.getfirst()) << this->people.popback(person.getlast());
cout << "Phone: %s " << this->people.popback(person.getphone());
}
i++;
}
if (!matched)
cout << "No entries with that name. ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.