Thread: help with classes

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    help with classes

    double post
    sorry
    Last edited by ammadeus; 11-25-2006 at 05:22 PM.

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    Question help with classes

    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

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    sorry for the double post

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
           std::map <Product&, unsigned> products_;
    You cannot have a map of references to Product. A reference is not copyable so it won't work with any of the stl containers.
    use
    Code:
           std::map <Product, unsigned> products_;
    btw: are you shure you want Product to be the key of the map ? If so you need to define operator < for Product.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM