Question

In: Computer Science

(1) Create three files to submit: ItemToPurchase.h - Class declaration ItemToPurchase.cpp - Class definition main.cpp -...

(1) Create three files to submit:

  • ItemToPurchase.h - Class declaration
  • ItemToPurchase.cpp - Class definition
  • main.cpp - main() function

Build the ItemToPurchase class with the following specifications:

  • Default constructor
  • Public class functions (mutators & accessors)
    • SetName() & GetName() (2 pts)
    • SetPrice() & GetPrice() (2 pts)
    • SetQuantity() & GetQuantity() (2 pts)
  • Private data members
    • string itemName - Initialized in default constructor to "none"
    • int itemPrice - Initialized in default constructor to 0
    • int itemQuantity - Initialized in default constructor to 0

(2) In main(), prompt the user for two items and create two objects of the ItemToPurchase class. Before prompting for the second item, call cin.ignore() to allow the user to input a new string. (2 pts)

Ex:

Item 1
Enter the item name:
Chocolate Chips
Enter the item price:
3
Enter the item quantity:
1

Item 2
Enter the item name:
Bottled Water
Enter the item price:
1
Enter the item quantity:
10


(3) Add the costs of the two items together and output the total cost. (2 pts)

Ex:

TOTAL COST
Chocolate Chips 1 @ $3 = $3
Bottled Water 10 @ $1 = $10

Total: $13

_____________________________________________________________________________________________

given started code:

main.cpp:

#include <iostream>
using namespace std;

#include "ItemToPurchase.h"

int main() {

/* Type your code here */

return 0;
}

__________________________________________________________________________________________

ItemToPurchase.cpp

#include <iostream>
using namespace std;

#include "ItemToPurchase.h"

/* Type your code here */

___________________________________________________________________________________________

ItemToPurchase.h

#ifndef ITEM_TO_PURCHASE_H
#define ITEM_TO_PURCHASE_H

#include <string>
using namespace std;

/* Type your code here */

#endif

Solutions

Expert Solution

//C++ Code

/*============ItemToPurchase.h=======================*/
#ifndef ITEM_TO_PURCHASE_H
#define ITEM_TO_PURCHASE_H

#include <string>
using namespace std;

class ItemToPurchase {
private:
   string itemName;
       int itemPrice;
       int itemQuantity;
public:
   //default constructor
   ItemToPurchase();
   //mutators
   void SetName(const string name);
   void SetPrice(const int price);
   void SetQuantity(const int quantity);
   //accessors
   string GetName()const;
   int GetPrice() const;
   int GetQuantity() const;
};

#endif

/*====================ItemToPurchase.cpp============*/
#include <iostream>
using namespace std;

#include "ItemToPurchase.h"

//default constructor
ItemToPurchase::ItemToPurchase()
{
   itemName = "none";
   itemPrice = 0;
   itemQuantity = 0;
}
//mutators
void ItemToPurchase::SetName(const string name)
{
   itemName = name;
}
void ItemToPurchase::SetPrice(const int price)
{
   itemPrice = price;
}
void ItemToPurchase::SetQuantity(const int quantity)
{
   itemQuantity = quantity;
}
//accessors
string ItemToPurchase::GetName()const
{
   return itemName;
}
int ItemToPurchase::GetPrice() const
{
   return itemPrice;
}
int ItemToPurchase::GetQuantity() const
{
   return itemQuantity;
}

/*============main.cpp===================*/
#include <iostream>
#include<string>
using namespace std;

#include "ItemToPurchase.h"

int main() {
   string name;
   int price, quantity;
   //Create two items
   ItemToPurchase item1, item2;
   cout << "Item 1\n";
   cout << "Enter the item name: ";
   getline(cin, name);
   cout << "Enter the item price: ";
   cin >> price;
   cout << "Enter the item quantity: ";
   cin >> quantity;
   item1.SetName(name);
   item1.SetPrice(price);
   item1.SetQuantity(quantity);

   cout << "Item 2\n";
   cout << "Enter the item name: ";
   cin.ignore();
   getline(cin, name);
   cout << "Enter the item price: ";
   cin >> price;
   cout << "Enter the item quantity: ";
   cin >> quantity;
   item2.SetName(name);
   item2.SetPrice(price);
   item2.SetQuantity(quantity);

   int total = item1.GetQuantity() * item1.GetPrice() + item2.GetQuantity() * item2.GetPrice();
   cout << "TOTAL COST: " << endl;
   cout << item1.GetName() << " " << item1.GetQuantity() << " @ $" << item1.GetPrice() << " = " << item1.GetQuantity() * item1.GetPrice() << endl;
   cout << item2.GetName() << " " << item2.GetQuantity() << " @ $" << item2.GetPrice() << " = " << item2.GetQuantity() * item2.GetPrice() << endl;
   cout << "Total: $" << total << endl;
   return 0;
}

