C++ 1) Design a class called Product that has the following member variables: e
ID: 3734889 • Letter: C
Question
C++ 1) Design a class called Product that has the following member variables: e description. A string that holds the description of the product. price. A double variable that holds the price of the product. The class should have the following member functions: A default constructor that assigns 0 to the price variable. A constructor that accepts the following values as arguments and assign them to the appropriate member variables: the description of the product and the price of the product. setDescription: Stores a value in description setPrice: Stores a value in price getDescription: Returns the value in description getPrice: Returns the value in price 2) Design a class called LineItem that has the following member variables: prod. A Product object. quantity. An integer variable that holds the quantity The class should have the following member functions A default constructor that assigns 0 to the quantity variable A constructor that accepts the following as arguments and assign them to the appropriate member variables: a product object and a quantity getTotalPrice: Returns the total price (Computes the total cost of this line item). print: Prints the line item. It prints the description of the product, the price of the product, the quantity, and the total price of the line item e The program should ask the user for the description, the price, and the quantity and will then display the information as in the following screenshot. iption of your iten: 6GB White Snartphone nter the price of your iten: 78.88 nter the quantity: escription Price Quantity Total Price 578.0 6GB White Snartphone 2858.0 ress any key to continue. -Explanation / Answer
Answer:
LineItemDisplay.cpp:
#include <iostream>
#include <string>
using namespace std;
// Class Product
class Product{
//Memeber variables
private:
string description;
double price;
//member functions
public:
Product(){// Default Constructor
setPrice(0);
}
Product(string desc,double prc ){// Parameterised Constructor
setPrice(prc);
setDescription(desc);
}
void setPrice(double prc){//Price setter method
price = prc;
}
double getPrice(){ // Price getter method
return price;
}
void setDescription(string desc){// description setter method
description = desc;
}
string getDescription(){//description getter method
return description;
}
};
//Class LineItem
class LineItem{
//Memeber variables
private:
Product prod;
int quantity;
//member functions
public:
LineItem(){// Default Constructor
quantity = 0;
}
LineItem(Product p, int quant){// Parameterised Constructor
prod = p;
quantity = quant;
}
double getTotalPrice(){ // method to total price
return prod.getPrice() * quantity; //returns total price
}
//method to display the Line item
void print(){
cout<<endl<<"Description Price Quantity Total Price"<<endl;
cout<<prod.getDescription()<<" "<<prod.getPrice()<<" "<<quantity<<" $"<<getTotalPrice();
}
};
// Driver function
int main() {
// Varaible to read input from user
string desc;
int quant;
double prc;
cout<<"Enter the Description of your Item:"<<endl;
getline(cin,desc);
cout<<"Enter the price of the Item:"<<endl;
cin>>prc;
cout<<"Enter the quantity:"<<endl;
cin>>quant;
Product p(desc, prc); // Intialising the class Product with description and price values
LineItem l(p,quant); // Intialising the class LineItem with Product object and quantity values
l.print();// Calling print function from LineItem class
}
Output:
Enter the Description of your Item:
16TB Hard Drive
Enter the price of the Item:
150.50
Enter the quantity:
7
Description Price Quantity Total Price
16TB Hard Drive 150.5 7 $1053.5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.