请给我完整的代码实现与一个配套的调试程序
时间: 2024-04-02 20:31:40 浏览: 18
好的,下面是完整的代码实现,包括头文件和实现文件:
**InvoiceItem.h**
```c++
#ifndef INVOICEITEM_H
#define INVOICEITEM_H
#include <string>
class InvoiceItem {
public:
InvoiceItem();
InvoiceItem(const std::string& name, double costPrice, int quantity);
void setName(const std::string& name);
void setCostPrice(double costPrice);
void setQuantity(int quantity);
std::string getName() const;
double getCostPrice() const;
int getQuantity() const;
double getUnitPrice() const;
double getItemSubtotal() const;
double getSalesTax() const;
double getSalesTotal() const;
void printInvoice() const;
private:
std::string m_name;
double m_costPrice;
int m_quantity;
};
#endif // INVOICEITEM_H
```
**InvoiceItem.cpp**
```c++
#include "InvoiceItem.h"
#include <iostream>
InvoiceItem::InvoiceItem() : m_name(""), m_costPrice(0.0), m_quantity(0) {}
InvoiceItem::InvoiceItem(const std::string& name, double costPrice, int quantity)
: m_name(name), m_costPrice(costPrice), m_quantity(quantity) {}
void InvoiceItem::setName(const std::string& name) {
m_name = name;
}
void InvoiceItem::setCostPrice(double costPrice) {
m_costPrice = costPrice;
}
void InvoiceItem::setQuantity(int quantity) {
m_quantity = quantity;
}
std::string InvoiceItem::getName() const {
return m_name;
}
double InvoiceItem::getCostPrice() const {
return m_costPrice;
}
int InvoiceItem::getQuantity() const {
return m_quantity;
}
double InvoiceItem::getUnitPrice() const {
return m_costPrice * 1.3;
}
double InvoiceItem::getItemSubtotal() const {
return getUnitPrice() * m_quantity;
}
double InvoiceItem::getSalesTax() const {
return getItemSubtotal() * 0.06;
}
double InvoiceItem::getSalesTotal() const {
return getItemSubtotal() + getSalesTax();
}
void InvoiceItem::printInvoice() const {
std::cout << "Item: " << getName() << std::endl;
std::cout << "Quantity: " << getQuantity() << std::endl;
std::cout << "Cost Price: $" << getCostPrice() << std::endl;
std::cout << "Unit Price: $" << getUnitPrice() << std::endl;
std::cout << "Subtotal: $" << getItemSubtotal() << std::endl;
std::cout << "Sales Tax: $" << getSalesTax() << std::endl;
std::cout << "Total: $" << getSalesTotal() << std::endl;
}
```
下面是一个配套的调试程序,你可以将代码保存到一个文件中,然后编译运行:
**main.cpp**
```c++
#include "InvoiceItem.h"
#include <iostream>
int main() {
std::string name;
double costPrice;
int quantity;
std::cout << "Enter the name of the item: ";
std::getline(std::cin, name);
std::cout << "Enter the cost price of the item: $";
std::cin >> costPrice;
std::cout << "Enter the quantity of the item: ";
std::cin >> quantity;
InvoiceItem item(name, costPrice, quantity);
item.printInvoice();
return 0;
}
```
在运行程序时,你需要输入商品的名称、成本价和购买数量,程序会自动计算商品小计、销售税和销售额,并打印出发票信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)