You are on page 1of 11

National University of Sciences and

Technology NUST Islamabad

Object Oriented Programming

Name: Ch Muhammad Shaheer Yasir

Class: BEE 11-B

Registration Number: 286021


Assignment No 2

Code:
PhoneBook.h:
//Name: Ch Muhammad Shaheer Yasir
//Class: BEE 11-B
//Registration Number: 286021
//This file contains declaration of class PhoneBook and its friend functions
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class PhoneBook
{
private:
//These are all data members which we will ask user to enter to store information
string name;
string gender;
string fatherName;
double phoneNumber;
string relation;
string email;
string address;
public:
//This variable is a static data member and it will contains number of records we
have.
static int count;
PhoneBook();
void showDetails();
//These are all friend funtions so they can easily use our class data members and
modify them. They are basicaly all functions which user can perform.
friend void addContact(PhoneBook obj[], int n);
friend void list(PhoneBook objarray[]);
friend void updateContact(PhoneBook obj[]);
friend void searchContact(PhoneBook objarray[]);
friend void deleteContact(PhoneBook obj[]);
};

PhoneBook.cpp:
//Name: Ch Muhammad Shaheer Yasir
// Class: BEE 11-B
// Registration Number: 286021
// This file contains the defintion of functions and class declared in header file.
#include <iostream>
#include <string>
#include <iomanip>
#include "PhoneBook.h"
using namespace std;
//The constructor will specify some default values as soon as object is created.
PhoneBook :: PhoneBook() {
name = "";
gender = "";
fatherName = "";
phoneNumber = 0;
relation = "";
email = "";
address = "";
}
//This function is member function of class PhoneBook. All it will do is to just print
all the details of that contact.
void PhoneBook :: showDetails(){
cout << "Your contact details are as follows: " << endl;
cout << "Name: " << name << endl;
cout << "Gender: " << gender << endl;
cout << "Father Name: " << fatherName << endl;
cout << "Phone Number: " << setprecision(0) << fixed << phoneNumber << endl;
cout << "Relation: " << relation << endl;
cout << "Email Address: " << email << endl;
cout << "Permanent Address: " << address << endl;
}
//This function will add a new contact in our phonebook. It is taking an array as a
parameter and n represents actually how many records are
//still existing.It will prompt user for details and set all the data members of an
object. As it is a friend function it can access data members.
void addContact(PhoneBook obj[], int n)
{
cout << "Please enter Name: ";
cin.ignore();
//We are using getline funtion instead of cin in order to input a whole line
otherwise everything after space will be truncated.
getline(cin, obj[n].name);
cout << "Please enter Gender: ";
getline(cin, obj[n].gender);
cout << "Please enter Father's Name: ";
getline(cin, obj[n].fatherName);
cout << "Please enter Relation: ";
getline(cin, obj[n].relation);
cout << "Please enter Email Address: ";
getline(cin, obj[n].email);
cout << "Please enter Permanent Address: ";
getline(cin, obj[n].address);
cout << "Please enter Phone Number: ";
cin >> obj[n].phoneNumber;
cout << "Your contact has been added successfully." << endl;
//When the contact will be added successfully the static member count will be
incremented hence increasing one contact in phonebook.
obj[n].count++;
}
//This function will list all the existing contacts by listing their names. It also takes
an array as parameter and uses a for loop to print through
//that array of objects.
void list(PhoneBook objarray[])
{
for (int i = 0; i < objarray[0].count; i++)
{
cout << i + 1 << " " << objarray[i].name << endl;
}
}
// This function will update any data member of exisiting contact. It also takes an array
of objects which are basically all contacts.
void updateContact(PhoneBook obj[])
{
int n;
//Here it will display all contacts names and ask user which contact he wants to
update. Then it will open that contact.
cout << "Which contact do you want to update? Enter number to update it." << endl;
for (int i = 0; i < obj[0].count; i++)
{
cout << i << " " << obj[i].name << endl;
}
cin >> n;
bool condition = true;
while (condition == true)
{
// As the respective contact is opened now it asks user which data member
user wants to change. Then it will take new input from user for
//that respective data member and update it.
cout << "Which parameter do you want to update? Enter respective number to
update it." << endl;
cout << "1.Name 2.Gender 3.Father Name 4.Phone Number
5.Relation 6.Email Address 7.Address" << endl;
int number;
cin >> number;
if (number == 1){
cout << "Enter Name: ";
cin.ignore();
getline(cin, obj[n].name);
}
else if (number == 2){
cout << "Enter Gender: ";
cin.ignore();
getline(cin, obj[n].gender);
}
else if (number == 3){
cout << "Enter Father's Name: ";
cin.ignore();
getline(cin, obj[n].fatherName);
}
else if (number == 4){
cout << "Enter Phone Number: ";
cin.ignore();
cin >> obj[n].phoneNumber;
}
else if (number == 5){
cout << "Enter Relation: ";
cin.ignore();
getline(cin, obj[n].relation);
}
else if (number == 6){
cout << "Enter Email Address: ";
cin.ignore();
getline(cin, obj[n].email);
}
else if (number == 7){
cout << "Enter Address: ";
cin.ignore();
getline(cin, obj[n].address);
}
else {
cout << "Wrong Input. Please Try Again" << endl;
}
cout << "Your Parameter has been updated successfully" << endl;
//here we are using a while loop so that user dont have to open that
contact again if he wants to update anything else too. So once one contact
//is opened now you can update its any data member as many times as you
like. and if you wants to go back to menu it will give u that option too.
cout << "Do you want to update anything else or return to menu? Enter Y /
N";
string a;
cin >> a;
if (a == "N"){
condition = false;
}
}
}
//This function will search any contact and then display it whole details. It is also
taking object array as parameter.
void searchContact(PhoneBook objarray[])
{
//Here we gave user two options whether he wants to search his contact by name or
phone number. So if he remembers any of those members he is in benefit.
cout << "Do you want to search by Name or Phone Number? Press 1 for Name. Press 2
for Phone Number";
int number;
cin >> number;
if (number == 1){
string a;
cin.ignore();
getline(cin, a);
//using a for loop to loop through the whole object array names or phone
numbers and check whether user input matches with any object member.
for (int i = 0; i < objarray[i].count; i++)
{
if (a == objarray[i].name){
cout << "Contact Found" << endl;
// If contact is found i.e input and member matches then it
will call the class function showdetails of that respective object.
objarray[i].showDetails();
number = 10;
break;
}
}
//This number is only if there is no such name or phone number exisitng in
records we can tell user that there is no such contact.
if (number != 10){
cout << "Contact Not Found." << endl;
}
}
else if (number == 2)
{
double number;
cin >> number;
for (int i = 0; i < objarray[i].count; i++)
{
if (number == objarray[i].phoneNumber){
cout << "Contact Found" << endl;
objarray[i].showDetails();
number = 10;
break;
}
}
if (number != 10){
cout << "Contact Not Found." << endl;
}
}
}
//This function will delete the contact requested by user . It is also taking an object
array as a parameter.
void deleteContact(PhoneBook obj[])
{
string a;
int number;
//We prompt user to enter the name of contact and then the same procedure will go
one. We will use a for loop and check which object's member
//matches with user input and act respectively.
cout << "Please enter the name of the contact you want to delete.";
cin.ignore();
getline(cin, a);
for (int i = 0; i < obj[i].count; i++)
{
//here if any of object's name matches with input, we will set all member
of that objects to default as we did in constructor thus removing
// all information.
if (obj[i].name == a)
{
obj[i].name = "";
obj[i].gender = "";
obj[i].fatherName = "";
obj[i].phoneNumber = 0;
obj[i].relation = "";
obj[i].email = "";
obj[i].address = "";
cout << "Contact Deleted Successfully" << endl;
number = 1;
}
}
if (number != 1)
{
cout << "Contact Not Found" << endl;
}
}

