221k views
4 votes
(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 constructorPublic 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

User Mark Comix
by
7.0k points

1 Answer

2 votes

Answer:

We have the files and codes below with appropriate comments

Step-by-step explanation:

ItemToPurchase.h:

#pragma once

#ifndef ITEMTOPURCHASE_H_INCLUDED

#define ITEMTOPURCHASE_H_INCLUDED

#include<string>

#include <iostream>

using namespace std;

class ItemToPurchase

{

public:

//Declaration of default constructor

ItemToPurchase();

//Declaration of SetName function

void SetName(string ItemName);

//Declaration of SetPrice function

void SetPrice(int itemPrice);

//Declaration of SetQuantity function

void SetQuantity(int itemQuantity);

//Declaration of GetName function

string GetName();

//Declaration of GetPrice function

int GetPrice();

//Declaration of GetQuantity function

int GetQuantity();

private:

//Declaration of itemName as

//type of string

string itemName;

//Declaration of itemPrice as

//type of integer

int itemPrice;

//Declaration of itemQuantity as

//type of integer

int itemQuantity;

};

#endif

ItemToPurchase.cpp:

#include <iostream>

#include <string>

#include "ItemToPurchase.h"

using namespace std;

//Implementation of default constructor

ItemToPurchase::ItemToPurchase()

{

itemName = "none";

itemPrice = 0;

itemQuantity = 0;

}

//Implementation of SetName function

void ItemToPurchase::SetName(string name)

{

itemName = name;

}

//Implementation of SetPrice function

void ItemToPurchase::SetPrice(int itemPrice)

{

this->itemPrice = itemPrice;

}

//Implementation of SetQuantity function

void ItemToPurchase::SetQuantity(int itemQuantity)

{

this->itemQuantity = itemQuantity;

}

//Implementation of GetName function

string ItemToPurchase::GetName()

{

return itemName;

}

//Implementation of GetPrice function

int ItemToPurchase::GetPrice()

{

return itemPrice;

}

//Implementation of GetQuantity function

int ItemToPurchase::GetQuantity()

{

return itemQuantity;

}

main.cpp:

#include<iostream>

#include<string>

#include "ItemToPurchase.h"

using namespace std;

int main()

{

//Declaration of ItemToPurchase class objects

ItemToPurchase item1Cart, item2Cart;

string itemName;

//create a variable names like itemPrice

//itemQuantity,totalCost as type of integer

int itemPrice;

int itemQuantity;

int totalCost = 0;

//Display statement for Item1

cout << "Item 1:" << endl;

cout << "Enter the item name : " << endl;

//call the getline function

getline(cin, itemName);

//Display statememt

cout << "Enter the item price : " << endl;

cin >> itemPrice;

cout << "Enter the item quantity : " << endl;

cin >> itemQuantity;

item1Cart.SetName(itemName);

item1Cart.SetPrice(itemPrice);

item1Cart.SetQuantity(itemQuantity);

//call cin.ignore() function

cin.ignore();

//Display statement for Item 2

cout << endl;

cout << "Item 2:" << endl;

cout << "Enter the item name : " << endl;

getline(cin, itemName);

cout << "Enter the item price : " << endl;

cin >> itemPrice;

cout << "Enter the item quantity : " << endl;

cin >> itemQuantity;

item2Cart.SetName(itemName);

item2Cart.SetPrice(itemPrice);

item2Cart.SetQuantity(itemQuantity);

//Display statement

cout << "TOTAL COST : " << endl;

cout << item1Cart.GetName() << " " << item1Cart.GetQuantity() << " @ $" << item1Cart.GetPrice() << " = " << (item1Cart.GetQuantity()*item1Cart.GetPrice()) << endl;

cout << item2Cart.GetName() << " " << item2Cart.GetQuantity() << " @ $" << item2Cart.GetPrice() << " = " << (item2Cart.GetQuantity()*item2Cart.GetPrice()) << endl;

totalCost = (item1Cart.GetQuantity()*item1Cart.GetPrice()) + (item2Cart.GetQuantity()*item2Cart.GetPrice());

cout << endl;

cout << "Total : $" << totalCost << endl;

return 0;

}

User Premal Khetani
by
6.4k points