//Output

//If you need any help regarding this solution.......... please leave a comment..... thanks


Related Solutions

(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp -...
(1) Create three files to submit. ContactNode.h - Class declaration ContactNode.cpp - Class definition main.cpp - main() function (2) Build the ContactNode class per the following specifications: Parameterized constructor. Parameters are name followed by phone number. Public member functions InsertAfter() (2 pts) GetName() - Accessor (1 pt) GetPhoneNumber - Accessor (1 pt) GetNext() - Accessor (1 pt) PrintContactNode() Private data members string contactName string contactPhoneNum ContactNode* nextNodePtr Ex. of PrintContactNode() output: Name: Roxanne Hughes Phone number: 443-555-2864 (3) In main(),...
3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the...
3 files cvehicle.h -- a partially filled-out class declaration for the CVehicle class main.cpp -- the main module that creates and manipulates CVehicle objects cars.dat -- a text file that contains name data for the main module 4th file is cvehicle.cpp and it needs to be created from scratch, and cvehicle.h needs to be filled in This was the test drive: carOne = Hyundai Sonata carTwo = Hyundai Sonata carThree = Enter the make and model of a vehicle: toyota...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main()...
In Java: (1) Create two files to submit: ItemToBuy.java - Class definition ShoppingCartDriver.java - Contains main() method Build the ItemToBuy class with the following specifications: Private fields String itemName - Initialized in the nor-arg constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 No-arg Constructor (set the String instance field to "none") Public member methods (mutators & accessors) setName() & getName() setPrice() & getPrice() setQuantity() & getQuantity() toString()...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method...
JAVA (1) Create two files to submit: Payroll.java - Class definition PayrollClient.java - Contains main() method Build the Payroll class with the following specifications: 4 private fields String name - Initialized in default constructor to "John Doe" int ID - Initialized in default constructor to 9999 doulbe payRate - Initialized in default constructor to 15.0 doulbe hrWorked - Initialized in default constructor to 40 2 constructors (public) Default constructor A constructor that accepts the employee’s name, ID, and pay rate...
C (1) Create three files to submit: ItemToPurchase.h - Struct definition and related function declarations ItemToPurchase.c...
C (1) Create three files to submit: ItemToPurchase.h - Struct definition and related function declarations ItemToPurchase.c - Related function definitions main.c - main() function Build the ItemToPurchase struct with the following specifications: Data members (3 pts) char itemName [ ] int itemPrice int itemQuantity Related functions MakeItemBlank() (2 pts) Has a pointer to an ItemToPurchase parameter. Sets item's name = "none", item's price = 0, item's quantity = 0 PrintItemCost() Has an ItemToPurchase parameter. Ex. of PrintItemCost() output: Bottled Water...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language...
Create a CodeBlocks project with a main.cpp file. Submit the main.cpp file in Canvas. C++ Language A Game store sells many types of gaming consoles. The console brands are Xbox, Nintendo, PlayStation. A console can have either 16 or 8 gigabytes of memory. Use can choose the shipping method as either Regular (Cost it $5) or Expedite (Cost is $10) The price list is given as follows: Memory size/Brand Xbox Nintendo PlayStation 16 gigabytes 499.99 469.99 409.99 8 gigabytes 419.99...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class Definition ShoppingCartPrinter.java – Contains main() method Build the ItemToPurchase class with the following specifications: Specifications Description ItemToPurchase(itemName) itemName – The name will be a String datatype and Initialized in default constructor to “none”. ItemToPurchase(itemPrice) itemPrice – The price will be integer datatype and Initialized in default constructor to 0. ItemToPurchase(itemQuantity) itemQuantity – The quantity will be integer datatype Initialized in default constructor to 0....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files....
Objectives: Use class inheritance to create new classes. Separate class definition and implementation in different files. Use include guard in class header files to avoid multiple inclusion of a header. Tasks: In our lecture, we wrote a program that defines and implements a class Rectangle. The source code can be found on Blackboard > Course Content > Classes and Objects > Demo Program 2: class Rectangle in three files. In this lab, we will use class inheritance to write a...
9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter)...
9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For...
C++ Create an ArrayBag template class from scratch. This will require you to create two files:...
C++ Create an ArrayBag template class from scratch. This will require you to create two files: ArrayBag.hpp for the interface and ArrayBag.cpp for the implementation. The ArrayBag class must contain the following protected members: static const int DEFAULT_CAPACITY = 200; // max size of items_ ItemType items_[DEFAULT_CAPACITY]; // items in the array bag int item_count_; // Current count of items in bag /** @param target to be found in items_ @return either the index target in the array items_ or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT