Challenge 21b

Description

The purpose of this challenge is to define a basic class that implements a copy constructor.

 Requirements

  1. Define a class called Contact as below
    class Contact
    {
      private: 
        string first;
        string last ;
        string * aliases;
      public:
        // default constructor
        Contact()
        {
          aliases = new string[3];
        }
    
        // copy constructor
        Contact(const Contact &contact)
        {
          // create the array
          // perform deep copy of data members 
        }
    
        // you will need to complete the definitions for these
        string get_fullname();
        void set_first(string f);
        void set_last(string l);
        
        // returns a comma-separated list of aliases
        string get_aliases();
        
        // stores alias in aliases array at position i
        void set_alias(string alias, int i);  
    };
  2. Write a default constructor that will create a new array of strings that will hold 3 elements (see above)
  3. Write a copy constructor that will perform a deep copy of the object itself (its data members and its array data)
  4. Write a getter function that will return the person’s full name as a concatenation of first and last.
  5. Complete the other public functions whose prototypes are given in the class definition above
  6. In main, instantiate an object, person1
  7. Ask the user to enter first name and last name for person1. Set person1’s first and last name using its setter functions. Use getline(cin, last) to enter a last name and getline(cin, first) to enter a first name.
  8. Ask the user to enter 3 aliases. Use getline(cin, alias) to enter a string with spaces. These aliases should be added to person1
      string alias;
    
      // this asks for one alias
      cout << "Enter an alias: ";
      getline(cin, alias);    // getline allows entry of string with spaces
  9. Instantiate another object, person2  and using object initialization, assign person1 to person2.
  10. None of the functions in the class should cout or cin. Do not ask for user input within the class (Using cout’s for debugging is ok)
  11. Ask the user to enter 3 aliases into person2. Similarly, you can use getline() to ask for aliases with spaces. You don’t have to ask for first name and last name for person2. 
  12. Create a global, non-class function
    void show_info(Contact person)
    {
      // this function will call the copy constructor automatically
      cout << person.get_fullname() << ": " << person.get_aliases() << endl;
    }
  13. In main(), call the show() function to display person1’s and person2’s full name and aliases. (In the output, notice that both objects have the same first and last name, but different aliases)
  14. Create as many supporting private/public functions as you need. Look closely at the class definition above. There is a missing function that should be created.

Sample main()

// class definition here

int main()
{
  Contact person1;
  string first;

  person1.set_first(first);

  // more code here

  show_info(person1);
  show_info(person2);

  return 0;
}

Sample Interaction

First name: Michael
Last Name: Jordan

Alias 1: MJ
Alias 2: Air Jordan
Alias 3: The GOAT

Alias 1: Greatest
Alias 2: Money
Alias 3: Number 23

Michael Jordan : MJ, Air Jordan, The GOAT
Michael Jordan : Greatest, Money, Number 23

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0021b

Print Requirements