Making a Phone Book application in C++ is interesting; as a result of it permits you to learn the language much.
Creating sample applications in any programing language is a remarkable job. And this may be the most effective approach to learning any programing language.
Learn one of the most powerful and portable programming languages C++ and become eligible to apply for the positions at Google, Microsoft, Facebook, Amazon etc.
As a technologist, your goal is to master the programming skills so you’ll be able to develop a code application. A lot of you write programs, a lot of new sensible habits you’ll develop. Besides this, you’ll even be able to observe a number of the language options by repeatedly victimization them in your programme.
The focus of this text is to point out the way the Phone Book application is developed in C++ victimization the OOP principles. You’ll fathom the options of the Phone Book application. You’ll additionally learn the way to effectively implement the OOP principles in your program. The ASCII text file is explained properly.
This article can take you through the subsequent.
- Summary and options
- Implementing the OOP principles
- Style and committal to writing: however the coding was done
Learn the Importance of learning Data Structures for C++ at Coding Ninjas.
Features of my Phonebook application
Before diving into technical details, an outline of the sample is critical. First of all, this can be not an entire huge project. Instead, this can be a sample project. Sample project means that a demo project, a little application that demonstrates, however, a full-working project of this kind will be developed. This little sample is developed so you’ll be able to learn bound options of the C++ programing language.
This is a sample program made in C++. That is, this can be a Console programme, not GUI. As the name implies, the telephone book is associate degree application that permits you to perform the terribly basic phone book operations like storing, showing, deleting and looking contacts. This application permits you to perform the required operations quickly. therefore it will be pretty helpful in world things. The options square measure explained as follows.
Show Contact Details: You will see the list of the names together with their phone numbers victimisation this feature. This feature shows you the contact list. All the contacts square measure shown one by one in a very list.
Add contacts: This feature permits you to feature a replacement contact to the contacts-list of the telephone book application. Adding new contacts is incredibly simple. you only got to enter the contact’s name and signal. A message are going to be shown upon productive addition of any contacts.
Validations: This feature ensures that you simply build solely the valid entries. It doesn’t permit you to feature contact with propaganda. for instance, you can’t add a mobile variety of one hundred digits.
Edit contacts: The existing contacts will be altered victimisation this feature. you’ll be able to edit the present contacts and save them to the contacts-list.
Deleting contacts: Contacts will be deleted from the contacts-list. you’ll be able to delete a contact from the most menu.
Search contacts: The looking facility permits you to search for a contact by name. you’ll be able to search a contact by coming into the contact’s name. If the contact isn’t found, the associate degree acceptable message is shown.
NOTE: This sample doesn’t embody the ideas of file-programming, Database-programming and Windows API programming. therefore once the applying is closed, all the contacts square measure nonexistent. As I already mentioned that this can be not a full-working sample, this doesn’t embody all the options that a perfect telephone book application ought to have. In fact, as a learner, this can be your task to develop this sample to the additional stage. There’s an enormous biological process scope.
Also Read - C++ Interview Questions
Implementing the OOP principles
C++ is associate degree object-oriented programing language. the most principles of OOP square measure encapsulation, polymorphism and inheritance. the first advantage of OOP is that it considerably reduces the committal to writing complexness. Generally, this can be noticeable in giant programs. complexness is reduced in many ways. The technique is by activity the implementation details within a category.
One of the most effective ways to programming is to use objects, provide some purposeful names to the objects and picture them as real-life objects like automobile, pen, ball, pencil etc. Then use values and ways for them. a lot of specifically, you’ll be able to set colour, size, height etc. To a real-life object, and you’ll be able to specify what actions those objects can perform. this can be however programmed square measure written victimisation C++.
Instead of imagining something like an enormous assortment of technical stuff, this could be way easier for {human beings|citizenry|folks|kinsfolk|kinsmen|kith associate degreed kin|masses|men|mortals|people at large|people in general|persons|groups of people|individuals|personalities} to imagine one thing as an object.
In easy words, after we say “car”, the image of a true automobile comes in our mind. it’s extraordinarily simple to imagine it as associate degree object, whereas it’s extraordinarily tough to imagine a “car” as a huge assortment of all the scientific, technical, electrical and mechanical stuff. In alternative words, it’s tough to imagine however it’s developed. within the same means, C++ hides bound things.
It permits you to cover an explicit side of the complete development. therefore rather than knowing however it’s developed, you would like to specialise in a way to implement. therefore the category styles can design and write the categories. As a technologist, you merely got to learn the way to implement the categories in your programme.
Designing the category: The name of the category is “contact”. There square measure 2 information members – name and mob. Examine {the category|the category} below to envision the information members and member functions utilised in this class of the telephone book project.
Code:
class contact
{
string name;
string mob;
public:
contact(): name(""), mob("")
{}
bool show();
bool show(string search_term);
bool name_exists(string tname);
bool add(string new_name, string new_mob);
bool edit(string);
bool erase(string new_name);
};
Creating an array of objects
An array of objects of the ‘contact’ class is created inside the main function. The given argument creates an array of objects.
contact person[100];
A person is the name of the object. A real-life significant name has been given to the present object in order that this C++ object seems like true-life objects. Thus it becomes easier to understand and write the code. This also increases the readability of the code.
100 objects are created. So you cannot add more than a hundred contacts to the contacts-list of the Phonebook application. You may use dynamic memory allocation to create objects as per your requirements. This also saves memory space. And the program becomes light.
Designing a menu-driven GUI: A console program is considerably less easy than an interface program. This way, a menu has been further to the program so it becomes additional easy to the users of this application. The menu is as follows:
- Show contacts.
- Add contact.
- Edit contact.
- Delete contact.
- Search contact.
- Quit
Consider the subsequent code.
Code:
cout << “0. Show contacts” << endl << “1. Add Contact” << endl << “2. Edit Contact” << endl << “3. Delete Contact” << endl << “4. Search” << endl << “5. Quit” << endl << endl << “Your selection…”; cin >> choice;
So if the user presses zero, the contacts area unit shown. If the user presses one, they’ll add a contact. during this means, the program becomes comparatively additional easy.
Showing contacts: The following code shows all the contacts from the contacts list. Examine the below code to know however it works.
Code:
//This block of code resides within the most operate
cout << “Showing Contacts” << endl;
printline(‘-‘, 20);
for(i=0; i<100; i++)
if(person[i].show())
flag = 1;
if(!flag)
cout << "No contacts found!" << endl;
//This block of code resides within the category
bool show()
{
if(name != “”)
{
cout << name << “\t” << mob << endl;
return 1; //Indicates success
}
else
return 0; //Indicates failure
}
Adding contacts: The following code adds a replacement contact to the contacts-list of the telephone directory application.
Code:
//The following code resides within the main perform.
cout << “Add New Contact\t\t\t\tpress $ to cancel” << endl;
printline(‘-‘, 20);
counter = 0;
//Loop till correct name and mobile variety area unit entered
do
{
flag = 0;
if(counter)
cout << "Try again\t\t\t\tpress $ to cancel"
<< endl;
//counts how many times the do-while loop executes
counter++;
cout << "Name: "; cin >> temp_name;
//Cancel operation
if(temp_name=="$")
{
cancel_flag = 1;
break;
}
cout << "Mobile No.: "; cin >> temp_mob;
//Cancel operation
if(temp_mob=="$")
{
cancel_flag = 1;
break;
}
//Check whether or not name exists
for(i=0; i<100; i++)
if(person[i].name_exists(temp_name))
{
cout << "The name you entered is already there"
" in the telephone directory, enter a distinct name."
<< endl;
flag = 1;
break;
}
}while(!name_valid(temp_name) ||
flag ||
!mob_valid(temp_mob));
if(cancel_flag)
{
system("cls");
break;
}
//This code adds the contact to phonebook
for(i=0; i<100; i++)
if(person[i].add(temp_name, temp_mob))
{
cout << "Contact added successfully!" << endl;
flag = 1;
break;
}
if(!flag)
cout << "Memory full! Delete some contacts first."
<< endl;
//The following code resides in the class
bool add(string new_name, string new_mob)
{
if(name=="")
{
name = new_name;
mob = new_mob;
return 1; // Success
}
else
return 0; // Failure
}
The higher than code checks whether or not the user has entered an accurate name and an accurate mobile variety. If the user has not entered the proper name and proper mobile variety, the program prompts the user to enter a valid reputation and a sound mobile variety. After that, it checks whether or not the name entered is already there within the telephone directory. The program prompts the user to enter another name. The user continually has the choice to cancel the operation.
If their area unit already a hundred contacts, a message is going to be shown indicating that the memory is full. during this case, the user must delete some contacts so as to enter new contacts. a hundred is the limit because the array size is a hundred. you’ve got 2 choices in order that the program will hold a lot of contacts. Either modification the array size or use dynamic memory allocation.
Editing a contact: The following code edits an existing contact. It edits both – name and mobile number.
Code:
// the subsequent code resides within the main perform.
cout << “Enter a contact name to edit:” “\t\t\t\tpress $ to cancel\n”; cin >> temp_name;
//Cancel Operation
if(temp_name=="$")
{
system("cls");
break;
}
for(i=0; i<100; i++)
if(person[i].edit(temp_name))
{
cout << "Edited Successfully!" << endl;
flag = 1;
break;
}
if(!flag)
cout << "Contact name not found!" << endl;
// the subsequent code resides within the category
bool contact :: edit(string new_name)
{
string new_mob;
if(new_name==name)
{
cout << “Enter new name: “; cin >> new_name;
cout << “Enter new mobile no: “; cin >> new_mob;
name = new_name;
mob = new_mob;
return 1;
}
else
return 0;
}
Removing a contact: The given code removes a contact from the phone directory.
Code:
// the subsequent code resides within the main perform.
do
{
if(counter)
cout << "Try again" << endl; counter++; cout << "Enter a contact name to delete:" "\t\t\tpress $ to cancel\n"; cin >> temp_name;
//Cancel Operation
if(temp_name=="$")
{
system("cls");
break;
}
//Final Confirmation
for(i=0; i<100; i++)
if(person[i].name_exists(temp_name))
{
flag = 1;
cout << "Are you sure you want to delete? (1/0)"
<< endl;
int yes;
cin >> yes;
if(!yes)
{
system("cls");
cancel_flag = 1;
}
break;
}
if(!flag)
cout << "Contact name not found!" << endl;
if(cancel_flag)
break;
// This code deletes the contact
if(flag)
{
for(i=0; i<100; i++)
if(person[i].erase(temp_name))
{
cout << "Deleted successfully!" << endl;
break;
}
}
}while(!flag);
// the subsequent code resides within the category
bool erase(string new_name)
{
if(new_name==name)
{
name = "";
mob = "";
return 1;
}
else
return 0;
}
Searching for a contact: The following code searches for a contact.
Code:
// the subsequent code resides within the main perform.
do
{
if(counter)
cout << "Try again" << endl; counter++; cout << "Search a name: \t\t\t\tpress $ to cancel\n"; cin >> temp_name;
//Cancel Operation
if(temp_name=="$")
{
system("cls");
break;
}
for(i=0; i<100; i++)
if(person[i].show(temp_name))
{
flag = 1;
break;
}
if(!flag)
cout << "Contact name not found" << endl;
}while(!flag);
// the subsequent code resides within the category
bool show(string search_term)
{
if(search_term == name)
{
cout << name << "\t" << mob << endl;
return 1;
}
else
return 0;
}
The printline() function: The printline() perform prints a line. you’ll specify the dimensions and also the character for drawing the road.
Code:
void printline(char ch, int size)
{
for(int i=0; i<size; i++)
cout << ch;
cout << "\n";
}
Validations: Two functions are used for validations. One is name_valid(), another is mob_valid. the primary one checks whether or not the name is valid, whereas the second perform checks whether or not the mobile variety is valid.
Code:
bool name_valid(string tname)
{
if(tname.size()>20)
{
cout << "Invalid Name!\nEnter a name within 20 characters!"
<< endl;
return 0;
}
else if(tname == "")
{
cout << "Invalid Name!\nName cannot be blank!" << endl;
return 0;
}
else
return 1;
}
//mobile variety validation
bool mob_valid(string tmob)
{
if(tmob.size()>13 || tmob.size()<10)
{
cout << "Invalid mobile no.\nEnter a no."
"between 10 and 13 digits" << endl;
return 0;
}
else if(tmob == "")
{
cout << "Invalid mobile no.\nMobile"
"no cannot be blank" << endl;
return 0;
}
else
return 1;
}
Try and compile by yourself with the help of online C++ Compiler for better understanding.