Question

C++ programming Phone Book Create a class that can be used for a Phone Book. The...

C++ programming

Phone Book

Create a class that can be used for a Phone Book. The class should have attributes for the name and phone number. The constructor should accept a name and a phone number. You should have methods that allow the name to be retrieved, the phone number to be retrieved, and one to allow the phone number to be changed. Create a toString() method to allow the name and number to be printed.

Write a program that creates a vector that can contain phone numbers. The program should prompt the user for an indeterminate number of phone numbers, create a phone number object, and add the object to the vector. After each phone number that is entered the program should display the phone number object that was just created and the total number of phone numbers in the list/vector. When the user has finished entering phone numbers into their phone book, the program should display the contents of the phone list (complete list of names/numbers).

This program should be tested at least three times with differing quantities of phone numbers.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <vector>
#include <string>

using namespace std;

class PhoneBook {
private:
    string name;
    string phoneNumber;
public:
    PhoneBook(const string &name, const string &phoneNumber) : name(name), phoneNumber(phoneNumber) {}

    string getName() const {
        return name;
    }

    string getPhoneNumber() const {
        return phoneNumber;
    }

    void change(string name, string phoneNumber) {
        this->name = name;
        this->phoneNumber = phoneNumber;
    }

    string toString() {
        string result = "Name: " + name + ", Phone number: " + phoneNumber;
        return result;
    }
};

int main() {
    vector<PhoneBook> phoneBook;
    string name, number;
    int n;
    cout << "How many numbers do you want to enter: ";
    cin >> n;
    getline(cin, name);
    for(int i = 0; i < n; ++i) {
        cout << "Enter name: ";
        getline(cin, name);
        cout << "Enter phone number: ";
        getline(cin, number);
        cout << endl;
        PhoneBook p(name, number);
        phoneBook.push_back(p);
    }
    // display phone number
    for(int i = 0; i < phoneBook.size(); ++i) {
        cout << phoneBook[i].toString() << endl;
    }
    return 0;
}

How many numbers do you want to enter: Enter phone number: Name: ronaldo, Phone number: 7 Name: messi, Phone number: 10 Name: bale, Phone number: 11 Process finished with exit code 0

\color{blue}Hey,\;Please\;let\;me\;know\;if\;you\;need\;me\;to\;make\;any\;changes. \\ I\;would\;really\;appreciate\;an\;upvote.\;Thank\;you!

Add a comment
Know the answer?
Add Answer to:
C++ programming Phone Book Create a class that can be used for a Phone Book. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Phone Book ArrayList Write a class named PhoneBookEntry that has fields for a person’s name and...

    Phone Book ArrayList Write a class named PhoneBookEntry that has fields for a person’s name and phone number. The class should have a constructor and appropriate accessor and mutator methods. Then write a program that creates at least five PhoneBookEntry objects and stores them in an ArrayList. Use a loop to display the contents of each object in the ArrayList Copyright | Addison-Wesley | Starting Out with Java | [email protected] | Printed from Chegg iOS App

  • Write a class named PhoneBookEntry that has fields for a person's name and phone number.The class...

    Write a class named PhoneBookEntry that has fields for a person's name and phone number.The class should have a constructor and appropriate accessor and mutator methods. Thenwrite a program that creates at least five PhoneBookEntry objects and stores them in anArrayLitt. Use a loop to display the contents of each object in the ArrayList.

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • In this assignment, you will implement Address and Residence classes. Create a new java project. Part...

    In this assignment, you will implement Address and Residence classes. Create a new java project. Part A Implementation details of Address class: Add and implement a class named Address according to specifications in the UML class diagram. Data fields: street, city, province and zipCode. Constructors: A no-arg constructor that creates a default Address. A constructor that creates an address with the specified street, city, state, and zipCode Getters and setters for all the class fields. toString() to print out all...

  • Using arrays create a personal phone directory that contains room for first names and phone numbers...

    Using arrays create a personal phone directory that contains room for first names and phone numbers for 20 people. Using the "names.txt" retrieve the first name and phone number for each person and store in arrays (parallel). Prompt the user for a name, search for the name, and if name is found in the array, display the corresponding phone number. If the name is not found, prompt the user to enter a phone number, and add the new name and...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • In C++, Step 1: Implement the Student Class and write a simple main() driver program to...

    In C++, Step 1: Implement the Student Class and write a simple main() driver program to instantiate several objects of this class, populate each object, and display the information for each object. The defintion of the student class should be written in an individual header (i.e. student.h) file and the implementation of the methods of the student class should be written in a corresponding source (i.e. student.cpp) file. Student class - The name of the class should be Student. -...

  • Create a class called AddressBook. It should be able to store a maximum of 50 names...

    Create a class called AddressBook. It should be able to store a maximum of 50 names and 50 phone numbers (make an array of structures in the object or two arrays). It should contain a method called addName that will take a name and phone number and add it to the address book. There should be a method called findName that will take a string for the name and return the phone number if found in the list otherwise return...

  • In Java Create a class Worker. Your Worker class should include the following attributes as String...

    In Java Create a class Worker. Your Worker class should include the following attributes as String variables: Name, Worker ID, Worker Address, Worker City, Worker State Create the constructor that initializes the above worker attributes. Then make another class name HourlyWorker that inherits from the Worker class.   HourlyWOrker has to use the inherited parent class variables and add in hourlySalary and billableHours. Your HourlyWorker class should contain a constructor that calls the constructor from the Worker class to initialize the...

  • GUI programming

    Please use netBeans Exercise 4:A librarian needs to keep track of the books he has in his Library.1. Create a class Book that has the following attributes: ISBN, Title, Author, Subject, and Price. Choose anappropriate type for each attribute.2. Add a static member BookCount to the class Book and initialize it to zero.3. Add a default constructor for the class Book. Increment the BookCount static member variable and an initialization constructor for the class Book. Increment the BookCount in all constructors.4....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT