Challenge 21

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();
        
        // sets one element of aliases 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, last name and 3 aliases into person1
  8. Instantiate another object, person2. Using object initialization, assign person1 to person2.
  9. 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)
  10. Ask the user to enter 3 aliases into person2. You don’t have to ask for first name and last name for person2.
  11. Display person1’s and person2’s full name and aliases. (Notice that both objects have the same first and last name, but different aliases)
  12. Create as many supporting private/public functions as you think you might need

Sample main()

// class definition here

int main()
{
  Contact person1;
  string first;

  person1.set_first(first);


  return 0;
}

Sample Interaction

First name: Michael
Last Name: Jordan
Alias 1: MJ
Alias 2: AirJordan
Alias 3: TheGOAT

Alias 1: Greatest
Alias 2: Money
Alias 3: Number23


Michael Jordan : MJ, AirJordan, TheGOAT
Michael Jordan : Greatest, Money, Number23

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0021

Print Requirements