Question

FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

FOR C++ PLEASE

Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that returns the cost of shipping a box, using the following formula: Shipping price for a single box = (((length + width + height) * $0.50) + (weight * $1.00)) Finally, create a print function that prints length, width, height, address, city, state, zip code and shipping price to the screen. You may not end up needing all of the above functions in main but you still need to create and test them all so that your box class is versatile and can be used by others. Main should create an array of 3 boxes. Have the user enter the information for each box, then display the information for all boxes as well as the total shipping price for all boxes combined. You must use a class file, header file, and main file (3 files with code in them). Input validation: Length, width, height, weight should all be positive. If negative or default constructor is used set to 0. Address needs no input validation, but can have spaces in it (remember getline and ignore), if default constructor is used set to blank (assume all addresses are 1 line only). City needs no input validation but can have spaces in it, if default constructor is used set to blank. State should be exactly two characters long. If invalid or default constructor is used set to "XX" (you do NOT need to check if the state is "real", i.e. "YZ" would be valid input even though there is no state with that abbreviation - Hint: string.length() ). Zip code should be 5 digits and positive (no leading zeros), if invalid or default constructor is used set to 0. Input validation should be done in your setter functions and/or constructors as needed to ensure no bad data can get in to the class variables. Invalid input should instead set the value to a default as specified above. Your program should be split into three files, a .h file for the box class, a .cpp file for the box class, and a .cpp file for main.

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

Thanks for the question, here are the 3 files.

===============================================================

//////////////////////////////// Box.h file ///////////////////////////////////////////

#ifndef BOX_H

#define BOX_H

#include<string>

using std::string;

class Box

{

                public:

                               

                                // constructors

                                Box();

                                Box(double,double,double,double,string,string,string,int)             ;

                               

                                // getters

                                double getLength() const;

                                double getWidth() const;

                                double getHeight() const;

                                double getWeight() const;

                                string getAddress() const;

                                string getCity() const;

                                string getState() const;

                                int getZipCode() const;

                               

                                //setters

                                void setLength(double val) ;

                                void setWidth(double val) ;

                                void setHeight(double val) ;

                                void setWeight(double val) ;

                                void setAddress(string val) ;

                                void setCity(string val) ;

                                void setState(string val) ;

                                void setZipCode(int val) ;

                               

                                // shiiping price

                                double calcShippingPrice() const;

                                // print

                                void print() const;

                               

                               

                               

                private:

                                double length;

                                double width;

                                double height;

                                double weight;

                                string address;

                                string city;

                                string state;

                                int zip;

};

#endif

===============================================================

//////////////////////////////// Box.cpp file ///////////////////////////////////////////

#include "box.h"

#include<iostream>

#include<string>

using std::string;

using std::cout;

// constructors

Box::Box():length(0),width(0),height(0),weight(0),address(""),city(""),state("XX"),zip(0){

               

}

Box::Box(double len, double wid, double hght,double wt,string add , string cit, string st, int zp)    :

                length(len),width(wid),height(hght),weight(wt),address(add),city(cit),state(st),zip(zp){

                               

                                if(length<0) length=0;

                                if(width<0) width=0;

                                if(height<0) height=0;

                                if(weight<0) weight=0;

                                if(state.length()>2 || state.length()<2) state="XX";

                                if(zip<10000 || zip>99999) zip=0;

}

// getters

double Box::getLength() const { return length;}

double Box::getWidth() const { return width; }

double Box::getHeight() const { return height; }

double Box::getWeight() const { return weight; }

string Box::getAddress() const {return address; }

string Box::getCity() const { return city; }

string Box::getState() const { return state; }

int Box::getZipCode() const { return zip; }

//setters

void Box::setLength(double val) { length=val<0?0:val;}

void Box::setWidth(double val) { width=val<0?0:val;}

void Box::setHeight(double val) { height=val<0?0:val;}

void Box::setWeight(double val) { weight=val<0?0:val;}

void Box::setAddress(string val) { address=val; }

void Box::setCity(string val) { city=val;}

