Question

Please help with a source code for C++ and also need screenshot of output: Step 1:...

Please help with a source code for C++ and also need screenshot of output:

Step 1:

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:

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 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, I completed your header file and implementation cpp files also. Here I am sending all the file with comments to most of lines for better explanation. I am also sending screen shot of execution. Message me for any clarifications.

PLEASE POST YOUR VALUABLE REVIEW THUMSUP!!

//Glasses.h

#include <iostream>

using namespace std;

//class defintion below:
class Glasses
{
   //attributes
   private:
       string Color;
       float Prescription;
  
   public:
       Glasses();//default constructor
       Glasses(string color,float prescription);//parameterized constructor
       ~Glasses();//default destructor
      
      
       string toString();//toString() method
       //accesors and mutators
       string getColor();
       void setColor(string color);
       float getPrescription();
       void setPrescription(float);
};

//Glasses.cpp implementation file
#include "Glasses.h"
  
//default constructor
Glasses::Glasses()
{
   Color="unknown";
   Prescription=0.0;
}

//parameterized constructor
Glasses::Glasses(string color,float prescription)
{
   Color=color;
   Prescription=prescription;
}

//default destructor
Glasses::~Glasses()
{
   Color="unknown"; //reset back values
   Prescription=0.0;
}

//accesors and mutators
string Glasses::getColor()
{ return Color;}

void Glasses::setColor(string color)
{ Color=color;}

float Glasses::getPrescription()
{ return Prescription;}

void Glasses::setPrescription(float prescription)
{ Prescription=prescription;}

//toString() method
string Glasses::toString()
{
   return "Color : "+Color+", "+to_string(Prescription);
}

//Step2: Person.h

#include "Glasses.cpp"

class Person
{
   //attributes
   private:
       string Name;
       string SSN;
       Glasses myGlasses;
  
   public:
       Person();//default constructor
       Person(string,string,Glasses); //Constructor with parameters
       ~Person();//destructor
       string toString();//returns description
      
       string writeToFile(); //method writeToFile
       string readFromFile();//method readfrom filie
      
       //accessors and mutators
       string getName();
       void setName(string);
       string getSSN();
       void setSSN(string);
       Glasses getGlasses();
       void setGlasses(Glasses);
};
      

//Person.cpp implementing header file
#include <fstream> //required for file operations
#include "Person.h"

/*
private:
       string Name;
       string SSN;
       Glasses myGlasses;
       */

//default constructor
Person::Person()
{
   Name=SSN="unknown";
   //myGlasses need not be initialized
}

//Constructor with parameters
Person::Person(string name,string ssn,Glasses myglasses)
{
   Name=name;
   SSN=ssn;
   myGlasses=myglasses;
}

//destructor
Person::~Person()
{
   Name=SSN="unknown";//reset values
}

//returns description
string Person::toString()
{
   return "Name : "+Name+", SSN : "+SSN+", Glasses: [ "+myGlasses.toString()+"]";
}

  
//method writeToFile
string Person::writeToFile()
{
   ofstream outfile(Name+".txt");//create output file
   outfile<<Name<<","<<SSN<<","<<myGlasses.getColor()<<","<<myGlasses.getPrescription()<<endl;//store current persons status to file
   outfile.close();//close the file
   return "Data stored in "+Name+".txt file";
}

//method readfrom filie
string Person::readFromFile()
{
   ifstream infile(Name+".txt");//opend file for reading
   string details;
   getline(infile,details); //read to line
   return details;//return the complete status line
}

//accessors and mutators
string Person::getName()
{ return Name;}

void Person::setName(string name)
{ Name=name; }

string Person::getSSN()
{ return SSN;}

void Person::setSSN(string ssn)
{ SSN=ssn;}

Glasses Person::getGlasses()
{ return myGlasses;}

void Person::setGlasses(Glasses myglasses)
{ myGlasses=myglasses;}

//Step3: implementation
//MainProgram.cpp
#include "Person.cpp"

//excutable main program here

int main()
{
   Person p1;//default person object
   Glasses g1("Black",0.4);//glass with parameters
   p1.setName("Ravi");//change name and ssn
   p1.setSSN("AP322");
   p1.setGlasses(g1);//change glasses to person
   cout<<"\n"<<p1.toString();//display Person object
  
   cout<<"\nPrescription : "<<p1.getGlasses().getPrescription();//call nested functions
   cout<<"\nColor : "<<p1.getGlasses().getColor();//call nested functions
   p1.writeToFile();//write person object to file
   Person p2;//create another default person object
   p2.setName(p1.getName());//set first person object to second person
   cout<<"\nPerson info : "<<p1.readFromFile();//read from file
   cout<<"\nNew Person info : "<<p2.toString();//display new person status
  
  
   return 0;
}

//Screenshot of execution

Sat 15:47 File Edit View Search Terminal Help Nane Ravi, SSN : AP322, Glasses: Color Black, 0.400000] Prescriptton 0.4 Color Black Person info:Rai,AP322,Black,0.4 New Person info : Name Ravi, sSN : unknown, Glasses: Color unknown, 0.090008] (program exited with code: 0) Press return to continue

Add a comment
Know the answer?
Add Answer to:
Please help with a source code for C++ and also need screenshot of output: Step 1:...
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
  • fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life...

    fisrt one is bicycle.java second code is bicycleapp.java can anyone help this??? please save my life Before you begin, DOWNLOAD the files “Lab8_Bicycle.java” and “Lab8_BicycleApp.java” from Canvas to your network drive (not the local hard drive or desktop). Once you have the files saved, RENAME THE FILES Bicycle.java and BicycleApp.java then open each file. 1) Look at the Bicycle class. Two of the many data properties we could use to define a bike are its color and the gear it...

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to...

    Deliverable A zipped NetBeans project with 2 classes app ZipCode Address Classes Suggestion: Use Netbeans to copy your last lab (Lab 03) to a new project called Lab04. Close Lab03. Work on the new Lab04 project then. The Address Class Attributes int number String name String type ZipCode zip String state Constructors one constructor with no input parameters since it doesn't receive any input values, you need to use the default values below: number - 0 name - "N/A" type...

  • Write the C# code and make a screenshot of the output according to tasks given below:...

    Write the C# code and make a screenshot of the output according to tasks given below: Create a class named Prism with protected data fields named height and volume (Use double data type for the fields) [5 points] Include public properties for each field. The Height and Volume properties should have get and set accessors. [5 points] Add default constructor which sets the value of Height and Volume properties to 1 and 1 respectively. [5 points] Add overloading constructor which...

  • Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors •...

    Diego’s Bike Lab Lab Objectives • Be able to create both default and non-default constructors • Be able to create accessor and mutator methods • Be able to create toString methods • Be able to refer to both global and local variables with the same name Introduction Until now, you have just simply been able to refer to any variable by its name. This works for our purposes, but what happens when there are multiple variables with the same name?...

  • I need this in java please Create an automobile class that will be used by a...

    I need this in java please Create an automobile class that will be used by a dealership as a vehicle inventory program. The following attributes should be present in your automobile class: private string make private string model private string color private int year private int mileage. Your program should have appropriate methods such as: default constructor parameterized constructor add a new vehicle method list vehicle information (return string array) remove a vehicle method update vehicle attributes method. All methods...

  • Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; //...

    Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; // Scanner class to support user input public class TestPetHierarchy { /* * All the 'work' of the process takes place in the main method. This is actually poor design/structure, * but we will use this (very simple) form to begin the semester... */ public static void main( String[] args ) { /* * Variables required for processing */ Scanner input = new Scanner( System.in...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

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