Hi,
This is the first of possibly many posts to follow.
I must write this tool centered around 3 classes (product, order, stock).
The following questions are about the first 2 classes.
First and foremost the code
product.h
Code:
#ifndef PRODUCT_H
#define PRODUCT_H
#include <iostream>
using std::istream;
using std::ostream;
using std::string;
using std::endl;
class Product {
public:
// constructor
Product(unsigned barcode, double price, unsigned vat, const string& info):
barcode_(barcode), price_(price), vat_(vat), info_(info) { }
// Product initialiseren van stream
Product(istream& is);
// gegevens opvragen (accessor)
const unsigned barcode() const { return barcode_; }
const string& info() const { return info_; }
double price() const { return price_; }
unsigned vat() const { return vat_; }
// gegevens wijzigen (modifier)
void info(const string& info) { info_ = info; }
void price(double price) { price_ = price; }
void vat(unsigned vat) { vat_ = vat; }
// lezen/schrijven van/naar streams
bool operator==(Product& p);
friend ostream& operator<<(ostream& os, const Product& product);
private:
unsigned barcode_;
double price_;
unsigned vat_;
string info_;
};
#endif
product.cc
Code:
#include "product.h"
Product::Product(istream& is) {
unsigned barcode;
double price;
unsigned vat;
string info;
if(is >> barcode >> price >> vat)
is.get();
getline(is,info);
barcode_ = barcode;
price_ = price;
vat_ = vat;
info_ = info;
}
ostream& operator<<(ostream& os, const Product& product) {
os << product.barcode_
<< " "
<< product.price_
<< " "
<< product.vat_
<< " "
<< product.info_
<< endl;
return os;
}
order.h
Code:
#ifndef ORDER_H
#define ORDER_H
#include <map>
#include "product.h"
using std::istream;
using std::ostream;
class Order {
public:
// Constructor
Order() {}
Order(istream& is);
// Producten toevoegen en verwijderen
unsigned addOrder(const Product& product, int quantity);
unsigned cancelOrder(const Product& product, int quantity);
// schrijven op stream
friend ostream& operator<<(ostream& os, const Order& order);
private:
// Map p157
std::map <Product&, unsigned> products_;
};
#endif
order.cc
Code:
#include "order.h"
Order::Order(istream& is) {
while(is >> Product& >> quantity)
{
Product p(is);
addOrder(p, quantity);
cancelOrder (p, quantity)
}
}
unsigned Order::addOrder(const Product& product, int quantity) {
products[product]+=quantity;
return products[product];
}
unsigned Order::cancelOrder(const Product& product, int quantity) {
if (products[product] <= quantity) {
products.erase(product);
return 0;
}
else {
products[product]-=quantity;
return products[product];
}
}
question #1
Why do the first 3 files cmpile ok and the 4th(order.cc) doesn't
I just cant get order.cc bugfree
please help
question #2
How would i be able to store barcodes starting with 00 (preferably without using strings)
question #3
Would it be easy to stock all products in a multi-dimensional array, knowing that the description is a string ( in other words can i just ignore the fact that the description is a string and let the format conversion char -> int do it's work, ifso how)
thanks for your time and please ignore my ignorance
Jeffrey