Question: Create a Person class and a Glasses class. Step One Create a C++ project Create a Glasses class using a separate header file and implementation

Create a Person class and a Glasses class.

Step One Create a C++ project

  • Create a Glasses class using a separate header file and implementation file.
  • Add the following attributes.
    • Color (string data type)
    • Prescription (float data type)
  • Create a default constructor that sets default attributes.
    • Color should be set to unknown because it is not given.
    • Prescription should be set to 0.0 because it is not given.
  • Create a parameterized constructor that sets the attributes to the given values.
  • Create a destructor for the class.
  • Create a toString( ) method.
    • The toString( ) method should return a description of the object's state (e.g., Color: Black, Prescription: 1.75).
      • You can use the to_string( ) method to convert prescription to a string, or you can create a stringstream object. (Google for more info.)
  • Create accessors and mutators for the attributes.

Step 2, you will create a Person class with a Glasses composite attribute. The Person has a Glasses object.

  • Create a Person class using a separate header file and implementation file.
  • Add the following attributes.
    • Name (string data type)
    • SSN (string data type)
    • myGlasses (Glasses data type)
  • Create a default constructor that sets default attributes.
    • Name should be set to unknown because it is not given.
    • SSN should be set to unknown because it is not given.
    • myGlasses should not be created because the attribute line creates a default Glasses object.
  • Create a parameterized constructor that sets the attributes to the given values.
  • Create a destructor for the class.
  • Create a toString( ) method.
    • The toString( ) method should return a description of the object's state (e.g., Name: Bob, SSN: 555-55-5555, Glasses: [Color: Black, Prescription: 1.75]).
  • Create a writeToFile( ) method with a string return message that saves the object's state (current attribute values) to a file using the person's name (e.g., Bob.txt).
  • Create a readFromFile( ) method with a string return message that reads the object's state from a file using the person's name (e.g., Bob.txt).
  • Create accessors and mutators for the attributes.

Step 3 Create a main method for your application to test the functionality of your Composition classes.

  • Create a default Person object.
  • Create a Glasses object using the parameterized constructor.
  • Change the name and SSN of the default Person object.
  • Change the myGlasses of the default Person object using the Glasses that you created.
  • Display the changed Person object state using its toString( ) method.
  • Change the myGlasses of the Person object using an anonymous Glasses object (on the fly).
  • Display the changed Person object state using its toString( ) method.
  • Show the prescription only for the Person object.
  • Show the color of the glasses only for the Person object.
  • Write the Person object's state to the file using the writeToFile( ) behavior.
  • Create a new default Person object.
  • Set the name of the new Person to match the first Person object.
  • Read the Person object's state from the file using the readFromFile( ) behavior.
  • Display the new Person object's state using its toString( ) method

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related General Management Questions!