Introduction
In this tutorial, we will build a simple Contact Book application in C++. The application allows users to store, manage, and display their contact information. We will implement features such as adding contacts, deleting contacts, and viewing all stored contacts. This is a basic project that can be expanded with additional features like searching contacts or storing contacts in a file for persistence.
Objective
The main objective of this program is to develop a contact management system in C++ that helps users keep track of important contacts. The application provides the ability to:
- Add new contacts.
- Delete existing contacts.
- Display all stored contacts.
Code
#include #include #include using namespace std; class Contact { public: string name; string phone; Contact(string n, string p) : name(n), phone(p) {} }; class ContactBook { private: vector contacts; public: void addContact(string name, string phone) { contacts.push_back(Contact(name, phone)); } void deleteContact(string name) { for (auto it = contacts.begin(); it != contacts.end(); ++it) { if (it->name == name) { contacts.erase(it); cout << "Contact deleted successfully.\n"; return; } } cout << "Contact not found.\n"; } void displayContacts() { if (contacts.empty()) { cout << "No contacts available.\n"; return; } cout << "Contacts List:\n"; for (const auto& contact : contacts) { cout << "Name: " << contact.name << ", Phone: " << contact.phone << "\n"; } } }; int main() { ContactBook contactBook; int choice; string name, phone; do { cout << "\nContact Book Menu:\n"; cout << "1. Add Contact\n"; cout << "2. Delete Contact\n"; cout << "3. Display Contacts\n"; cout << "4. Exit\n"; cout << "Enter your choice: "; cin >> choice; cin.ignore(); // to clear the newline character left in the buffer switch(choice) { case 1: cout << "Enter contact name: "; getline(cin, name); cout << "Enter contact phone: "; getline(cin, phone); contactBook.addContact(name, phone); break; case 2: cout << "Enter contact name to delete: "; getline(cin, name); contactBook.deleteContact(name); break; case 3: contactBook.displayContacts(); break; case 4: cout << "Exiting the program...\n"; break; default: cout << "Invalid choice! Please try again.\n"; } } while (choice != 4); return 0; }
Explanation of the Program Structure
The program is structured with two main classes: Contact and ContactBook.
Contact Class
The Contact class stores individual contact information such as the contact’s name and phone number. It has a constructor to initialize the contact details.
ContactBook Class
The ContactBook class manages a collection of Contact objects. It provides three main functions:
- addContact: Adds a new contact to the contact book.
- deleteContact: Deletes a contact from the contact book based on the name.
- displayContacts: Displays all contacts stored in the contact book.
Main Function
The main() function serves as the entry point for the program. It repeatedly displays a menu of options and takes user input to interact with the contact book. The user can choose to add, delete, or display contacts, or exit the program.
How to Run the Program
To run this program, follow these steps:
- Save the code in a file named contact_book.cpp.
- Open your terminal or command prompt.
- Navigate to the directory where the file is saved.
- Compile the program using a C++ compiler, for example:
g++ -o contact_book contact_book.cpp
- Run the program by typing:
./contact_book