Partial overriding



  • @#ifndef PRODUCT_H
    #define PRODUCT_H
    #include <QString>

    class Product {

    private:
    QString m_Description;
    double m_PricePerItem;
    int m_NoOfItems;

    public:
    Product(QString d,double p,int n);
    virtual void sell(int n);
    };

    #endif // PRODUCT_H

    product.cpp

    #include "product.h"
    Product::Product(QString d,double p,int n):m_Description(d),m_PricePerItem(p),
    m_NoOfItems(n){}

    void Product::sell(int n) {
    if (m_NoOfItems > 0) {
    m_NoOfItems -= n;

    }
    }

    FoodProduct.h

    #ifndef FOODPRODUCT_H
    #define FOODPRODUCT_H
    #include "product.h"
    #include <QDate>

    class FoodProduct:public Product {
    private:
    QDate m_SellByDate;

    public:
    FoodProduct(QString d,double p,int n,QDate sbd):Product(d,p,n),
    m_SellByDate (sbd){}

    //overriden sell function
    void sell(int n);
    };
    #endif // FOODPRODUCT_H

    **FoddProduct.cpp
    #include "foodproduct.h"
    void FoodProduct::sell(int n) {
    if (QDate::currentDate() < m_SellByDate) {

    }
    }**@

    The sell function in FoodProduct ensures that the product has not expired and then it reduces the no of items in stock by the requested number only if there is sufficient no of items in stock. It uses partial overriding.

    so it is something like:

    if (QDate::currentDate() < m_SellByDate) { //check if it is expired

    The sell() function in Product should be invoked with the request (n) that is asked by the client. how?



  • See "here":http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.5.
    @
    void FoodProduct::sell(int n) {
    if (QDate::currentDate() < m_SellByDate) {
    Product::sell(n);
    }
    }
    @



  • When I tried it the compiler outputs

    product.exe exited with code -1073741510



  • I don't see any reason for this in the code you've provided. You either run your code in debug mode to actually find out where it crashes or you post a small, compilable example that reproduces your problem so we can take a look at it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.