Learn how to create a basic contact book application in C++ to store, manage, and display contacts.

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:

  1. Save the code in a file named contact_book.cpp.
  2. Open your terminal or command prompt.
  3. Navigate to the directory where the file is saved.
  4. Compile the program using a C++ compiler, for example:
    g++ -o contact_book contact_book.cpp
  5. Run the program by typing:
    ./contact_book
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)