In: Computer Science
create code for an address book console program in C++ that:
#include <iostream>
#include <string>
using namespace std;
class Record{ //.H FILE RECORD
private: //PRIVATE MEMBER
int recordNumber;
string firstName;
string lastName;
int age;
int telePhone;
public:
Record(); //DEFAULT CONSTRUCTOR
Record(int,string,string,int,int); // PARAMETERIZE
void display(); // DISPLAY FUN
void setData(int,string,string,int,int); // EXTRA FUN MUTATOR
};
Record::Record(){
recordNumber=0;
age=0;
telePhone=0;
// STRING IS NULL BY DEFAULT
}
Record::Record(int rec, string first, string last, int ag, int
phone){
// PARAMETERIZE CONSTRUCTOR
recordNumber=rec;
firstName=first;
lastName=last;
age=ag;
telePhone=phone;
}
void Record::setData(int rec, string first, string last, int ag,
int phone){
recordNumber=rec;
firstName=first;
lastName=last;
age=ag;
telePhone=phone;
}
void Record::display(){ //DISPLAY FUNCTION
cout<<"Record Number:
"<<recordNumber<<endl;
cout<<"First Name: "<<firstName<<endl;
cout<<"Last Name: "<<lastName<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Telephone: "<<telePhone<<endl;
}
void menu(){ //MENU
cout<<endl;
cout<<"----------------- MENU
-----------------"<<endl;
cout<<"| Press 1 to Insert |"<<endl;
cout<<"| Press 2 to Display |"<<endl;
cout<<"| Press 3 to exit |"<<endl;
cout<<"----------------------------------------"<<endl;
}
int main()
{
int count=0;
int reco,ag,phone,choice;
string first,last;
int size=10;
Record rec[10];
while(1){
menu();
cout<<"Enter choice: ";
cin>>choice;
if(choice==1){
if(count==10){
cout<<"Array is full"<<endl;
}
else{
cout<<"Enter record number: ";
cin>>reco;
cout<<"Enter first name: ";
cin.ignore();
getline(cin,first);
cout<<"Enter last name: ";
cin.ignore();
getline(cin,last);
cout<<"Enter age: ";
cin>>ag;
cout<<"Enter telephone Number: ";
cin>>phone;
rec[count].setData(reco,first,last,ag,phone);
count++;
}
}
else if(choice==2){
if(count==0){
cout<<"Array is empty"<<endl;
}
else{
for(int i=0;i<count;i++){
rec[i].display();
}
}
}
else if(choice ==3){
return 0;
}
else{
cout<<"Invalid option"<<endl;
}
}
return 0;
}
OUTPUT:
IF YOU HAVE ANY QUERY PLEASE COMMENT DOWN BELOW
PLEASE GIVE A THUMBS UP