Use your class with a test program that creates several trips with different fue
ID: 3842388 • Letter: U
Question
Use your class with a test program that creates several trips with different fuel efficiencies. You should decide which variables should be public, if any. (Optional - Extra Credit) Write an AddressBook class that manages a collection of Person 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
#include <iostream>
using namespace std;
class Person
{
public:
char fname[20], lname[20], address[50];
int n, pid;
void add();
void search();
void del();
};
void Person::add()
{
cout<<" Enter First Name:" ;
cin>>fname;
cout<<" Enter Last Name:";
cin>>lname;
cout<<" Enter Person ID:";
cin>>pid;
cout<<" Enter Address:";
cin>>address;
}
void Person::search()
{
int id;
cout<<" Enter Person ID to be searched";
cin>>id;
for(int i=0;i<n;++i)
{
if(pid == id)
{
cout<<" Details Found";
}
if(pid != id)
{
cout<<" Details Not Found";
}
}
}
void Person::del()
{
char name[20];
cout<<" Enter Person First Name to be deleted:";
cin>>name;
for(int i=0;i<n;i++)
{
if(fname == name)
{
cout<<" Person Details is deleted";
}
}
}
int main()
{
Person per[10];
int ch, i, n;
cout<<" Menu";
cout<<" 1. Add to an address book";
cout<<" 2. Search from Address book";
cout<<" 3. Delete from an address book";
cout<<" 4. Exit";
cout<<" Enter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<" Enter Number of Person to be added:";
cin>>n;
for(i=0;i<n;i++)
{
per[i].add();
}
cout<<" Details Added";
break;
case 2:
per[i].search();
break;
case 3:
per[i].del();
break;
case 4:
cout<<" Program Ends";
break;
default:
cout<<" Enter Correct Choice";
break;
}
return 0;
}
Menu
1. Add to an address book
2. Search from Address book
3. Delete from an address book
4. Exit
Enter Your Choice:1
Enter Number of Person to be added:2
Enter First Name:sophie
Enter Last Name:margeret
Enter Person ID:1234
Enter Address:35A Chennai
Details Added
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.