void Box::setState(string val) { state=val;if(state.length()>2 || state.length()<2) state="XX"; }

void Box::setZipCode(int val) {zip = val; if(zip<10000 || zip>99999) zip=0;}

// shiiping price

double Box::calcShippingPrice() const {

                return (length+width+height)*0.5 + weight*1.00;

}

// print

void Box::print() const {

               

                cout<<"Dimension (in inches): "<<length<<" x "<<width<<" x "<<height<<", Weight (in pounds): "<<weight<<"\n";

                cout<<"Delivery Address: "<<address<<", City: "<<city<<", State: "<<state<<", Zip Code: "<<zip<<"\n";

                cout<<"Shipping Price: $"<<calcShippingPrice()<<"\n";

}

===============================================================

//////////////////////////////// BoxMain.cpp file ///////////////////////////////////////////

#include <iostream>

#include<string>

#include "Box.h"

using namespace std;

int main(){

               

                                double length;

                                double width;

                                double height;

                                double weight;

                                string address;

                                string city;

                                string state;

                                int zip;

                               

                Box boxes[3];

               

                for(int i=0; i<3; i++){

                               

                                cout<<"Box "<<i+1<<endl;

                                cout<<"Enter length: "; cin>>length;cout<<"Enter width: "; cin>>width;cout<<"Enter height: "; cin>>height;

                                cout<<"Enter weight: "; cin>>weight;

                                cin.ignore(256,'\n');

                                cout<<"Enter address: "; getline(cin,address,'\n');

                                cout<<"Enter city: "; getline(cin,city,'\n');

                                cout<<"Enter state (e.g. NY ): "; getline(cin,state,'\n');

                                cout<<"Enter zip code (5 digit): "; cin>>zip;

                               

                                boxes[i].setLength(length);

                                boxes[i].setWidth(width);

                                boxes[i].setHeight(height);

                                boxes[i].setWeight(weight);

                                boxes[i].setAddress(address);

                                boxes[i].setCity(city);

                                boxes[i].setState(state);

                                boxes[i].setZipCode(zip);

               

                }

               

                cout<<endl;

                for(int i=0; i<3; i++){

                                cout<<"Box "<<i+1<<endl;cout<<"=======================================================================\n";

                                boxes[i].print();

                                cout<<"=======================================================================\n";

                                cout<<endl<<endl;

                }

               

}

===============================================================

thanks!

Add a comment
Know the answer?
Add Answer to:
FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...
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
  • (Classes and Objects) Design a class named Box. The class should have the following private members:...

    (Classes and Objects) Design a class named Box. The class should have the following private members: width - A double member variablethat holds the width of the box. length – A double member variable for holding the length of the box. height – A double member variable of type double that holds the height of the box. In addition, provide the following member functions with full (inline) implementation. a) A default constructor that sets all member variables to 0. b)...

  • Long Answer 2: (Classes and Objects) Design a class named Box. The class should have the...

    Long Answer 2: (Classes and Objects) Design a class named Box. The class should have the following private members: width - A double member variablethat holds the width of the box. length – A double member variable for holding the length of the box. height – A double member variable of type double that holds the height of the box. In addition, provide the following member functions with full (inline) implementation. a) A default constructor that sets all member variables...

  • Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default...

    Create a BoxFigure class that inherits RectangleFigure class BoxFigure Class should contain:- Height instance field- Default constructor -- that calls the default super class constructor and sets the default height to 0- Overloaded constructor with three parameters (length, width and height) – that calls the two parameterized super class constructor passing in length and width passed and sets the height to passed height.- setDimension method with three parameters (length, width, height) – call the super class setDimension and pass in...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • Question 4: CLO5 Write a class called Rectangle that has length and width as instance variables,...

    Question 4: CLO5 Write a class called Rectangle that has length and width as instance variables, a constructor with two parameter to initialize length and width, set and get methods for each variables and the following methods: a. The first method calculates and returns the value of the ratio of the length to the width of the rectangle. b. The second method determines whether a rectangle is a Square depending on the value of the ratio, which should be calculated...

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day is...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

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