【发布时间】:2020-06-14 22:17:12
【问题描述】:
我知道每个人都会说不要在所有东西上使用命名空间,但我不知道我需要做什么才能摆脱它。
我正在尝试创建一个简单的程序,它包含三个属于一个类的项目,并为其中两个函数获取用户输入。
标题:
#include <iostream>
#include <string>
using namespace std;
class retailItem {
private:
string description;
int unitsOnHand;
double price;
public:
retailItem();
retailItem(string, int, double);
~retailItem();
void setDescription(string);
string getDescription();
void setUnitsOnHand(int);
int getUnitsOnHand();
void setPrice(double);
double getPrice();
}
implementation.cpp:
#include "items.h"
using namespace std;
retailItem::retailItem() {
description = "Small Tent";
unitsOnHand = 200;
price = 35.99;
}
retailItem::retailItem(string d, int u, double p) {
description = d;
unitsOnHand = u;
price = p;
}
retailItem::~retailItem() {}
void retailItem::setDescription(string d) {
description = d;
}
string retailItem::getDescription() {
return description;
}
void retailItem::setUnitsOnHand(int u) {
unitsOnHand = u;
}
int retailItem::getUnitsOnHand() {
return unitsOnHand;
}
void retailItem::setPrice(double p) {
price = p;
}
double retailItem::getPrice() {
return price;
}
主要:
#include "items.h"
using namespace std;
void PrintItem(retailItem);
int TotalAmount(retailItem, retailItem, retailItem);
double TotalCost(retailItem, retailItem, retailItem);
int main() {
string d;
int u;
double p;
retailItem r1("Small Tent", 200, 35.99);
retailItem r2;
retailItem r3;
cout << "Welcome to the tent store!\n\n";
cout << "=====================\n\n";
cout << "Enter description for item two: \n";
getline(cin, d);
cout << "Enter amount on hand for " << d << endl;
cin >> u;
if (u < 0)
cout << "Amount must be greater than 0 \n";
else;
cout << "Enter price for " << d << endl;
cin >> p;
if (p < 0)
cout << "Price must be greater than 0 \n";
else;
r2.setDescription(d);
r2.setUnitsOnHand(u);
r2.setPrice(p);
cout << "Enter description for item three: \n";
getline(cin, d);
cout << "Enter amount on hand for " << d << endl;
cin >> u;
if (u < 0)
cout << "Amount must be greater than 0 \n";
else;
cout << "Enter price for " << d << endl;
cin >> p;
if (p < 0)
cout << "Price must be greater than 0 \n";
else;
r2.setDescription(d);
r2.setUnitsOnHand(u);
r2.setPrice(p);
PrintItem(r1);
PrintItem(r1);
PrintItem(r1);
cout << "\n The total amount on hand is: " << TotalAmount(r1, r2, r3) << endl;
cout << "\n The total price of all items is: " << TotalCost(r1, r2, r3) << endl;
}
void PrintItem(retailItem r) {
cout << "\n Item Description: " << r.getDescription();
cout << "\n Amount on hand: " << r.getUnitsOnHand();
cout << "\n Item Description: " << r.getPrice();
}
int TotalAmount(retailItem r1, retailItem r2, retailItem r3) {
return r1.getUnitsOnHand() + r2.getUnitsOnHand() + r3.getUnitsOnHand();
}
double TotalCost(retailItem r1, retailItem r2, retailItem r3) {
return r1.getPrice() + r2.getPrice() + r3.getPrice();
}
【问题讨论】:
标签: c++ visual-studio class