TestPhoneBook.cpp:
//Name : Ch Muhammad Shaheer Yasir
//Class: BEE 11-B
//Registration Number: 286021
//This is the file containg main program.
#include <iostream>
#include <string>
#include <iomanip>
#include "PhoneBook.h"
using namespace std;
int PhoneBook::count = 0;
//These are the prototypes for out functions. Remember our functions are not member
functions of class but they are friend functions of class.
//Hence their prototypes have to be written twice both here as well as in class declaring
them friend.We can also transfer them to Phonebook.h but it
//will be more crowded we want to keep things simple :).
void addContact(PhoneBook obj[], int n);
void list(PhoneBook objarray[]);
void updateContact(PhoneBook obj[]);
void searchContact(PhoneBook objarray[]);
void deleteContact(PhoneBook obj[]);
int main()
{
// Here we are using pointer concept to dynamically allocate memory to an array
containing objects of Phonebook class which are basically all
//our contacts. So here array of 100 elements is created as I think is enough
however we can create bigger ones as required.
PhoneBook *contacts = new PhoneBook[100];
cout << " PHONEBOOK" << endl;
int n = 0;
int number;
bool condition = true;
//Here we are using while loop as to keep returning menu to user . So he can only
exit when he will choose one of the option to exit.
while (condition == true)
{
//Here we are displaying menu so user will know what functions are
available to him.
cout << " MENU" << endl;
cout << " 1. Add Contact." << endl;
cout << " 2. List." << endl;
cout << " 3. Update Contact." << endl;
cout << " 4. Search Contact." << endl;
cout << " 5. Delete Contact." << endl;
cout << " 6. Exit PhoneBook." << endl;
cin >> number;
// Here when user will enter the respective number we will act
respectively.
if (number == 1)
{
addContact(contacts, n);
// n also does the same thing as count does but count is used to
keep records in class while n contains number of records in main program.
n++;
}
else if (number == 2)
{
list(contacts);
}
else if (number == 3)
{
updateContact(contacts);
}
else if (number == 4)
{
searchContact(contacts);
}
else if (number == 5)
{
deleteContact(contacts);
}
else if (number == 6)
{
// Here when the user will choose to exit phonebook we will set
condtion false so the while loop will end and our program will exit.
condition = false;
}
}
}

Outputs:

You might also like